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