Skip to content

Commit

Permalink
fix(profil): don't reset on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
OverGlass committed Sep 24, 2024
1 parent db03a4a commit a148b9e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
32 changes: 14 additions & 18 deletions src/screens/profil/account/form/AbstractProfilForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { Control, DefaultValues, FieldValues, FormState, Path, useForm } from 'r
import { XStack } from 'tamagui'
import * as z from 'zod'

const isPathExist = <TF extends FieldValues, S extends string>(path: S, obj: DefaultValues<TF>): path is Path<TF> => {
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: DefaultValues<TF>
defaultValues: TF
uuid: string
validatorSchema: T
onErrors?: (errors: FormState<TF>['errors']) => void
Expand All @@ -25,7 +25,7 @@ const AbstractForm = <T extends z.Schema<any, any>, TF extends FieldValues>(
) => {
const { control, handleSubmit, formState, reset, setError } = useForm({
resolver: zodResolver(props.validatorSchema),
defaultValues: props.defaultValues,
values: props.defaultValues,
mode: 'all',
})
const { isDirty, isValid } = formState
Expand All @@ -39,21 +39,17 @@ const AbstractForm = <T extends z.Schema<any, any>, TF extends FieldValues>(
const { mutateAsync, isPending } = useMutationUpdateProfil({ userUuid: props.uuid })

const onSubmit = handleSubmit((data) => {
mutateAsync(data)
.then(() => {
reset()
})
.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)
}
})
}
})
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 (
Expand Down
4 changes: 2 additions & 2 deletions src/screens/profil/account/form/RSForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const socialPlatforms = [
{ id: 'facebook', placeholder: 'facebook.com/nom-utilisateur', label: 'Facebook', starturl: 'https://facebook.com/' },
{ id: 'telegram', placeholder: 't.me/nom-utilisateur', label: 'Telegram', starturl: 'https://t.me/' },
{ id: 'instagram', placeholder: 'instagram.com/nom-utilisateur', label: 'Instagram', starturl: 'https://instagram.com/' },
{ id: 'twitter', placeholder: 'twitter.com/nom-utilisateur', label: 'Twitter', starturl: 'https://twitter.com/' },
{ id: 'twitter', placeholder: 'twitter.com/nom-utilisateur', label: 'Twitter', starturl: 'https://x.com/' },
{ id: 'linkedin', placeholder: 'linkedin.com/nom-utilisateur', label: 'Linkedin', starturl: 'https://linkedin.com/in/' },
] as const

Expand Down Expand Up @@ -43,7 +43,7 @@ export const RSForm = ({ profile }: { profile: RestDetailedProfileResponse }) =>
<Input
color="gray"
placeholder={platform.label}
value={!!value ? value : platform.starturl}
value={value ?? platform.starturl}
onBlur={onBlur}
error={error?.message}
onChange={onChange}
Expand Down

0 comments on commit a148b9e

Please sign in to comment.