Fix refetch cached views after field creation (#13120)
This PR fixes a bug that happened when adding a field to a table right after a field creation in the data model settings. This happened because the backend adds the newly created field to all relevant views without telling the frontend (because we don't have web sockets yet), so the frontend ended up in a stale state, and when the user added the field manually to a view the backend rightly threw an error telling that the view field it already existed. So the fix is just to refetch all views after a field creation, sparing us the difficult work of manually updating the Apollo cache. Fixes https://github.com/twentyhq/twenty/issues/13071
This commit is contained in:
@ -9,6 +9,7 @@ import {
|
|||||||
import { CREATE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
|
import { CREATE_ONE_FIELD_METADATA_ITEM } from '../graphql/mutations';
|
||||||
|
|
||||||
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
|
import { useRefreshObjectMetadataItems } from '@/object-metadata/hooks/useRefreshObjectMetadataItem';
|
||||||
|
import { useRefreshCachedViews } from '@/views/hooks/useRefreshViews';
|
||||||
|
|
||||||
export const useCreateOneFieldMetadataItem = () => {
|
export const useCreateOneFieldMetadataItem = () => {
|
||||||
const { refreshObjectMetadataItems } =
|
const { refreshObjectMetadataItems } =
|
||||||
@ -19,6 +20,8 @@ export const useCreateOneFieldMetadataItem = () => {
|
|||||||
CreateOneFieldMetadataItemMutationVariables
|
CreateOneFieldMetadataItemMutationVariables
|
||||||
>(CREATE_ONE_FIELD_METADATA_ITEM);
|
>(CREATE_ONE_FIELD_METADATA_ITEM);
|
||||||
|
|
||||||
|
const { refreshCachedViews } = useRefreshCachedViews();
|
||||||
|
|
||||||
const createOneFieldMetadataItem = async (input: CreateFieldInput) => {
|
const createOneFieldMetadataItem = async (input: CreateFieldInput) => {
|
||||||
const result = await mutate({
|
const result = await mutate({
|
||||||
variables: {
|
variables: {
|
||||||
@ -30,6 +33,8 @@ export const useCreateOneFieldMetadataItem = () => {
|
|||||||
|
|
||||||
await refreshObjectMetadataItems();
|
await refreshObjectMetadataItems();
|
||||||
|
|
||||||
|
await refreshCachedViews();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,6 @@ import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|||||||
import { SubMenuTopBarContainer } from '@/ui/layout/page/components/SubMenuTopBarContainer';
|
import { SubMenuTopBarContainer } from '@/ui/layout/page/components/SubMenuTopBarContainer';
|
||||||
import { View } from '@/views/types/View';
|
import { View } from '@/views/types/View';
|
||||||
import { ViewType } from '@/views/types/ViewType';
|
import { ViewType } from '@/views/types/ViewType';
|
||||||
import { useApolloClient } from '@apollo/client';
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { useLingui } from '@lingui/react/macro';
|
import { useLingui } from '@lingui/react/macro';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
@ -57,7 +56,6 @@ export const SettingsObjectNewFieldConfigure = () => {
|
|||||||
const activeObjectMetadataItem =
|
const activeObjectMetadataItem =
|
||||||
findActiveObjectMetadataItemByNamePlural(objectNamePlural);
|
findActiveObjectMetadataItemByNamePlural(objectNamePlural);
|
||||||
const { createMetadataField } = useFieldMetadataItem();
|
const { createMetadataField } = useFieldMetadataItem();
|
||||||
const apolloClient = useApolloClient();
|
|
||||||
|
|
||||||
const formConfig = useForm<SettingsDataModelNewFieldFormValues>({
|
const formConfig = useForm<SettingsDataModelNewFieldFormValues>({
|
||||||
mode: 'onTouched',
|
mode: 'onTouched',
|
||||||
|
|||||||
Reference in New Issue
Block a user