fix: fix Relation field optimistic effect on Record update (#3352)
* fix: fix Relation field optimistic effect on Record update Related to #3099 * Fix lint * Fix * fix --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
import { useApolloClient } from '@apollo/client';
|
||||
import { Reference, useApolloClient } from '@apollo/client';
|
||||
|
||||
import { useOptimisticEffect } from '@/apollo/optimistic-effect/hooks/useOptimisticEffect';
|
||||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
|
||||
import { isRecordMatchingFilter } from '@/object-record/record-filter/utils/isRecordMatchingFilter';
|
||||
import { sanitizeRecordInput } from '@/object-record/utils/sanitizeRecordInput';
|
||||
import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName';
|
||||
import { capitalize } from '~/utils/string/capitalize';
|
||||
|
||||
type useUpdateOneRecordProps = {
|
||||
@ -59,6 +61,54 @@ export const useUpdateOneRecord = <T>({
|
||||
[`update${capitalize(objectMetadataItem.nameSingular)}`]:
|
||||
optimisticallyUpdatedRecord,
|
||||
},
|
||||
update: (cache, { data }) => {
|
||||
const response =
|
||||
data?.[`update${capitalize(objectMetadataItem.nameSingular)}`];
|
||||
|
||||
if (!response) return;
|
||||
|
||||
cache.modify<Record<string, Reference>>({
|
||||
fields: {
|
||||
[objectMetadataItem.namePlural]: (
|
||||
existingConnectionRef,
|
||||
{ readField, storeFieldName },
|
||||
) => {
|
||||
if (
|
||||
readField('__typename', existingConnectionRef) !==
|
||||
`${capitalize(objectMetadataItem.nameSingular)}Connection`
|
||||
)
|
||||
return existingConnectionRef;
|
||||
|
||||
const { variables } = parseApolloStoreFieldName(storeFieldName);
|
||||
|
||||
const edges = readField<{ node: Reference }[]>(
|
||||
'edges',
|
||||
existingConnectionRef,
|
||||
);
|
||||
|
||||
if (
|
||||
variables?.filter &&
|
||||
!isRecordMatchingFilter({
|
||||
record: response,
|
||||
filter: variables.filter,
|
||||
objectMetadataItem,
|
||||
}) &&
|
||||
edges?.length
|
||||
) {
|
||||
return {
|
||||
...existingConnectionRef,
|
||||
edges: edges.filter(
|
||||
(edge) =>
|
||||
readField('id', readField('node', edge)) !== response.id,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
return existingConnectionRef;
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
if (!updatedRecord?.data) {
|
||||
|
||||
Reference in New Issue
Block a user