|
| 1 | +import { Button } from '@heroui/button'; |
| 2 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 3 | +import { updatePublicProfileInfo } from '@lib/api/backend'; |
| 4 | +import { buildPublicProfileSchema } from '@schemas/profile'; |
| 5 | +import InputWithLabel from '@shared/InputWithLabel'; |
| 6 | +import Label from '@shared/Label'; |
| 7 | +import StyledInput from '@shared/StyledInput'; |
| 8 | +import { PublicProfileInfo } from '@typedefs/general'; |
| 9 | +import { useMemo, useState } from 'react'; |
| 10 | +import { Controller, FormProvider, useForm } from 'react-hook-form'; |
| 11 | +import toast from 'react-hot-toast'; |
| 12 | +import z from 'zod'; |
| 13 | + |
| 14 | +const PLATFORM_NAMES = { |
| 15 | + Linkedin: 'LinkedIn', |
| 16 | +}; |
| 17 | + |
| 18 | +export default function EditPublicProfile({ |
| 19 | + profileInfo, |
| 20 | + brandingPlatforms, |
| 21 | + setEditing, |
| 22 | + onEdit, |
| 23 | +}: { |
| 24 | + profileInfo: PublicProfileInfo; |
| 25 | + brandingPlatforms: string[]; |
| 26 | + setEditing: (editing: boolean) => void; |
| 27 | + onEdit: () => void; |
| 28 | +}) { |
| 29 | + const schema = useMemo(() => buildPublicProfileSchema(brandingPlatforms), [brandingPlatforms]); |
| 30 | + type FormValues = z.infer<typeof schema>; |
| 31 | + |
| 32 | + const [isLoading, setLoading] = useState<boolean>(false); |
| 33 | + |
| 34 | + const defaultValues: FormValues = useMemo(() => { |
| 35 | + const emptyLinks = brandingPlatforms.reduce( |
| 36 | + (acc, platform) => { |
| 37 | + acc[platform] = ''; |
| 38 | + return acc; |
| 39 | + }, |
| 40 | + {} as Record<string, string>, |
| 41 | + ); |
| 42 | + |
| 43 | + const values = { |
| 44 | + name: profileInfo.name ?? '', |
| 45 | + description: profileInfo.description ?? '', |
| 46 | + links: { |
| 47 | + ...emptyLinks, |
| 48 | + ...profileInfo.links, |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + return values; |
| 53 | + }, [profileInfo, brandingPlatforms]); |
| 54 | + |
| 55 | + const form = useForm<FormValues>({ |
| 56 | + resolver: zodResolver(schema), |
| 57 | + mode: 'onTouched', |
| 58 | + defaultValues, |
| 59 | + shouldUnregister: true, |
| 60 | + }); |
| 61 | + |
| 62 | + const { control } = form; |
| 63 | + |
| 64 | + const onSubmit = async (data: FormValues) => { |
| 65 | + console.log(data); |
| 66 | + setLoading(true); |
| 67 | + |
| 68 | + try { |
| 69 | + await updatePublicProfileInfo(data); |
| 70 | + toast.success('Public profile updated successfully.'); |
| 71 | + onEdit(); |
| 72 | + } catch (error) { |
| 73 | + console.error('Error updating public profile info.'); |
| 74 | + } finally { |
| 75 | + setLoading(false); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + const onError = (errors: any) => { |
| 80 | + console.log('Validation errors:', errors); |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <FormProvider {...form}> |
| 85 | + <form onSubmit={form.handleSubmit(onSubmit, onError)}> |
| 86 | + <div className="col gap-2"> |
| 87 | + <InputWithLabel name="name" label="Name" placeholder="" /> |
| 88 | + <InputWithLabel name="description" label="Description" placeholder="None" /> |
| 89 | + |
| 90 | + <Label value="Links" /> |
| 91 | + {brandingPlatforms |
| 92 | + .sort((a, b) => a.localeCompare(b)) |
| 93 | + .map((platform, index) => { |
| 94 | + return ( |
| 95 | + <Controller |
| 96 | + key={index} |
| 97 | + name={`links.${platform}`} |
| 98 | + control={control} |
| 99 | + render={({ field, fieldState }) => { |
| 100 | + return ( |
| 101 | + <StyledInput |
| 102 | + placeholder={PLATFORM_NAMES[platform] ?? platform} |
| 103 | + value={field.value ?? ''} |
| 104 | + onChange={(e) => { |
| 105 | + const value = e.target.value; |
| 106 | + field.onChange(value); |
| 107 | + }} |
| 108 | + onBlur={field.onBlur} |
| 109 | + isInvalid={!!fieldState.error} |
| 110 | + errorMessage={fieldState.error?.message} |
| 111 | + /> |
| 112 | + ); |
| 113 | + }} |
| 114 | + /> |
| 115 | + ); |
| 116 | + })} |
| 117 | + |
| 118 | + <div className="row mt-2 justify-between"> |
| 119 | + <Button |
| 120 | + className="h-9 border-2 border-slate-200 bg-white data-[hover=true]:opacity-65!" |
| 121 | + color="default" |
| 122 | + size="sm" |
| 123 | + variant="solid" |
| 124 | + onPress={() => setEditing(false)} |
| 125 | + > |
| 126 | + <div className="text-sm">Cancel</div> |
| 127 | + </Button> |
| 128 | + |
| 129 | + <Button className="h-9" type="submit" color="primary" size="sm" variant="solid" isLoading={isLoading}> |
| 130 | + <div className="text-sm">Update profile</div> |
| 131 | + </Button> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + </form> |
| 135 | + </FormProvider> |
| 136 | + ); |
| 137 | +} |
0 commit comments