-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from SocialGouv/fix/offer-use-form
feat: payload form builder integration
- Loading branch information
Showing
19 changed files
with
850 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import React, { useState, useCallback } from "react"; | ||
import { buildInitialFormState } from "./buildInitialFormState"; | ||
import { fields } from "./fields"; | ||
import { Form as FormType } from "@payloadcms/plugin-form-builder/dist/types"; | ||
import { useForm } from "react-hook-form"; | ||
import { useRouter } from "next/router"; | ||
import { | ||
Box, | ||
ButtonGroup, | ||
Flex, | ||
Icon, | ||
IconButton, | ||
Text, | ||
} from "@chakra-ui/react"; | ||
import { api } from "~/utils/api"; | ||
import { HiArrowLeft, HiArrowRight } from "react-icons/hi2"; | ||
|
||
export type Value = unknown; | ||
|
||
export interface Property { | ||
[key: string]: Value; | ||
} | ||
|
||
export interface Data { | ||
[key: string]: Value | Property | Property[]; | ||
} | ||
|
||
export type FormBlockType = { | ||
blockName?: string; | ||
blockType?: "formBlock"; | ||
enableIntro: Boolean; | ||
form: FormType; | ||
}; | ||
|
||
export const FormBlock: React.FC< | ||
FormBlockType & { | ||
id?: string; | ||
afterOnSubmit: () => void; | ||
} | ||
> = (props) => { | ||
const { | ||
form: formFromProps, | ||
form: { id: formID, confirmationType, redirect } = {}, | ||
afterOnSubmit, | ||
} = props; | ||
|
||
const formMethods = useForm({ | ||
defaultValues: buildInitialFormState(formFromProps.fields), | ||
}); | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
control, | ||
} = formMethods; | ||
|
||
const { mutateAsync } = api.form.submitForm.useMutation(); | ||
|
||
const [currentStep, setCurrentStep] = useState(0); | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
const [hasSubmitted, setHasSubmitted] = useState<boolean>(); | ||
const [error, setError] = useState< | ||
{ status?: string; message: string } | undefined | ||
>(); | ||
const router = useRouter(); | ||
|
||
const handleNextStep = () => { | ||
if (currentStep === formFromProps.fields.length - 1) { | ||
onSubmit(formMethods.getValues()); | ||
} else { | ||
setCurrentStep((prev) => prev + 1); | ||
} | ||
}; | ||
|
||
// const handlePrevStep = () => { | ||
// setCurrentStep((prev) => prev - 1); | ||
// }; | ||
|
||
const onSubmit = useCallback( | ||
(data: Data) => { | ||
let loadingTimerID: ReturnType<typeof setTimeout>; | ||
|
||
const submitForm = async () => { | ||
setError(undefined); | ||
|
||
const dataToSend = Object.entries(data).map(([name, value]) => ({ | ||
field: name, | ||
value, | ||
})) as { field: string; value: string }[]; | ||
|
||
loadingTimerID = setTimeout(() => { | ||
setIsLoading(true); | ||
}, 1000); | ||
|
||
try { | ||
await mutateAsync({ | ||
formId: formID as unknown as number, | ||
submissionData: dataToSend, | ||
}); | ||
|
||
clearTimeout(loadingTimerID); | ||
|
||
setIsLoading(false); | ||
setHasSubmitted(true); | ||
|
||
afterOnSubmit(); | ||
} catch (err) { | ||
console.warn(err); | ||
setIsLoading(false); | ||
setError({ | ||
message: "Something went wrong.", | ||
}); | ||
} | ||
}; | ||
|
||
submitForm(); | ||
}, | ||
[router, formID, redirect, confirmationType] | ||
); | ||
|
||
return ( | ||
<div> | ||
{error && <div>{`${error.status || "500"}: ${error.message || ""}`}</div>} | ||
{!hasSubmitted && ( | ||
<Box as="form" id={formID} onSubmit={handleSubmit(onSubmit)} w="full"> | ||
<div> | ||
{formFromProps && | ||
formFromProps.fields && | ||
(() => { | ||
const field = formFromProps.fields[currentStep]; | ||
const Field: React.FC<any> = | ||
fields?.[field.blockType as keyof typeof fields]; | ||
return ( | ||
<Flex flexDir="column" gap={6}> | ||
{"label" in field && ( | ||
<Text fontSize={24} fontWeight={800} textAlign="center"> | ||
{field.label} | ||
</Text> | ||
)} | ||
<React.Fragment key={field.blockName}> | ||
<Field | ||
form={formFromProps} | ||
field={{ ...field, label: undefined }} | ||
{...formMethods} | ||
register={register} | ||
errors={errors} | ||
control={control} | ||
/> | ||
</React.Fragment> | ||
</Flex> | ||
); | ||
})()} | ||
</div> | ||
<ButtonGroup justifyContent="space-between" w="full" mt={6}> | ||
{/* {currentStep > 0 && ( | ||
<IconButton | ||
colorScheme="blackBtn" | ||
aria-label="Next" | ||
icon={<Icon as={HiArrowLeft} w={5} h={5} />} | ||
onClick={handlePrevStep} | ||
px={6} | ||
/> | ||
)} */} | ||
<IconButton | ||
colorScheme="blackBtn" | ||
aria-label="Next" | ||
isLoading={isLoading || hasSubmitted} | ||
icon={<Icon as={HiArrowRight} w={5} h={5} />} | ||
onClick={handleNextStep} | ||
ml="auto" | ||
px={6} | ||
/> | ||
</ButtonGroup> | ||
</Box> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from "react"; | ||
import { TextField } from "@payloadcms/plugin-form-builder/dist/types"; | ||
import { | ||
UseFormRegister, | ||
FieldValues, | ||
FieldErrorsImpl, | ||
Controller, | ||
} from "react-hook-form"; | ||
import { | ||
Flex, | ||
FormControl, | ||
FormLabel, | ||
IconButton, | ||
Text, | ||
} from "@chakra-ui/react"; | ||
|
||
export const Ladder: React.FC<{ | ||
register: UseFormRegister<FieldValues & any>; | ||
control: any; | ||
errors: Partial< | ||
FieldErrorsImpl<{ | ||
[x: string]: any; | ||
}> | ||
>; | ||
field: TextField & { | ||
min: number; | ||
max: number; | ||
textLegend: { label: string }[]; | ||
}; | ||
}> = ({ field: currentField, control }) => { | ||
const ladderArr = Array.from( | ||
{ length: currentField.max - currentField.min + 1 }, | ||
(_, index) => (index + currentField.min).toString() | ||
); | ||
|
||
return ( | ||
<FormControl> | ||
<Controller | ||
control={control} | ||
name={currentField.name} | ||
render={({ field: { onChange, value } }) => { | ||
return ( | ||
<Flex flexDir="column" gap={2} alignItems="center"> | ||
<Flex alignItems="center" gap={0.5} w="full"> | ||
{ladderArr.map((item) => ( | ||
<IconButton | ||
flex={1} | ||
key={item} | ||
aria-label="Test" | ||
icon={<>{item}</>} | ||
h={10} | ||
borderRadius="base" | ||
fontSize={16} | ||
fontWeight={800} | ||
minWidth="auto" | ||
color={value === item ? "white" : "black"} | ||
colorScheme={ | ||
value === item ? "primaryShades" : "bgGrayShades" | ||
} | ||
onClick={() => onChange(item)} | ||
/> | ||
))} | ||
</Flex> | ||
<Flex w="full" alignItems="center" justifyContent="space-between"> | ||
{currentField.textLegend.map((item) => ( | ||
<Text fontSize={14} fontWeight={500}> | ||
{item.label} | ||
</Text> | ||
))} | ||
</Flex> | ||
</Flex> | ||
); | ||
}} | ||
/> | ||
</FormControl> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from "react"; | ||
import { TextField } from "@payloadcms/plugin-form-builder/dist/types"; | ||
import { | ||
UseFormRegister, | ||
FieldValues, | ||
FieldErrorsImpl, | ||
FieldError, | ||
} from "react-hook-form"; | ||
import FormInput from "../../FormInput"; | ||
|
||
export const Textarea: React.FC<{ | ||
register: UseFormRegister<FieldValues & any>; | ||
errors: Partial< | ||
FieldErrorsImpl<{ | ||
[x: string]: any; | ||
}> | ||
>; | ||
field: TextField; | ||
}> = ({ register, field, errors }) => { | ||
return ( | ||
<FormInput | ||
key={field.name} | ||
register={register} | ||
field={{ ...field, kind: field.blockType as any }} | ||
fieldError={errors[field.name] as FieldError} | ||
/> | ||
); | ||
}; |
43 changes: 43 additions & 0 deletions
43
webapp/src/components/forms/payload/buildInitialFormState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { FormFieldBlock } from "@payloadcms/plugin-form-builder/dist/types"; | ||
|
||
export const buildInitialFormState = (fields: FormFieldBlock[]) => { | ||
return fields.reduce((initialSchema, field) => { | ||
if (field.blockType === "checkbox") { | ||
return { | ||
...initialSchema, | ||
[field.name]: false, | ||
}; | ||
} | ||
if (field.blockType === "country") { | ||
return { | ||
...initialSchema, | ||
[field.name]: "", | ||
}; | ||
} | ||
if (field.blockType === "email") { | ||
return { | ||
...initialSchema, | ||
[field.name]: "", | ||
}; | ||
} | ||
if (field.blockType === "text") { | ||
return { | ||
...initialSchema, | ||
[field.name]: "", | ||
}; | ||
} | ||
if (field.blockType === "select") { | ||
return { | ||
...initialSchema, | ||
[field.name]: "", | ||
}; | ||
} | ||
if (field.blockType === "state") { | ||
return { | ||
...initialSchema, | ||
[field.name]: "", | ||
}; | ||
} | ||
return initialSchema; | ||
}, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Ladder } from "./Ladder"; | ||
import { Textarea } from "./Textarea"; | ||
|
||
export const fields = { | ||
country: Ladder, | ||
textarea: Textarea, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.