Move settings data model refreshMetadata to sync calls

This commit is contained in:
Charles Bochet 2024-12-12 19:16:30 +01:00
parent d56c815897
commit 3f9acacea7
13 changed files with 84 additions and 41 deletions

View File

@ -66,7 +66,7 @@ export const PageChangeEffect = () => {
useEffect(() => {
if (isDefined(pageChangeEffectNavigateLocation)) {
navigate(pageChangeEffectNavigateLocation);
setIsAppWaitingForFreshObjectMetadataDebounced();
// setIsAppWaitingForFreshObjectMetadataDebounced();
}
}, [
navigate,

View File

@ -11,6 +11,8 @@ export const ObjectMetadataItemsGater = ({
isAppWaitingForFreshObjectMetadataState,
);
console.log(isAppWaitingForFreshObjectMetadata);
const shouldDisplayChildren = !isAppWaitingForFreshObjectMetadata;
return (

View File

@ -11,6 +11,7 @@ export const ObjectMetadataItemsProvider = ({
children,
}: React.PropsWithChildren) => {
const objectMetadataItems = useRecoilValue(objectMetadataItemsState);
console.log('objectMetadataItems', objectMetadataItems);
const shouldDisplayChildren = objectMetadataItems.length > 0;

View File

@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import {
CreateFieldInput,
@ -8,12 +7,14 @@ import {
} from '~/generated-metadata/graphql';
import { CREATE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { useApolloMetadataClient } from './useApolloMetadataClient';
export const useCreateOneFieldMetadataItem = () => {
const apolloMetadataClient = useApolloMetadataClient();
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const [mutate] = useMutation<
CreateOneFieldMetadataItemMutation,
@ -23,15 +24,17 @@ export const useCreateOneFieldMetadataItem = () => {
});
const createOneFieldMetadataItem = async (input: CreateFieldInput) => {
return await mutate({
const result = await mutate({
variables: {
input: {
field: input,
},
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
return result;
};
return {

View File

@ -1,5 +1,4 @@
import { useApolloClient, useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
@ -10,13 +9,15 @@ import {
} from '~/generated-metadata/graphql';
import { CREATE_ONE_OBJECT_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { useApolloMetadataClient } from './useApolloMetadataClient';
export const useCreateOneObjectMetadataItem = () => {
const apolloMetadataClient = useApolloMetadataClient();
const apolloClient = useApolloClient();
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const { findManyRecordsQuery } = useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.View,
@ -34,10 +35,10 @@ export const useCreateOneObjectMetadataItem = () => {
variables: {
input: { object: input },
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
return createdObjectMetadata;
};

View File

@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import {
CreateOneRelationMetadataMutation,
@ -7,12 +6,12 @@ import {
} from '~/generated-metadata/graphql';
import { CREATE_ONE_RELATION_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import {
formatRelationMetadataInput,
FormatRelationMetadataInputParams,
} from '../utils/formatRelationMetadataInput';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { useApolloMetadataClient } from './useApolloMetadataClient';
export const useCreateOneRelationMetadataItem = () => {
@ -25,14 +24,19 @@ export const useCreateOneRelationMetadataItem = () => {
client: apolloMetadataClient,
});
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const createOneRelationMetadataItem = async (
input: FormatRelationMetadataInputParams,
) => {
return await mutate({
const result = await mutate({
variables: { input: { relation: formatRelationMetadataInput(input) } },
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
return result;
};
return {

View File

@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import {
DeleteOneFieldMetadataItemMutation,
@ -7,8 +6,8 @@ import {
} from '~/generated-metadata/graphql';
import { DELETE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { useApolloMetadataClient } from './useApolloMetadataClient';
export const useDeleteOneFieldMetadataItem = () => {
@ -21,16 +20,21 @@ export const useDeleteOneFieldMetadataItem = () => {
client: apolloMetadataClient,
});
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const deleteOneFieldMetadataItem = async (
idToDelete: DeleteOneFieldMetadataItemMutationVariables['idToDelete'],
) => {
return await mutate({
const result = await mutate({
variables: {
idToDelete,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
return result;
};
return {

View File

@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import {
DeleteOneObjectMetadataItemMutation,
@ -7,8 +6,8 @@ import {
} from '~/generated-metadata/graphql';
import { DELETE_ONE_OBJECT_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { useApolloMetadataClient } from './useApolloMetadataClient';
export const useDeleteOneObjectMetadataItem = () => {
@ -21,16 +20,21 @@ export const useDeleteOneObjectMetadataItem = () => {
client: apolloMetadataClient,
});
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const deleteOneObjectMetadataItem = async (
idToDelete: DeleteOneObjectMetadataItemMutationVariables['idToDelete'],
) => {
return await mutate({
const result = await mutate({
variables: {
idToDelete,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
return result;
};
return {

View File

@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import { DELETE_ONE_RELATION_METADATA_ITEM } from '@/object-metadata/graphql/mutations';
import {
@ -7,8 +6,7 @@ import {
DeleteOneRelationMetadataItemMutationVariables,
} from '~/generated-metadata/graphql';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { useApolloMetadataClient } from './useApolloMetadataClient';
export const useDeleteOneRelationMetadataItem = () => {
@ -21,16 +19,21 @@ export const useDeleteOneRelationMetadataItem = () => {
client: apolloMetadataClient,
});
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const deleteOneRelationMetadataItem = async (
idToDelete: DeleteOneRelationMetadataItemMutationVariables['idToDelete'],
) => {
return await mutate({
const result = await mutate({
variables: {
idToDelete,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
return result;
};
return {

View File

@ -1,3 +1,4 @@
import { isAppWaitingForFreshObjectMetadataState } from '@/object-metadata/states/isAppWaitingForFreshObjectMetadataState';
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { useRecoilCallback } from 'recoil';
import { generatedMockObjectMetadataItems } from '~/testing/mock-data/generatedMockObjectMetadataItems';
@ -15,6 +16,14 @@ export const useLoadMockedObjectMetadataItems = () => {
) {
set(objectMetadataItemsState, generatedMockObjectMetadataItems);
}
if (
snapshot
.getLoadable(isAppWaitingForFreshObjectMetadataState)
.getValue() === true
) {
set(isAppWaitingForFreshObjectMetadataState, false);
}
},
[],
);

View File

@ -1,5 +1,6 @@
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '@/object-metadata/graphql/queries';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import { isAppWaitingForFreshObjectMetadataState } from '@/object-metadata/states/isAppWaitingForFreshObjectMetadataState';
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { mapPaginatedObjectMetadataItemsToObjectMetadataItems } from '@/object-metadata/utils/mapPaginatedObjectMetadataItemsToObjectMetadataItems';
@ -7,13 +8,18 @@ import { useRecoilCallback } from 'recoil';
import { ObjectMetadataItemsQuery } from '~/generated-metadata/graphql';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
export const useRefreshObjectMetadataItems = () => {
type FetchPolicy = 'network-only' | 'cache-first';
export const useRefreshObjectMetadataItems = (
fetchPolicy: FetchPolicy = 'cache-first',
) => {
const client = useApolloMetadataClient();
const refreshObjectMetadataItems = async () => {
const result = await client.query<ObjectMetadataItemsQuery>({
query: FIND_MANY_OBJECT_METADATA_ITEMS,
variables: {},
fetchPolicy,
});
const objectMetadataItems =
@ -34,6 +40,7 @@ export const useRefreshObjectMetadataItems = () => {
)
) {
set(objectMetadataItemsState, toSetObjectMetadataItems);
set(isAppWaitingForFreshObjectMetadataState, false);
}
},
[],

View File

@ -1,5 +1,4 @@
import { useApolloClient, useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import {
UpdateOneFieldMetadataItemMutation,
@ -7,9 +6,9 @@ import {
} from '~/generated-metadata/graphql';
import { UPDATE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { GET_CURRENT_USER } from '@/users/graphql/queries/getCurrentUser';
@ -19,6 +18,8 @@ import { useApolloMetadataClient } from './useApolloMetadataClient';
export const useUpdateOneFieldMetadataItem = () => {
const apolloMetadataClient = useApolloMetadataClient();
const apolloClient = useApolloClient();
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const setCurrentWorkspace = useSetRecoilState(currentWorkspaceState);
@ -67,10 +68,10 @@ export const useUpdateOneFieldMetadataItem = () => {
idToUpdate: fieldMetadataIdToUpdate,
updatePayload: updatePayload,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
const { data } = await apolloClient.query({ query: GET_CURRENT_USER });
setCurrentWorkspace(data?.currentUser?.defaultWorkspace);

View File

@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';
import {
UpdateOneObjectInput,
@ -8,8 +7,8 @@ import {
} from '~/generated-metadata/graphql';
import { UPDATE_ONE_OBJECT_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
import { useApolloMetadataClient } from './useApolloMetadataClient';
// TODO: Slice the Apollo store synchronously in the update function instead of subscribing, so we can use update after read in the same function call
@ -23,6 +22,9 @@ export const useUpdateOneObjectMetadataItem = () => {
client: apolloClientMetadata ?? undefined,
});
const { refreshObjectMetadataItems } =
useRefreshObjectMetadataItems('network-only');
const updateOneObjectMetadataItem = async ({
idToUpdate,
updatePayload,
@ -30,14 +32,16 @@ export const useUpdateOneObjectMetadataItem = () => {
idToUpdate: UpdateOneObjectInput['id'];
updatePayload: UpdateOneObjectInput['update'];
}) => {
return await mutate({
const result = await mutate({
variables: {
idToUpdate,
updatePayload,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});
await refreshObjectMetadataItems();
return result;
};
return {