Introduce useOptimisticEvict (#1629)
This commit is contained in:
@ -18,12 +18,12 @@ export const useOptimisticEffect = () => {
|
|||||||
|
|
||||||
const registerOptimisticEffect = useRecoilCallback(
|
const registerOptimisticEffect = useRecoilCallback(
|
||||||
({ snapshot, set }) =>
|
({ snapshot, set }) =>
|
||||||
({
|
<T>({
|
||||||
variables,
|
variables,
|
||||||
definition,
|
definition,
|
||||||
}: {
|
}: {
|
||||||
variables: OperationVariables;
|
variables: OperationVariables;
|
||||||
definition: OptimisticEffectDefinition<unknown>;
|
definition: OptimisticEffectDefinition<T>;
|
||||||
}) => {
|
}) => {
|
||||||
const optimisticEffects = snapshot
|
const optimisticEffects = snapshot
|
||||||
.getLoadable(optimisticEffectState)
|
.getLoadable(optimisticEffectState)
|
||||||
@ -55,8 +55,8 @@ export const useOptimisticEffect = () => {
|
|||||||
variables,
|
variables,
|
||||||
data: {
|
data: {
|
||||||
people: definition.resolver({
|
people: definition.resolver({
|
||||||
currentData: (existingData as GetPeopleQuery).people,
|
currentData: (existingData as GetPeopleQuery).people as T[],
|
||||||
newData,
|
newData: newData as T[],
|
||||||
variables,
|
variables,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@ -69,8 +69,9 @@ export const useOptimisticEffect = () => {
|
|||||||
variables,
|
variables,
|
||||||
data: {
|
data: {
|
||||||
companies: definition.resolver({
|
companies: definition.resolver({
|
||||||
currentData: (existingData as GetCompaniesQuery).companies,
|
currentData: (existingData as GetCompaniesQuery)
|
||||||
newData,
|
.companies as T[],
|
||||||
|
newData: newData as T[],
|
||||||
variables,
|
variables,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -0,0 +1,24 @@
|
|||||||
|
import { useApolloClient } from '@apollo/client';
|
||||||
|
|
||||||
|
export const useOptimisticEvict = () => {
|
||||||
|
const cache = useApolloClient().cache;
|
||||||
|
|
||||||
|
const performOptimisticEvict = (
|
||||||
|
typename: string,
|
||||||
|
fieldName: string,
|
||||||
|
fieldValue: string,
|
||||||
|
) => {
|
||||||
|
const serializedCache = cache.extract();
|
||||||
|
|
||||||
|
Object.values(serializedCache)
|
||||||
|
.filter((item) => item.__typename === typename)
|
||||||
|
.forEach((item) => {
|
||||||
|
if (item[fieldName] === fieldValue) {
|
||||||
|
cache.evict({ id: cache.identify(item) });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
performOptimisticEvict,
|
||||||
|
};
|
||||||
|
};
|
||||||
@ -1,6 +1,7 @@
|
|||||||
import { getOperationName } from '@apollo/client/utilities';
|
import { getOperationName } from '@apollo/client/utilities';
|
||||||
import { useRecoilState, useRecoilValue } from 'recoil';
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||||
|
|
||||||
|
import { useOptimisticEvict } from '@/apollo/optimistic-effect/hooks/useOptimisticEvict';
|
||||||
import { GET_PIPELINES } from '@/pipeline/graphql/queries/getPipelines';
|
import { GET_PIPELINES } from '@/pipeline/graphql/queries/getPipelines';
|
||||||
import { useResetTableRowSelection } from '@/ui/table/hooks/useResetTableRowSelection';
|
import { useResetTableRowSelection } from '@/ui/table/hooks/useResetTableRowSelection';
|
||||||
import { selectedRowIdsSelector } from '@/ui/table/states/selectors/selectedRowIdsSelector';
|
import { selectedRowIdsSelector } from '@/ui/table/states/selectors/selectedRowIdsSelector';
|
||||||
@ -15,6 +16,7 @@ export const useDeleteSelectedComapnies = () => {
|
|||||||
const [deleteCompanies] = useDeleteManyCompaniesMutation({
|
const [deleteCompanies] = useDeleteManyCompaniesMutation({
|
||||||
refetchQueries: [getOperationName(GET_PIPELINES) ?? ''],
|
refetchQueries: [getOperationName(GET_PIPELINES) ?? ''],
|
||||||
});
|
});
|
||||||
|
const { performOptimisticEvict } = useOptimisticEvict();
|
||||||
|
|
||||||
const [tableRowIds, setTableRowIds] = useRecoilState(tableRowIdsState);
|
const [tableRowIds, setTableRowIds] = useRecoilState(tableRowIdsState);
|
||||||
|
|
||||||
@ -42,6 +44,9 @@ export const useDeleteSelectedComapnies = () => {
|
|||||||
cache.evict({
|
cache.evict({
|
||||||
id: cache.identify({ __typename: 'Company', id: companyId }),
|
id: cache.identify({ __typename: 'Company', id: companyId }),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
performOptimisticEvict('PipelineProgress', 'companyId', companyId);
|
||||||
|
|
||||||
cache.gc();
|
cache.gc();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user