[FE] Update remote table schema + refactor Tables list (#5548)
Closes #5062. Refactoring tables list to avoid rendering all toggles on each sync or schema update while using fresh data: - introducing id for RemoteTables in apollo cache - manually updating the cache for the record that was updated after a sync or schema update instead of fetching all tables again
This commit is contained in:
@ -0,0 +1,53 @@
|
||||
import { useCallback } from 'react';
|
||||
import { ApolloClient, useMutation } from '@apollo/client';
|
||||
|
||||
import { SYNC_REMOTE_TABLE_SCHEMA_CHANGES } from '@/databases/graphql/mutations/syncRemoteTableSchemaChanges';
|
||||
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRemoteTableFromCache';
|
||||
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
|
||||
import {
|
||||
RemoteTableInput,
|
||||
SyncRemoteTableSchemaChangesMutation,
|
||||
SyncRemoteTableSchemaChangesMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
export const useSyncRemoteTableSchemaChanges = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const [mutate, mutationInformation] = useMutation<
|
||||
SyncRemoteTableSchemaChangesMutation,
|
||||
SyncRemoteTableSchemaChangesMutationVariables
|
||||
>(SYNC_REMOTE_TABLE_SCHEMA_CHANGES, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const syncRemoteTableSchemaChanges = useCallback(
|
||||
async (input: RemoteTableInput) => {
|
||||
const remoteTable = await mutate({
|
||||
variables: {
|
||||
input,
|
||||
},
|
||||
update: (cache, { data }) => {
|
||||
if (isDefined(data)) {
|
||||
modifyRemoteTableFromCache({
|
||||
cache: cache,
|
||||
remoteTableName: input.name,
|
||||
fieldModifiers: {
|
||||
schemaPendingUpdates: () =>
|
||||
data.syncRemoteTableSchemaChanges.schemaPendingUpdates || [],
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return remoteTable;
|
||||
},
|
||||
[mutate],
|
||||
);
|
||||
|
||||
return {
|
||||
syncRemoteTableSchemaChanges,
|
||||
isLoading: mutationInformation.loading,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user