Feat/front temp seed custom objects (#2070)
* wip * Fixed bugs * Added flexible backend test
This commit is contained in:
83
front/src/modules/metadata/hooks/useSeedCustomObjectsTemp.ts
Normal file
83
front/src/modules/metadata/hooks/useSeedCustomObjectsTemp.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import {
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables,
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_FIELD, CREATE_ONE_OBJECT } from '../graphql/mutations';
|
||||
|
||||
import { useApolloClientMetadata } from './useApolloClientMetadata';
|
||||
|
||||
export const useSeedCustomObjectsTemp = () => {
|
||||
const client = useApolloClientMetadata();
|
||||
|
||||
return async () => {
|
||||
if (!client) return;
|
||||
|
||||
const { data: createSuppliersData } = await client?.mutate<
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_OBJECT,
|
||||
variables: {
|
||||
input: {
|
||||
object: {
|
||||
labelPlural: 'Suppliers',
|
||||
labelSingular: 'Supplier',
|
||||
nameSingular: 'supplier',
|
||||
namePlural: 'suppliers',
|
||||
description: 'Suppliers',
|
||||
icon: 'IconBuilding',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const supplierObjectId = createSuppliersData?.createOneObject?.id ?? '';
|
||||
|
||||
await client?.mutate<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_FIELD,
|
||||
variables: {
|
||||
input: {
|
||||
field: {
|
||||
objectId: supplierObjectId,
|
||||
labelSingular: 'Name',
|
||||
nameSingular: 'name',
|
||||
type: 'text',
|
||||
description: 'Name',
|
||||
labelPlural: 'Names',
|
||||
namePlural: 'names',
|
||||
placeholder: 'Name',
|
||||
icon: 'IconBuilding',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await client?.mutate<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_FIELD,
|
||||
variables: {
|
||||
input: {
|
||||
field: {
|
||||
objectId: supplierObjectId,
|
||||
labelSingular: 'City',
|
||||
nameSingular: 'city',
|
||||
type: 'text',
|
||||
description: 'City',
|
||||
labelPlural: 'Cities',
|
||||
namePlural: 'cities',
|
||||
placeholder: 'City',
|
||||
icon: 'IconMap',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
};
|
||||
61
front/src/modules/metadata/hooks/useSetDataTableData.ts
Normal file
61
front/src/modules/metadata/hooks/useSetDataTableData.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { useResetTableRowSelection } from '@/ui/data/data-table/hooks/useResetTableRowSelection';
|
||||
import { isFetchingDataTableDataState } from '@/ui/data/data-table/states/isFetchingDataTableDataState';
|
||||
import { numberOfTableRowsState } from '@/ui/data/data-table/states/numberOfTableRowsState';
|
||||
import { TableRecoilScopeContext } from '@/ui/data/data-table/states/recoil-scope-contexts/TableRecoilScopeContext';
|
||||
import { tableRowIdsState } from '@/ui/data/data-table/states/tableRowIdsState';
|
||||
import { entityFieldsFamilyState } from '@/ui/data/field/states/entityFieldsFamilyState';
|
||||
import { availableFiltersScopedState } from '@/ui/data/view-bar/states/availableFiltersScopedState';
|
||||
import { availableSortsScopedState } from '@/ui/data/view-bar/states/availableSortsScopedState';
|
||||
import { entityCountInCurrentViewState } from '@/ui/data/view-bar/states/entityCountInCurrentViewState';
|
||||
import { useRecoilScopeId } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopeId';
|
||||
|
||||
export const useSetObjectDataTableData = () => {
|
||||
const resetTableRowSelection = useResetTableRowSelection();
|
||||
|
||||
const tableContextScopeId = useRecoilScopeId(TableRecoilScopeContext);
|
||||
|
||||
return useRecoilCallback(
|
||||
({ set, snapshot }) =>
|
||||
<T extends { node: { id: string } }>(newEntityArrayRaw: T[]) => {
|
||||
const newEntityArray = newEntityArrayRaw.map((entity) => entity.node);
|
||||
|
||||
for (const entity of newEntityArray) {
|
||||
const currentEntity = snapshot
|
||||
.getLoadable(entityFieldsFamilyState(entity.id))
|
||||
.valueOrThrow();
|
||||
|
||||
if (JSON.stringify(currentEntity) !== JSON.stringify(entity)) {
|
||||
set(entityFieldsFamilyState(entity.id), entity);
|
||||
}
|
||||
}
|
||||
|
||||
const entityIds = newEntityArray.map((entity) => entity.id);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log({ newEntityArray, entityIds });
|
||||
|
||||
set(tableRowIdsState, (currentRowIds) => {
|
||||
if (JSON.stringify(currentRowIds) !== JSON.stringify(entityIds)) {
|
||||
return entityIds;
|
||||
}
|
||||
|
||||
return currentRowIds;
|
||||
});
|
||||
|
||||
resetTableRowSelection();
|
||||
|
||||
set(numberOfTableRowsState, entityIds.length);
|
||||
|
||||
set(entityCountInCurrentViewState, entityIds.length);
|
||||
|
||||
set(availableFiltersScopedState(tableContextScopeId), []);
|
||||
|
||||
set(availableSortsScopedState(tableContextScopeId), []);
|
||||
|
||||
set(isFetchingDataTableDataState, false);
|
||||
},
|
||||
[resetTableRowSelection, tableContextScopeId],
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user