From f4779a02ca33a70c03e108926811b96a4f25635d Mon Sep 17 00:00:00 2001 From: Antoine Moreaux Date: Tue, 21 Jan 2025 17:17:58 +0100 Subject: [PATCH] fix(workspace): ensure proper handling of updates and errors (#9752) Added `await` to `updateWorkspaceById` in resolver for proper async handling. Enhanced workspace settings UI with specific error handling for subdomain conflicts and improved feedback for invalid form values. Fix https://github.com/twentyhq/twenty/issues/9709#issuecomment-2597919251 --- .../settings/workspace/SettingsDomain.tsx | 68 +++++++++---------- .../workspace/workspace.resolver.ts | 2 +- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/packages/twenty-front/src/pages/settings/workspace/SettingsDomain.tsx b/packages/twenty-front/src/pages/settings/workspace/SettingsDomain.tsx index e705debfb..b10836580 100644 --- a/packages/twenty-front/src/pages/settings/workspace/SettingsDomain.tsx +++ b/packages/twenty-front/src/pages/settings/workspace/SettingsDomain.tsx @@ -1,3 +1,4 @@ +import { ApolloError } from '@apollo/client'; import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState'; import { useRedirectToWorkspaceDomain } from '@/domain-manager/hooks/useRedirectToWorkspaceDomain'; import { domainConfigurationState } from '@/domain-manager/states/domainConfigurationState'; @@ -80,44 +81,43 @@ export const SettingsDomain = () => { const subdomainValue = watch('subdomain'); const handleSave = async () => { - try { - const values = getValues(); + const values = getValues(); - if (!values || !isValid || !currentWorkspace) { - throw new Error(t`Invalid form values`); - } - - await updateWorkspace({ - variables: { - input: { - subdomain: values.subdomain, - }, - }, - }); - - setCurrentWorkspace({ - ...currentWorkspace, - subdomain: values.subdomain, - }); - - redirectToWorkspaceDomain(values.subdomain); - } catch (error) { - if ( - error instanceof Error && - (error.message === t`Subdomain already taken` || - error.message.endsWith(t`not allowed`)) - ) { - control.setError('subdomain', { - type: 'manual', - message: (error as Error).message, - }); - return; - } - - enqueueSnackBar((error as Error).message, { + if (!values || !isValid || !currentWorkspace) { + return enqueueSnackBar(t`Invalid form values`, { variant: SnackBarVariant.Error, }); } + + await updateWorkspace({ + variables: { + input: { + subdomain: values.subdomain, + }, + }, + onError: (error) => { + if ( + error instanceof ApolloError && + error.graphQLErrors[0]?.extensions?.code === 'CONFLICT' + ) { + return control.setError('subdomain', { + type: 'manual', + message: t`Subdomain already taken`, + }); + } + enqueueSnackBar((error as Error).message, { + variant: SnackBarVariant.Error, + }); + }, + onCompleted: () => { + setCurrentWorkspace({ + ...currentWorkspace, + subdomain: values.subdomain, + }); + + redirectToWorkspaceDomain(values.subdomain); + }, + }); }; return ( diff --git a/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.ts b/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.ts index bcfb44a54..db28abe76 100644 --- a/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.ts +++ b/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.ts @@ -103,7 +103,7 @@ export class WorkspaceResolver { @AuthWorkspace() workspace: Workspace, ) { try { - return this.workspaceService.updateWorkspaceById({ + return await this.workspaceService.updateWorkspaceById({ ...data, id: workspace.id, });