Skip to content

Commit

Permalink
avatar form uses the icon as a button and updates the state of the av…
Browse files Browse the repository at this point in the history
…atar on success request
  • Loading branch information
dpgraham4401 committed Aug 31, 2024
1 parent 96008e1 commit d687154
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions client/app/features/profile/components/AvatarForm.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React from 'react';
import React, { useRef, useState } from 'react';
import { useForm } from 'react-hook-form';
import { Input } from '~/components/ui';

import { FaUser } from 'react-icons/fa';
import { Avatar, AvatarFallback, AvatarImage, Input } from '~/components/ui';
import { useAuth } from '~/hooks';
import { htApi } from '~/services';

export function AvatarForm() {
interface AvatarFormProps {
avatar?: string;
}

export function AvatarForm({ avatar }: AvatarFormProps) {
const [preview, setPreview] = useState<string | undefined>(avatar);
const inputRef = useRef<HTMLInputElement | null>(null);
const { register, handleSubmit } = useForm();
const { user } = useAuth();

Expand All @@ -16,13 +24,33 @@ export function AvatarForm() {
.patch(`/profile/${user?.id}`, formData, {
headers: { 'Content-Type': 'multipart/form-data' },
})
.then((res) => console.log(res))
.then((res) => setPreview(res.data.avatar))
.catch((err) => console.log(err));
};

const registerProps = register('avatar');

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Input type="file" {...register('avatar')} />
<Input
type="file"
{...registerProps}
ref={(e) => {
registerProps.ref(e);
inputRef.current = e;
}}
className="tw-hidden"
/>
<div className="tw-flex tw-justify-center">
<button type="button" onClick={() => inputRef.current?.click()}>
<Avatar className="tw-h-40 tw-w-40">
<AvatarImage src={preview} alt="avatar" />
<AvatarFallback>
<FaUser size={80} />
</AvatarFallback>
</Avatar>
</button>
</div>
<button type="submit">Submit</button>
</form>
);
Expand Down

0 comments on commit d687154

Please sign in to comment.