From 23f0581ad5e867ad88ee092191f5ec1406b3cf40 Mon Sep 17 00:00:00 2001 From: Kevin Foong <55353265+kevin9foong@users.noreply.github.com> Date: Tue, 24 Dec 2024 10:45:40 +0800 Subject: [PATCH] feat: add accept deny modal and created field ids from mfb --- .../assistance/AssistanceService.ts | 2 +- .../admin-form/assistance/mutations.ts | 7 +- .../BuilderAndDesignContent/BuilderFields.tsx | 9 + .../FieldRow/FieldRowContainer.tsx | 31 +++- .../MagicFormBuilderContainer.tsx | 168 +++++++++++++----- .../useMagicFormBuilderStore.tsx | 22 +++ .../admin-form.assistance.controller.ts | 5 +- .../admin-form.assistance.service.ts | 10 +- 8 files changed, 200 insertions(+), 54 deletions(-) create mode 100644 frontend/src/features/admin-form/create/builder-and-design/useMagicFormBuilderStore.tsx diff --git a/frontend/src/features/admin-form/assistance/AssistanceService.ts b/frontend/src/features/admin-form/assistance/AssistanceService.ts index de49afca22..68b1b24abd 100644 --- a/frontend/src/features/admin-form/assistance/AssistanceService.ts +++ b/frontend/src/features/admin-form/assistance/AssistanceService.ts @@ -9,7 +9,7 @@ export const makeTextPrompt = ({ formId: string prompt: string }) => { - return ApiService.post( + return ApiService.post<{ message: string; createdFieldIds?: string[] }>( `${ADMIN_FORM_ENDPOINT}/${formId}/assistance/text-prompt`, { prompt }, ).then(({ data }) => data) diff --git a/frontend/src/features/admin-form/assistance/mutations.ts b/frontend/src/features/admin-form/assistance/mutations.ts index d5c87cfd2b..823cff5048 100644 --- a/frontend/src/features/admin-form/assistance/mutations.ts +++ b/frontend/src/features/admin-form/assistance/mutations.ts @@ -5,6 +5,7 @@ import { makeTextPrompt } from '~features/admin-form/assistance/AssistanceServic import { useToast } from '../../../hooks/useToast' import { adminFormKeys } from '../common/queries' +import { useMagicFormBuilderStore } from '../create/builder-and-design/useMagicFormBuilderStore' export const useAssistanceMutations = () => { const { formId } = useParams() @@ -19,7 +20,11 @@ export const useAssistanceMutations = () => { const useMakeTextPromptMutation = useMutation( (prompt: string) => makeTextPrompt({ formId, prompt }), { - onSuccess: () => { + onSuccess: (data) => { + const { createdFieldIds } = data + useMagicFormBuilderStore.setState({ + recentlyCreatedFieldIds: new Set(createdFieldIds), + }) queryClient.invalidateQueries(adminFormKeys.id(formId)) toast.closeAll() toast({ description: 'Form generated successfully', status: 'success' }) diff --git a/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/BuilderFields.tsx b/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/BuilderFields.tsx index 5c87f5cf8c..e2c0eeddd8 100644 --- a/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/BuilderFields.tsx +++ b/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/BuilderFields.tsx @@ -11,6 +11,10 @@ import { stateDataSelector, useFieldBuilderStore, } from '../useFieldBuilderStore' +import { + recentlyCreatedFieldIdsSelector, + useMagicFormBuilderStore, +} from '../useMagicFormBuilderStore' import { useDesignColorTheme } from '../utils/useDesignColorTheme' import FieldRow from './FieldRow' @@ -44,6 +48,10 @@ export const BuilderFields = ({ const isDirty = useDirtyFieldStore(isDirtySelector) + const mfbRecentlyCreatedFieldIds = useMagicFormBuilderStore( + recentlyCreatedFieldIdsSelector, + ) + return ( <> {fieldsWithQuestionNos.map((f, i) => { @@ -64,6 +72,7 @@ export const BuilderFields = ({ handleBuilderClick={handleBuilderClick} isDirty={isDirty} colorTheme={colorTheme} + isHighlighted={mfbRecentlyCreatedFieldIds.has(f._id)} {...activeFieldExtraProps} /> ) diff --git a/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/FieldRow/FieldRowContainer.tsx b/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/FieldRow/FieldRowContainer.tsx index 55636375cc..3de64c686f 100644 --- a/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/FieldRow/FieldRowContainer.tsx +++ b/frontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignContent/FieldRow/FieldRowContainer.tsx @@ -5,6 +5,7 @@ import { BiCog, BiDuplicate, BiGridHorizontal, BiTrash } from 'react-icons/bi' import { useIsMutating } from 'react-query' import { Box, + BoxProps, ButtonGroup, chakra, Collapse, @@ -94,6 +95,30 @@ export interface FieldRowContainerProps { colorTheme?: FormColorTheme // handleBuilderClick is passed down to prevent unnecessary re-renders from useContext handleBuilderClick: CreatePageSidebarContextProps['handleBuilderClick'] + isHighlighted?: boolean +} + +/** + * Used for highlighting fields that were created by Magic Form Builder. + */ +const HighlightableBox = ({ + children, + isHighlighted, + ...props +}: { + children: React.ReactNode + isHighlighted?: boolean +} & BoxProps) => { + return ( + + {children} + + ) } const FieldRowContainer = ({ @@ -106,6 +131,7 @@ const FieldRowContainer = ({ isDirty, colorTheme, handleBuilderClick, + isHighlighted, }: FieldRowContainerProps): JSX.Element => { const isMobile = useIsMobile() const numFormFieldMutations = useIsMutating(adminFormKeys.base) @@ -285,7 +311,8 @@ const FieldRowContainer = ({ )} - - + {isActive && ( void + onSettled: () => void + onClose: () => void }) => { const { register, @@ -82,50 +82,130 @@ const MagicFormBuilderPopover = ({ const onSubmit = async ({ prompt }: TextPromptInputs) => { useMakeTextPromptMutation.mutate(prompt, { - onSettled: () => setIsOpen(false), + onSettled: onSettled, }) } return ( - +
+ + Generate form + + + + +