|
|
@@ -1,5 +1,5 @@
|
|
|
import { reactive, computed, toRaw, isReactive, watch } from "vue";
|
|
|
-import { isEqual as isEqualDates, parse, isValid } from "date-fns";
|
|
|
+import { isEqual } from "lodash";
|
|
|
|
|
|
export const useFormUpdateTracker = (initialFormValue) => {
|
|
|
const form = reactive(deepClone(initialFormValue));
|
|
|
@@ -17,14 +17,10 @@ export const useFormUpdateTracker = (initialFormValue) => {
|
|
|
watch(
|
|
|
form,
|
|
|
(newValue) => {
|
|
|
- const changes = diff(newValue, originalForm);
|
|
|
- Object.keys(changes).forEach((key) => {
|
|
|
- if (changes[key] === null) {
|
|
|
- delete updatedFields[key];
|
|
|
- } else {
|
|
|
- updatedFields[key] = deepClone(changes[key]);
|
|
|
- }
|
|
|
- });
|
|
|
+ const changes = diff(toRaw(newValue), originalForm);
|
|
|
+ let tempObj = {}
|
|
|
+ Object.assign(tempObj, changes);
|
|
|
+ updatedFields.value = tempObj
|
|
|
},
|
|
|
{ deep: true },
|
|
|
);
|
|
|
@@ -169,83 +165,3 @@ function deepClone(obj) {
|
|
|
}
|
|
|
return JSON.parse(JSON.stringify(obj));
|
|
|
}
|
|
|
-
|
|
|
-/**
|
|
|
- * Attempts to parse a string into a Date object using a list of expected formats.
|
|
|
- * @param {string} str The string to parse.
|
|
|
- * @returns {Date|null} A valid Date object or null if parsing fails.
|
|
|
- */
|
|
|
-function tryParseDate(str) {
|
|
|
- if (typeof str !== "string") {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- const dateFormats = ["yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd"];
|
|
|
-
|
|
|
- for (const format of dateFormats) {
|
|
|
- const parsedDate = parse(str, format, new Date());
|
|
|
- if (isValid(parsedDate)) {
|
|
|
- return parsedDate;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Compares two values for deep equality.
|
|
|
- * Handles arrays, objects, primitives, Date objects, and common date string formats.
|
|
|
- * @param {*} a The first value to compare.
|
|
|
- * @param {*} b The second value to compare.
|
|
|
- * @returns {boolean} True if the values are equal, false otherwise.
|
|
|
- */
|
|
|
-export function isEqual(a, b) {
|
|
|
- if (a === b) return true;
|
|
|
-
|
|
|
- if (
|
|
|
- typeof a !== "object" ||
|
|
|
- typeof b !== "object" ||
|
|
|
- a === null ||
|
|
|
- b === null
|
|
|
- ) {
|
|
|
- if (typeof a === "string" && typeof b === "string") {
|
|
|
- const dateA = tryParseDate(a);
|
|
|
- const dateB = tryParseDate(b);
|
|
|
-
|
|
|
- if (dateA && dateB) {
|
|
|
- return isEqualDates(dateA, dateB);
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (a instanceof Date && b instanceof Date) {
|
|
|
- return isEqualDates(a, b);
|
|
|
- }
|
|
|
-
|
|
|
- if (Array.isArray(a) && Array.isArray(b)) {
|
|
|
- if (a.length !== b.length) return false;
|
|
|
- for (let i = 0; i < a.length; i++) {
|
|
|
- if (!isEqual(a[i], b[i])) return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- if (Array.isArray(a) || Array.isArray(b)) return false;
|
|
|
-
|
|
|
- const aKeys = Object.keys(a);
|
|
|
- const bKeys = Object.keys(b);
|
|
|
-
|
|
|
- if (aKeys.length !== bKeys.length) return false;
|
|
|
-
|
|
|
- for (const key of aKeys) {
|
|
|
- if (
|
|
|
- !Object.prototype.hasOwnProperty.call(b, key) ||
|
|
|
- !isEqual(a[key], b[key])
|
|
|
- ) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
-}
|