| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | fieldsWithModelValues, |
| 5 | getAdditionalFieldDefaultValues, |
| 6 | getAdditionalFieldSubmitValues, |
| 7 | validateStringLength, |
| 8 | } from "@better-auth-ui/core"; |
| 9 | import type { UsernameAuthClient |
| 10 | import { useAuth, useSession |
| 11 | import { useEffect, useMemo |
| 12 | import { toast } |
| 13 | |
| 14 | import { Card, CardContent |
| 15 | import { Field, FieldLabel |
| 16 | import { Input } |
| 17 | import { Skeleton } |
| 18 | import { |
| 19 | getAuthAdditionalFieldValidators, |
| 20 | isAuthFormFieldInvalid, |
| 21 | useAuthForm, |
| 22 | } from "../../auth-form"; |
| 23 | import { ChangeAvatar } |
| 24 | |
| 25 | export function UserProfile() |
| 26 | const { additionalFields, |
| 27 | useAuth<UsernameAuthClient> |
| 28 | const { data: |
| 29 | |
| 30 | const { mutateAsync: |
| 31 | onSuccess: () => |
| 32 | }); |
| 33 | |
| 34 | const profileFields = |
| 35 | () => additionalFields?. |
| 36 | [additionalFields], |
| 37 | ); |
| 38 | const form = |
| 39 | defaultValues: { |
| 40 | additionalFields: getAdditionalFieldDefaultValues(profileFields |
| 41 | name: "", |
| 42 | }, |
| 43 | onSubmit: async ( |
| 44 | await updateUser({ |
| 45 | name: value.name |
| 46 | ...getAdditionalFieldSubmitValues( |
| 47 | profileFields, |
| 48 | value.additionalFields, |
| 49 | ), |
| 50 | }); |
| 51 | }, |
| 52 | }); |
| 53 | |
| 54 | useEffect(() => |
| 55 | if (!session) |
| 56 | form.reset({ |
| 57 | additionalFields: getAdditionalFieldDefaultValues( |
| 58 | fieldsWithModelValues( |
| 59 | profileFields, |
| 60 | session.user as |
| 61 | ), |
| 62 | ), |
| 63 | name: session.user |
| 64 | }); |
| 65 | }, [form, |
| 66 | |
| 67 | return ( |
| 68 | <div> |
| 69 | <h2 className="text-sm font-semibold mb-3 |
| 70 | {localization.settings.userProfile |
| 71 | </h2> |
| 72 | |
| 73 | <form.AppForm> |
| 74 | <form.AuthFormRoot> |
| 75 | <Card> |
| 76 | <CardContent className=" |
| 77 | <ChangeAvatar /> |
| 78 | |
| 79 | <form.AppField |
| 80 | name="name" |
| 81 | validators={{ |
| 82 | onChange: ({ value |
| 83 | validateStringLength(value, { |
| 84 | requiredMessage: localization.auth |
| 85 | trim: true, |
| 86 | }), |
| 87 | }} |
| 88 | > |
| 89 | {(field) => |
| 90 | const isInvalid = |
| 91 | |
| 92 | return ( |
| 93 | <Field data-invalid={ |
| 94 | <FieldLabel htmlFor=" |
| 95 | {localization.auth.name |
| 96 | </FieldLabel> |
| 97 | |
| 98 | {session ? ( |
| 99 | <Input |
| 100 | id="name" |
| 101 | name={field.name |
| 102 | autoComplete="name" |
| 103 | placeholder={localization.auth |
| 104 | disabled={isPending} |
| 105 | value={field.state |
| 106 | onBlur={field.handleBlur |
| 107 | onChange={(event) |
| 108 | field.handleChange(event |
| 109 | } |
| 110 | aria-invalid={isInvalid} |
| 111 | /> |
| 112 | ) : ( |
| 113 | <Skeleton> |
| 114 | <Input className=" |
| 115 | </Skeleton> |
| 116 | )} |
| 117 | |
| 118 | <field.AuthFormFieldError /> |
| 119 | </Field> |
| 120 | ); |
| 121 | }} |
| 122 | </form.AppField> |
| 123 | |
| 124 | {profileFields.map(( |
| 125 | if (!session) { |
| 126 | if (configuredField.inputType |
| 127 | return null; |
| 128 | } |
| 129 | |
| 130 | return ( |
| 131 | <Skeleton key={ |
| 132 | <Input className=" |
| 133 | </Skeleton> |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | return ( |
| 138 | <form.AppField |
| 139 | key={configuredField.name |
| 140 | name={`additionalFields.${configuredField |
| 141 | validators={getAuthAdditionalFieldValidators( |
| 142 | configuredField, |
| 143 | localization.auth.fieldRequired |
| 144 | )} |
| 145 | > |
| 146 | {(field) => |
| 147 | <field.AuthFormAdditionalField |
| 148 | field={configuredField} |
| 149 | isPending={isPending} |
| 150 | /> |
| 151 | )} |
| 152 | </form.AppField> |
| 153 | ); |
| 154 | })} |
| 155 | </CardContent> |
| 156 | |
| 157 | <CardFooter> |
| 158 | <form.AuthFormSubmitButton |
| 159 | size="sm" |
| 160 | disabled={isPending || |
| 161 | > |
| 162 | {localization.settings.saveChanges |
| 163 | </form.AuthFormSubmitButton> |
| 164 | </CardFooter> |
| 165 | </Card> |
| 166 | </form.AuthFormRoot> |
| 167 | </form.AppForm> |
| 168 | </div> |
| 169 | ); |
| 170 | } |