Skip to content

Commit

Permalink
feat(dynamic): normaliza data in isModified calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimzinchuk committed Oct 16, 2024
1 parent 3c769f6 commit 2389032
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,12 @@ export default {

return () => {
return properties && properties.length
? properties?.map((field) =>
h(
field.component as Component,
Object.assign(unrefNested(field.props), {
class: unrefNested(props.baseProps).classNames ?? "",
}),
),
? properties.map((field) =>
h(field.component as Component, {
...unrefNested(field.props),
class: unrefNested(props.baseProps).classNames ?? "",
key: field.props.key,
}),
)
: null;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComputedRef, MaybeRef, computed, ref, watch } from "vue";
import { ComputedRef, MaybeRef, computed, ref, watch, toRaw } from "vue";
import * as _ from "lodash-es";
import { useForm, useIsFormValid } from "vee-validate";
import { useAsync, useLoading } from "../../../../../core/composables";
Expand Down Expand Up @@ -74,14 +74,41 @@ export const useDetailsFactory = <Item extends { id?: string }>(factoryParams: U
}),
);

function normalizeData(data: unknown): unknown {
if (
data === null ||
data === undefined ||
data === "" ||
(Array.isArray(data) && data.length === 0) ||
(typeof data === "object" && data !== null && Object.keys(data).length === 0)
) {
return undefined;
}

if (Array.isArray(data)) {
return data.map((item) => normalizeData(item));
}

if (typeof data === "object") {
const normalizedObj: Record<string, unknown> = {};
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
normalizedObj[key] = normalizeData(data[key as keyof typeof data]);
}
}
return normalizedObj;
}

return data;
}

watch(
[() => item, () => itemTemp],
[() => item.value, () => itemTemp.value],
([state, stateCopy]) => {
isModified.value = !_.isEqualWith(stateCopy.value, state.value, (a, b) => {
if (a === undefined && b === null) return true;
if (a === null && b === undefined) return true;
return undefined; // Use default comparison for other cases
});
const normalizedState = normalizeData(state);
const normalizedStateCopy = normalizeData(stateCopy);

isModified.value = !_.isEqual(normalizedState, normalizedStateCopy);
},
{ deep: true },
);
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4937,17 +4937,17 @@ __metadata:
languageName: unknown
linkType: soft

"@vcmp-vendor-portal/api@npm:^1.0.315, @vcmp-vendor-portal/api@workspace:apps/vendor-portal/src/api_client":
"@vcmp-vendor-portal/api@npm:^1.0.316, @vcmp-vendor-portal/api@workspace:apps/vendor-portal/src/api_client":
version: 0.0.0-use.local
resolution: "@vcmp-vendor-portal/api@workspace:apps/vendor-portal/src/api_client"
languageName: unknown
linkType: soft

"@vcmp-vendor-portal/modules@npm:^1.0.315, @vcmp-vendor-portal/modules@workspace:apps/vendor-portal/src/modules":
"@vcmp-vendor-portal/modules@npm:^1.0.316, @vcmp-vendor-portal/modules@workspace:apps/vendor-portal/src/modules":
version: 0.0.0-use.local
resolution: "@vcmp-vendor-portal/modules@workspace:apps/vendor-portal/src/modules"
peerDependencies:
"@vcmp-vendor-portal/api": ^1.0.315
"@vcmp-vendor-portal/api": ^1.0.316
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -5691,8 +5691,8 @@ __metadata:
"@vc-shell/framework": "npm:^1.0.313"
"@vc-shell/release-config": "npm:^1.0.313"
"@vc-shell/ts-config": "npm:^1.0.313"
"@vcmp-vendor-portal/api": "npm:^1.0.315"
"@vcmp-vendor-portal/modules": "npm:^1.0.315"
"@vcmp-vendor-portal/api": "npm:^1.0.316"
"@vcmp-vendor-portal/modules": "npm:^1.0.316"
"@virtocommerce/import-app": "npm:^1.0.167"
"@vitejs/plugin-vue": "npm:5.0.3"
"@vue/eslint-config-prettier": "npm:^9.0.0"
Expand Down

0 comments on commit 2389032

Please sign in to comment.