feat: revert the resource columns

This commit is contained in:
Ahmed Bouhuolia
2024-04-07 23:48:23 +02:00
parent 086b060351
commit a1f8417b5d
21 changed files with 304 additions and 610 deletions

View File

@@ -1,7 +1,20 @@
// @ts-nocheck
import { useMemo } from 'react';
import { chain, isEmpty, lowerCase, head, last, set, get } from 'lodash';
import { useImportFileContext } from './ImportFileProvider';
import {
chain,
isEmpty,
lowerCase,
head,
last,
set,
get,
assign,
} from 'lodash';
import {
EntityColumn,
SheetColumn,
useImportFileContext,
} from './ImportFileProvider';
import { useImportFileMapBootContext } from './ImportFileMappingBoot';
import { deepdash, transformToForm } from '@/utils';
import { ImportFileMappingFormValues } from './_types';
@@ -34,15 +47,40 @@ export const transformValueToReq = (
};
/**
*
* @param value
* @returns
* Transformes the mapping response to form values.
* @param {{ from: string; to: string, group: string }[]} value
* @returns {Record<string, object | string>}
*/
export const transformResToFormValues = (
value: { from: string; to: string }[],
) => {
value: { from: string; to: string , group: string }[],
): Record<string, object | string> => {
return value?.reduce((acc, map) => {
acc[map.to] = map.from;
const path = map?.group ? `${map.group}.${map.to}` : map.to;
set(acc, path, map.from);
return acc;
}, {});
};
/**
* Retrieves the initial values of mapping form.
* @param {EntityColumn[]} entityColumns
* @param {SheetColumn[]} sheetColumns
*/
const getInitialDefaultValues = (
entityColumns: EntityColumn[],
sheetColumns: SheetColumn[],
) => {
return entityColumns.reduce((acc, { fields, groupKey }) => {
fields.forEach(({ key, name }) => {
const _name = lowerCase(name);
const _matched = sheetColumns.find(
(column) => lowerCase(column) === _name,
);
const _key = groupKey ? `${groupKey}.${key}` : key;
const _value = _matched ? _matched : '';
set(acc, _key, _value);
});
return acc;
}, {});
};
@@ -59,30 +97,18 @@ export const useImportFileMappingInitialValues = () => {
() => transformResToFormValues(importFile?.map || []),
[importFile?.map],
);
const initialValues = useMemo(
() =>
entityColumns.reduce((acc, { fields, groupKey }) => {
fields.forEach(({ key, name }) => {
const _name = lowerCase(name);
const _matched = sheetColumns.find(
(column) => lowerCase(column) === _name,
);
const _key = groupKey ? `${groupKey}.${key}` : key;
const _value = _matched ? _matched : '';
set(acc, _key, _value);
});
return acc;
}, {}),
// Retrieves the initial default values.
const initialDefaultValues = useMemo(
() => getInitialDefaultValues(entityColumns, sheetColumns),
[entityColumns, sheetColumns],
);
return useMemo<Record<string, any>>(
() => ({
...transformToForm(initialResValues, initialValues),
...initialValues,
}),
[initialValues, initialResValues],
() =>
assign(
initialDefaultValues,
transformToForm(initialResValues, initialDefaultValues),
),
[initialDefaultValues, initialResValues],
);
};