/* eslint-disable @typescript-eslint/no-explicit-any */ import { ToolbarOptions } from "../../../type"; import _ from "lodash"; /** * Checks if there are unsaved changes in fields with enableCustomView=true * Compares current uiElementGroupData with saved state */ export const hasUnsavedChanges = ( currentData: Record, savedData: Record | null, toolBarOptions: ToolbarOptions, ): boolean => { if (!currentData) return false; // Get the filter fields for the active widget type const filterFields: Array> = [ ...(currentData.relativeWidgetFilterFields ?? []), { field: "globalSearch", enableCustomView: toolBarOptions.globalSearch.enableCustomView, applicableValueProperty: "appliedQuery", }, { field: "advancedSearch", enableCustomView: toolBarOptions.advancedSearch.enableCustomView, applicableValueProperty: "appliedQuery", }, { field: "quickFilter", enableCustomView: toolBarOptions.quickFilter.enableCustomView, applicableValueProperty: "appliedQuery", }, { field: "groupedBy", enableCustomView: toolBarOptions.groupBy.enableCustomView, applicableValueProperty: "appliedGroups", }, { field: "setting", enableCustomView: toolBarOptions.setting.enableCustomView, applicableValueProperty: null, }, ]; let unsavedChangesFound = false; filterFields.forEach((filterField) => { if (filterField.enableCustomView) { const newValue = filterField.applicableValueProperty ? currentData[filterField.field] ? currentData[filterField.field][filterField.applicableValueProperty] : currentData[filterField.field] : currentData[filterField.field]; const oldData = savedData ? savedData[filterField.field] : null; if (!_.isEqual(newValue ?? null, oldData ?? null)) { unsavedChangesFound = true; return; } } }); return unsavedChangesFound; };