Skip to content

Commit

Permalink
avatar form submits the file on change instead of having to click a s…
Browse files Browse the repository at this point in the history
…ubmit button
  • Loading branch information
dpgraham4401 committed Aug 31, 2024
1 parent d687154 commit a3af05c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions client/app/features/profile/components/AvatarForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { useForm } from 'react-hook-form';

import { FaUser } from 'react-icons/fa';
Expand All @@ -13,7 +13,7 @@ interface AvatarFormProps {
export function AvatarForm({ avatar }: AvatarFormProps) {
const [preview, setPreview] = useState<string | undefined>(avatar);
const inputRef = useRef<HTMLInputElement | null>(null);
const { register, handleSubmit } = useForm();
const { register, handleSubmit, watch } = useForm({ mode: 'onChange' });
const { user } = useAuth();

const onSubmit = (data: any) => {
Expand All @@ -28,7 +28,16 @@ export function AvatarForm({ avatar }: AvatarFormProps) {
.catch((err) => console.log(err));
};

const registerProps = register('avatar');
const registerProps = register('avatar', {
required: true,
});

// When the avatar is changed, submit the form
useEffect(() => {
watch((data) => {
onSubmit(data);
});
}, [watch]);

return (
<form onSubmit={handleSubmit(onSubmit)}>
Expand All @@ -51,7 +60,6 @@ export function AvatarForm({ avatar }: AvatarFormProps) {
</Avatar>
</button>
</div>
<button type="submit">Submit</button>
</form>
);
}

0 comments on commit a3af05c

Please sign in to comment.