Skip to content

Commit

Permalink
feat: support field addition during revert
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin9foong committed Dec 24, 2024
1 parent 9af405a commit 6d8b9f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useAssistanceMutations } from '~features/admin-form/assistance/mutation

import { useDeleteFormField } from '../../mutations/useDeleteFormField'
import {
isAcceptDenyOpenSelector,
recentlyCreatedFieldIdsSelector,
useMagicFormBuilderStore,
} from '../../useMagicFormBuilderStore'
Expand Down Expand Up @@ -70,10 +71,8 @@ const MagicFormBuilderButton = ({
}

const MagicFormBuilderCreateFormPrompt = ({
onSettled,
onClose,
}: {
onSettled: () => void
onClose: () => void
}) => {
const {
Expand All @@ -85,9 +84,7 @@ const MagicFormBuilderCreateFormPrompt = ({
const { useMakeTextPromptMutation } = useAssistanceMutations()

const onSubmit = async ({ prompt }: TextPromptInputs) => {
useMakeTextPromptMutation.mutate(prompt, {
onSettled: onSettled,
})
useMakeTextPromptMutation.mutate(prompt)
}

return (
Expand Down Expand Up @@ -176,18 +173,18 @@ const MagicFormBuilderPopover = ({
isOpen: boolean
setIsOpen: (isOpen: boolean) => void
}) => {
const [isAcceptDenyOpen, setIsAcceptDenyOpen] = useState(false)
const clearRecentlyCreatedFieldIds = useMagicFormBuilderStore(
(state) => state.clearRecentlyCreatedFieldIds,
)
const recentlyCreatedFieldIds = useMagicFormBuilderStore(
recentlyCreatedFieldIdsSelector,
)

const isAcceptDenyOpen = useMagicFormBuilderStore(isAcceptDenyOpenSelector)

const onClickDefaults = () => {
clearRecentlyCreatedFieldIds()
setIsOpen(false)
setTimeout(() => setIsAcceptDenyOpen(false), 100) // delay to allow popover to close before updating state
}

const { deleteMultipleFormFieldsMutation } = useDeleteFormField()
Expand All @@ -203,7 +200,6 @@ const MagicFormBuilderPopover = ({
{!isAcceptDenyOpen ? (
<MagicFormBuilderCreateFormPrompt
onClose={() => setIsOpen(false)}
onSettled={() => setIsAcceptDenyOpen(true)}
/>
) : (
<MagicFormBuilderAcceptDeny
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import create from 'zustand'

export type MagicFormBuilderStore = {
isAcceptDenyOpen: boolean
recentlyCreatedFieldIds: Set<string>
setRecentlyCreatedFieldIds: (fieldIds: Set<string>) => void
clearRecentlyCreatedFieldIds: () => void
}

export const useMagicFormBuilderStore = create<MagicFormBuilderStore>(
(set) => ({
(set, get) => ({
isAcceptDenyOpen: get().recentlyCreatedFieldIds.size > 0,
recentlyCreatedFieldIds: new Set(),
setRecentlyCreatedFieldIds: (fieldIds) =>
set({ recentlyCreatedFieldIds: fieldIds }),
Expand All @@ -16,6 +18,10 @@ export const useMagicFormBuilderStore = create<MagicFormBuilderStore>(
}),
)

export const isAcceptDenyOpenSelector = (
state: MagicFormBuilderStore,
): MagicFormBuilderStore['isAcceptDenyOpen'] => state.isAcceptDenyOpen

export const recentlyCreatedFieldIdsSelector = (
state: MagicFormBuilderStore,
): MagicFormBuilderStore['recentlyCreatedFieldIds'] =>
Expand Down

0 comments on commit 6d8b9f9

Please sign in to comment.