Fixed bug for refectch activities and create activity on the currently filtered user. (#1493)
* Fixed bug for refectch activities and create activity on the currently filtered user. * Refactor optimistif effect --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,39 +1,111 @@
|
||||
import { useApolloClient } from '@apollo/client';
|
||||
import {
|
||||
ApolloCache,
|
||||
DocumentNode,
|
||||
OperationVariables,
|
||||
useApolloClient,
|
||||
} from '@apollo/client';
|
||||
import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { GET_COMPANIES } from '@/companies/graphql/queries/getCompanies';
|
||||
import { GET_PEOPLE } from '@/people/graphql/queries/getPeople';
|
||||
import { GetCompaniesQuery, GetPeopleQuery } from '~/generated/graphql';
|
||||
|
||||
import { optimisticEffectState } from '../states/optimisticEffectState';
|
||||
import { OptimisticEffect } from '../types/OptimisticEffect';
|
||||
import { OptimisticEffectDefinition } from '../types/OptimisticEffectDefinition';
|
||||
|
||||
export function useOptimisticEffect() {
|
||||
const apolloClient = useApolloClient();
|
||||
|
||||
const registerOptimisticEffect = useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
(optimisticEffect: OptimisticEffect<unknown, unknown>) => {
|
||||
const { key } = optimisticEffect;
|
||||
({
|
||||
variables,
|
||||
definition,
|
||||
}: {
|
||||
variables: OperationVariables;
|
||||
definition: OptimisticEffectDefinition<unknown>;
|
||||
}) => {
|
||||
const optimisticEffects = snapshot
|
||||
.getLoadable(optimisticEffectState)
|
||||
.getValue();
|
||||
|
||||
function optimisticEffectWriter({
|
||||
cache,
|
||||
newData,
|
||||
query,
|
||||
variables,
|
||||
}: {
|
||||
cache: ApolloCache<unknown>;
|
||||
newData: unknown[];
|
||||
variables: OperationVariables;
|
||||
query: DocumentNode;
|
||||
}) {
|
||||
const existingData = cache.readQuery({
|
||||
query,
|
||||
variables,
|
||||
});
|
||||
|
||||
if (!existingData) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (query === GET_PEOPLE) {
|
||||
cache.writeQuery({
|
||||
query,
|
||||
variables,
|
||||
data: {
|
||||
people: definition.resolver({
|
||||
currentData: (existingData as GetPeopleQuery).people,
|
||||
newData,
|
||||
variables,
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (query === GET_COMPANIES) {
|
||||
cache.writeQuery({
|
||||
query,
|
||||
variables,
|
||||
data: {
|
||||
companies: definition.resolver({
|
||||
currentData: (existingData as GetCompaniesQuery).companies,
|
||||
newData,
|
||||
variables,
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const optimisticEffect = {
|
||||
key: definition.key,
|
||||
variables,
|
||||
typename: definition.typename,
|
||||
query: definition.query,
|
||||
writer: optimisticEffectWriter,
|
||||
};
|
||||
|
||||
set(optimisticEffectState, {
|
||||
...optimisticEffects,
|
||||
[key]: optimisticEffect,
|
||||
[definition.key]: optimisticEffect,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
const triggerOptimisticEffects = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
(typename: string, entities: any[]) => {
|
||||
(typename: string, newData: any[]) => {
|
||||
const optimisticEffects = snapshot
|
||||
.getLoadable(optimisticEffectState)
|
||||
.getValue();
|
||||
|
||||
Object.values(optimisticEffects).forEach((optimisticEffect) => {
|
||||
if (optimisticEffect.typename === typename) {
|
||||
optimisticEffect.resolver({
|
||||
optimisticEffect.writer({
|
||||
cache: apolloClient.cache,
|
||||
entities,
|
||||
query: optimisticEffect.query,
|
||||
newData,
|
||||
variables: optimisticEffect.variables,
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
import { OptimisticEffect } from '../types/OptimisticEffect';
|
||||
import { OptimisticEffect } from '../types/internal/OptimisticEffect';
|
||||
|
||||
export const optimisticEffectState = atom<
|
||||
Record<string, OptimisticEffect<unknown, unknown>>
|
||||
Record<string, OptimisticEffect<unknown>>
|
||||
>({
|
||||
key: 'optimisticEffectState',
|
||||
default: {},
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
import { ApolloCache } from '@apollo/client';
|
||||
|
||||
type OptimisticEffectResolver<T, QueryVariables> = ({
|
||||
cache,
|
||||
entities,
|
||||
variables,
|
||||
}: {
|
||||
cache: ApolloCache<T>;
|
||||
entities: T[];
|
||||
variables: QueryVariables;
|
||||
}) => void;
|
||||
|
||||
export type OptimisticEffect<T, QueryVariables> = {
|
||||
key: string;
|
||||
typename: string;
|
||||
variables: QueryVariables;
|
||||
resolver: OptimisticEffectResolver<T, QueryVariables>;
|
||||
};
|
||||
@ -0,0 +1,10 @@
|
||||
import { DocumentNode } from 'graphql';
|
||||
|
||||
import { OptimisticEffectResolver } from './OptimisticEffectResolver';
|
||||
|
||||
export type OptimisticEffectDefinition<T> = {
|
||||
key: string;
|
||||
query: DocumentNode;
|
||||
typename: string;
|
||||
resolver: OptimisticEffectResolver<T>;
|
||||
};
|
||||
@ -0,0 +1,11 @@
|
||||
import { OperationVariables } from '@apollo/client';
|
||||
|
||||
export type OptimisticEffectResolver<T> = ({
|
||||
currentData,
|
||||
newData,
|
||||
variables,
|
||||
}: {
|
||||
currentData: T[];
|
||||
newData: T[];
|
||||
variables: OperationVariables;
|
||||
}) => void;
|
||||
@ -0,0 +1,21 @@
|
||||
import { ApolloCache, DocumentNode, OperationVariables } from '@apollo/client';
|
||||
|
||||
type OptimisticEffectWriter<T> = ({
|
||||
cache,
|
||||
newData,
|
||||
variables,
|
||||
query,
|
||||
}: {
|
||||
cache: ApolloCache<T>;
|
||||
query: DocumentNode;
|
||||
newData: T[];
|
||||
variables: OperationVariables;
|
||||
}) => void;
|
||||
|
||||
export type OptimisticEffect<T> = {
|
||||
key: string;
|
||||
query: DocumentNode;
|
||||
typename: string;
|
||||
variables: OperationVariables;
|
||||
writer: OptimisticEffectWriter<T>;
|
||||
};
|
||||
Reference in New Issue
Block a user