The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Open the form settings from an Edit button on the record creation form (#27550)
Thomas Trompette380b5d658e2f
diff --git a/packages/twenty-front/src/modules/side-panel/hooks/useOpenRecordCreationFormSettingsInSidePanel.ts b/packages/twenty-front/src/modules/side-panel/hooks/useOpenRecordCreationFormSettingsInSidePanel.tsnew file mode 100644index 00000000000..b8d4eddd286--- /dev/null+++ b/packages/twenty-front/src/modules/side-panel/hooks/useOpenRecordCreationFormSettingsInSidePanel.ts@@ -0,0 +1,43 @@+import { type EnrichedObjectMetadataItem } from '@/object-metadata/types/EnrichedObjectMetadataItem';+import { useSidePanelMenu } from '@/side-panel/hooks/useSidePanelMenu';+import { recordCreationFormSettingsObjectMetadataIdComponentState } from '@/side-panel/pages/record-creation-form-settings/states/recordCreationFormSettingsObjectMetadataIdComponentState';+import { useLingui } from '@lingui/react/macro';+import { useStore } from 'jotai';+import { useCallback } from 'react';+import { SidePanelPages } from 'twenty-shared/types';+import { IconPlus } from 'twenty-ui/icon';+import { v4 } from 'uuid';++export const useOpenRecordCreationFormSettingsInSidePanel = () => {+ const { t } = useLingui();+ const store = useStore();+ const { navigateSidePanelMenu } = useSidePanelMenu();++ const openRecordCreationFormSettingsInSidePanel = useCallback(+ (+ objectMetadataItem: Pick<+ EnrichedObjectMetadataItem,+ 'id' | 'labelSingular'+ >,+ ) => {+ const pageId = v4();++ store.set(+ recordCreationFormSettingsObjectMetadataIdComponentState.atomFamily({+ instanceId: pageId,+ }),+ objectMetadataItem.id,+ );++ navigateSidePanelMenu({+ page: SidePanelPages.RecordCreationFormSettings,+ pageTitle: t`Create ${objectMetadataItem.labelSingular}`,+ pageIcon: IconPlus,+ pageId,+ });+ },+ [navigateSidePanelMenu, store, t],+ );++ return { openRecordCreationFormSettingsInSidePanel };+};diff --git a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsxindex c882ad7bfb3..6e57ce25027 100644--- a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx+++ b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx@@ -14,6 +14,8 @@ import { useRecordCreationFormSettle } from '@/object-record/record-form/hooks/uimport { useRecordFormFields } from '@/object-record/record-form/hooks/useRecordFormFields';import { computeRecordFormCreateRecordInput } from '@/object-record/record-form/utils/computeRecordFormCreateRecordInput';import { type ObjectRecord } from '@/object-record/types/ObjectRecord';+import { useHasPermissionFlag } from '@/settings/roles/hooks/useHasPermissionFlag';+import { useOpenRecordCreationFormSettingsInSidePanel } from '@/side-panel/hooks/useOpenRecordCreationFormSettingsInSidePanel';import { recordCreationFormAreHiddenFieldsShownComponentState } from '@/side-panel/pages/record-creation-form/states/recordCreationFormAreHiddenFieldsShownComponentState';import { recordCreationFormDraftComponentState } from '@/side-panel/pages/record-creation-form/states/recordCreationFormDraftComponentState';import { recordCreationFormRequestComponentState } from '@/side-panel/pages/record-creation-form/states/recordCreationFormRequestComponentState';@@ -30,6 +32,7 @@ import { styled } from '@linaria/react';import { t } from '@lingui/core/macro';import { Key } from 'ts-key-enum';import { type JsonValue } from 'type-fest';+import { PermissionFlagType } from 'twenty-shared/constants';import { isDefined } from 'twenty-shared/utils';import { LightButton } from 'twenty-ui/components/input';import { IconChevronDown, IconChevronUp, IconPlus } from 'twenty-ui/icon';@@ -130,6 +133,11 @@ const SidePanelRecordCreationForm = ({const draftRecord = recordCreationFormDraft ?? initialDraftRecord;+ const hasLayoutsPermission = useHasPermissionFlag(PermissionFlagType.LAYOUTS);++ const { openRecordCreationFormSettingsInSidePanel } =+ useOpenRecordCreationFormSettingsInSidePanel();+const { recordFormFields } = useRecordFormFields({ objectMetadataItem });const objectPermissions = useObjectPermissionsForObject(objectMetadataItem.id,@@ -311,6 +319,19 @@ const SidePanelRecordCreationForm = ({)}<SidePanelFooteractions={[+ ...(hasLayoutsPermission+ ? [+ <Button+ key="edit-form"+ size="sm"+ onClick={() =>+ openRecordCreationFormSettingsInSidePanel(+ objectMetadataItem,+ )+ }+ >{t`Edit`}</Button>,+ ]+ : []),<Buttonkey="create-record"startIcon={<IconPlus />}diff --git a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsxindex d9e45ca7f40..c9aa38d2761 100644--- a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx+++ b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx@@ -94,6 +94,8 @@ const buildValidationRuleViolationError = (fieldMetadataId: string) =>],});let mockValidationRules: ValidationRule[] = [];+const openRecordCreationFormSettingsInSidePanel = jest.fn();+const mockHasPermissionFlag = jest.fn();jest.mock('@/object-metadata/hooks/useObjectMetadataItemById', () => ({useObjectMetadataItemById: () => ({ objectMetadataItem: COMPANY_OBJECT }),@@ -123,6 +125,19 @@ jest.mock('@/validation-rules/hooks/useValidationRules', () => ({useValidationRules: () => ({ validationRules: mockValidationRules }),}));+jest.mock('@/settings/roles/hooks/useHasPermissionFlag', () => ({+ useHasPermissionFlag: () => mockHasPermissionFlag(),+}));++jest.mock(+ '@/side-panel/hooks/useOpenRecordCreationFormSettingsInSidePanel',+ () => ({+ useOpenRecordCreationFormSettingsInSidePanel: () => ({+ openRecordCreationFormSettingsInSidePanel,+ }),+ }),+);+jest.mock('@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement', () => ({useHotkeysOnFocusedElement: () => undefined,}));@@ -243,6 +258,7 @@ describe('SidePanelRecordCreationFormPage', () => {resetJotaiStore();mockValidationRules = [];settleRecordCreationDraft.mockResolvedValue({});+ mockHasPermissionFlag.mockReturnValue(true);seedRecordFormPageLayout();jotaiStore.set(recordCreationFormRequestComponentState.atomFamily({@@ -391,4 +407,26 @@ describe('SidePanelRecordCreationFormPage', () => {});expect(screen.queryByLabelText('Nickname')).not.toBeInTheDocument();});++ it('opens the form configuration for members allowed to edit layouts', async () => {+ const user = userEvent.setup();++ renderPage();++ await user.click(screen.getByRole('button', { name: 'Edit' }));++ expect(openRecordCreationFormSettingsInSidePanel).toHaveBeenCalledWith(+ COMPANY_OBJECT,+ );+ });++ it('hides the edit button from members without the layouts permission', () => {+ mockHasPermissionFlag.mockReturnValue(false);++ renderPage();++ expect(+ screen.queryByRole('button', { name: 'Edit' }),+ ).not.toBeInTheDocument();+ });});