Skip to content

Commit

Permalink
Remove refetch
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Oct 21, 2024
1 parent 92cc59e commit e8b8620
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from 'react';
import { useRecoilCallback, useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue } from 'recoil';

import { useIsLogged } from '@/auth/hooks/useIsLogged';
import { currentUserState } from '@/auth/states/currentUserState';
Expand All @@ -21,31 +21,30 @@ export const ObjectMetadataItemsLoadEffect = () => {
skip: !isLoggedIn,
});

const updateObjectMetadataItems = useRecoilCallback(
({ set, snapshot }) =>
() => {
const toSetObjectMetadataItems =
isUndefinedOrNull(currentUser) ||
currentWorkspace?.activationStatus !==
WorkspaceActivationStatus.Active
? generatedMockObjectMetadataItems
: newObjectMetadataItems;

if (
!isDeeplyEqual(
snapshot.getLoadable(objectMetadataItemsState).getValue(),
toSetObjectMetadataItems,
)
) {
set(objectMetadataItemsState, toSetObjectMetadataItems);
}
},
[currentUser, currentWorkspace?.activationStatus, newObjectMetadataItems],
console.log('a', newObjectMetadataItems);

const [objectMetadataItems, setObjectMetadataItems] = useRecoilState(
objectMetadataItemsState,
);

useEffect(() => {
updateObjectMetadataItems();
}, [updateObjectMetadataItems]);
const toSetObjectMetadataItems =
isUndefinedOrNull(currentUser) ||
currentWorkspace?.activationStatus !== WorkspaceActivationStatus.Active
? generatedMockObjectMetadataItems
: newObjectMetadataItems;

console.log(toSetObjectMetadataItems);
if (!isDeeplyEqual(objectMetadataItems, toSetObjectMetadataItems)) {
setObjectMetadataItems(toSetObjectMetadataItems);
}
}, [
currentUser,
currentWorkspace?.activationStatus,
newObjectMetadataItems,
objectMetadataItems,
setObjectMetadataItems,
]);

return <></>;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApolloClient, useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';

import {
CreateFieldInput,
Expand All @@ -8,7 +7,6 @@ import {
} from '~/generated-metadata/graphql';

import { CREATE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';

import { v4 } from 'uuid';
import { useApolloMetadataClient } from './useApolloMetadataClient';
Expand All @@ -30,8 +28,6 @@ export const useCreateOneFieldMetadataItem = () => {
field: input,
},
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
optimisticResponse: {
createOneField: {
...input,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApolloClient, 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';
Expand All @@ -10,7 +9,6 @@ import {
} from '~/generated-metadata/graphql';

import { CREATE_ONE_OBJECT_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';

import { v4 } from 'uuid';
import { useApolloMetadataClient } from './useApolloMetadataClient';
Expand All @@ -35,18 +33,18 @@ export const useCreateOneObjectMetadataItem = () => {
variables: {
input: { object: input },
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
optimisticResponse: {
createOneObject: {
...input,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
labelIdentifierFieldMetadataId: '',
imageIdentifierFieldMetadataId: '',
__typename: 'object',
id: v4(),
dataSourceId: '',
isCustom: false,
isActive: false,
updatedAt: undefined,
isActive: true,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ApolloClient, useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';

import {
DeleteOneFieldMetadataItemMutation,
DeleteOneFieldMetadataItemMutationVariables,
} from '~/generated-metadata/graphql';

import { DELETE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';

import { useApolloMetadataClient } from './useApolloMetadataClient';

Expand All @@ -29,7 +27,6 @@ export const useDeleteOneFieldMetadataItem = () => {
idToDelete,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
optimisticResponse: {
deleteOneField: {
id: idToDelete,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ApolloClient, useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';

import {
DeleteOneObjectMetadataItemMutation,
DeleteOneObjectMetadataItemMutationVariables,
} from '~/generated-metadata/graphql';

import { DELETE_ONE_OBJECT_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';

import { useApolloMetadataClient } from './useApolloMetadataClient';

Expand All @@ -28,8 +26,6 @@ export const useDeleteOneObjectMetadataItem = () => {
variables: {
idToDelete,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
optimisticResponse: {
deleteOneObject: {
id: idToDelete,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';

import {
FieldMetadataType,
Expand All @@ -8,7 +7,6 @@ import {
} from '~/generated-metadata/graphql';

import { UPDATE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';

import { useApolloMetadataClient } from './useApolloMetadataClient';

Expand Down Expand Up @@ -46,8 +44,6 @@ export const useUpdateOneFieldMetadataItem = () => {
label: updatePayload.label ?? undefined,
},
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
optimisticResponse: {
updateOneField: {
id: fieldMetadataIdToUpdate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';

import {
UpdateOneObjectInput,
Expand All @@ -8,7 +7,6 @@ import {
} from '~/generated-metadata/graphql';

import { UPDATE_ONE_OBJECT_METADATA_ITEM } from '../graphql/mutations';
import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';

import { useApolloMetadataClient } from './useApolloMetadataClient';

Expand All @@ -35,8 +33,6 @@ export const useUpdateOneObjectMetadataItem = () => {
idToUpdate,
updatePayload,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
optimisticResponse: {
updateOneObject: {
id: idToUpdate,
Expand Down

0 comments on commit e8b8620

Please sign in to comment.