-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
852 additions
and
437 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
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,72 @@ | ||
import React from 'react' | ||
import { VoxButton } from '@/components/Button' | ||
import VoxCard from '@/components/VoxCard/VoxCard' | ||
import { ProfileFormError } from '@/services/profile/error' | ||
import { useMutationUpdateProfil } from '@/services/profile/hook' | ||
import { ErrorMonitor } from '@/utils/ErrorMonitor' | ||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import { has } from 'lodash' | ||
import { Control, DefaultValues, FieldValues, FormState, Path, useForm } from 'react-hook-form' | ||
import { XStack } from 'tamagui' | ||
import * as z from 'zod' | ||
|
||
const isPathExist = <TF extends FieldValues, S extends string>(path: S, obj: TF): path is Path<TF> => { | ||
return has(obj, path) | ||
} | ||
|
||
const AbstractForm = <T extends z.Schema<any, any>, TF extends FieldValues>( | ||
props: { | ||
defaultValues: TF | ||
uuid: string | ||
validatorSchema: T | ||
onErrors?: (errors: FormState<TF>['errors']) => void | ||
children: (props: { control: Control<TF>; formState: FormState<TF> }) => React.ReactNode | ||
} & { cardProps?: React.ComponentProps<typeof VoxCard> }, | ||
) => { | ||
const { control, handleSubmit, formState, reset, setError } = useForm({ | ||
resolver: zodResolver(props.validatorSchema), | ||
values: props.defaultValues, | ||
mode: 'all', | ||
}) | ||
const { isDirty, isValid } = formState | ||
|
||
React.useEffect(() => { | ||
if (props.onErrors) { | ||
props.onErrors(formState.errors) | ||
} | ||
}, [formState]) | ||
|
||
const { mutateAsync, isPending } = useMutationUpdateProfil({ userUuid: props.uuid }) | ||
|
||
const onSubmit = handleSubmit((data) => { | ||
mutateAsync(data).catch((e) => { | ||
if (e instanceof ProfileFormError) { | ||
e.violations.forEach((violation) => { | ||
if (isPathExist(violation.propertyPath, props.defaultValues)) { | ||
setError(violation.propertyPath, { message: violation.message }) | ||
} else { | ||
ErrorMonitor.log('Unknown property path / profil form', violation) | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
return ( | ||
<VoxCard {...props.cardProps}> | ||
<VoxCard.Content> | ||
{props.children({ control, formState })} | ||
<XStack justifyContent="flex-end" gap="$2"> | ||
<VoxButton variant="outlined" display={isDirty ? 'flex' : 'none'} onPress={() => reset()}> | ||
Annuler | ||
</VoxButton> | ||
<VoxButton variant="outlined" theme="blue" onPress={onSubmit} loading={isPending} disabled={!isDirty || !isValid}> | ||
Enregister | ||
</VoxButton> | ||
</XStack> | ||
</VoxCard.Content> | ||
</VoxCard> | ||
) | ||
} | ||
|
||
export default AbstractForm |
Oops, something went wrong.