Feat/front temp seed custom objects (#2070)
* wip * Fixed bugs * Added flexible backend test
This commit is contained in:
@ -1,20 +1,28 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { isFlexibleBackendEnabledState } from '@/client-config/states/isFlexibleBackendEnabledState';
|
||||
import { ObjectsQuery } from '~/generated-metadata/graphql';
|
||||
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
import { useApolloClientMetadata } from '../hooks/useApolloClientMetadata';
|
||||
import { useSeedCustomObjectsTemp } from '../hooks/useSeedCustomObjectsTemp';
|
||||
import { metadataObjectsState } from '../states/metadataObjectsState';
|
||||
import { MetadataObject } from '../types/MetadataObject';
|
||||
|
||||
export const FetchMetadataEffect = () => {
|
||||
const [metadataObjects, setMetadataObjects] =
|
||||
useRecoilState(metadataObjectsState);
|
||||
|
||||
const [isFlexibleBackendEnabled] = useRecoilState(
|
||||
isFlexibleBackendEnabledState,
|
||||
);
|
||||
const apolloClientMetadata = useApolloClientMetadata();
|
||||
|
||||
const seedCustomObjectsTemp = useSeedCustomObjectsTemp();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isFlexibleBackendEnabled) return;
|
||||
|
||||
(async () => {
|
||||
if (apolloClientMetadata && metadataObjects.length === 0) {
|
||||
const objects = await apolloClientMetadata.query<ObjectsQuery>({
|
||||
@ -31,10 +39,38 @@ export const FetchMetadataEffect = () => {
|
||||
fields: object.node.fields.edges.map((field) => field.node),
|
||||
}));
|
||||
setMetadataObjects(formattedObjects);
|
||||
} else if (
|
||||
objects.data.objects.edges.length === 0 &&
|
||||
metadataObjects.length === 0
|
||||
) {
|
||||
try {
|
||||
await seedCustomObjectsTemp();
|
||||
|
||||
const objects = await apolloClientMetadata.query<ObjectsQuery>({
|
||||
query: GET_ALL_OBJECTS,
|
||||
});
|
||||
|
||||
const formattedObjects: MetadataObject[] =
|
||||
objects.data.objects.edges.map((object) => ({
|
||||
...object.node,
|
||||
fields: object.node.fields.edges.map((field) => field.node),
|
||||
}));
|
||||
|
||||
setMetadataObjects(formattedObjects);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
}, [metadataObjects, setMetadataObjects, apolloClientMetadata]);
|
||||
}, [
|
||||
isFlexibleBackendEnabled,
|
||||
metadataObjects,
|
||||
setMetadataObjects,
|
||||
apolloClientMetadata,
|
||||
seedCustomObjectsTemp,
|
||||
]);
|
||||
|
||||
return <></>;
|
||||
};
|
||||
|
||||
@ -11,8 +11,7 @@ import { sortsScopedState } from '@/ui/data/view-bar/states/sortsScopedState';
|
||||
import { useRecoilScopeId } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopeId';
|
||||
|
||||
import { useFindManyCustomObjects } from '../hooks/useFindManyCustomObjects';
|
||||
|
||||
import { useSetObjectDataTableData } from './useSetDataTableData';
|
||||
import { useSetObjectDataTableData } from '../hooks/useSetDataTableData';
|
||||
|
||||
export const ObjectDataTableEffect = ({
|
||||
objectName,
|
||||
|
||||
@ -1,61 +0,0 @@
|
||||
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