diff --git a/src/ui/components/internal/create-article-form.tsx b/src/ui/components/internal/create-article-form.tsx index 099a170..cf010e8 100644 --- a/src/ui/components/internal/create-article-form.tsx +++ b/src/ui/components/internal/create-article-form.tsx @@ -54,6 +54,7 @@ function validateContentFile(file: File): string | null { export const CreateArticleForm = () => { const [coverImageFile, setCoverImageFile] = useState(null); + const [coverImageUploading, setCoverImageUploading] = useState(false); const [contentFile, setContentFile] = useState(null); const coverImageUrlRef = useRef(null); @@ -78,10 +79,8 @@ export const CreateArticleForm = () => { }, }); - const title = useWatch({ - control: form.control, - name: 'title', - }); + const title = useWatch({ control: form.control, name: 'title' }); + const coverImageUrl = useWatch({ control: form.control, name: 'coverImageUrl' }); useEffect(() => { if (!title) return; form.setValue('slug', slugify(title).toLowerCase()); @@ -93,6 +92,7 @@ export const CreateArticleForm = () => { coverImageUrlRef.current = null; } setCoverImageFile(null); + setCoverImageUploading(false); setContentFile(null); }, []); @@ -128,10 +128,13 @@ export const CreateArticleForm = () => { setCoverImageFile(file); if (!file) { setCoverImageFile(null); + setCoverImageUploading(false); form.setValue('coverImageUrl', ''); return; } + setCoverImageUploading(true); const fileMetadataResult = await uploadFile(file); + setCoverImageUploading(false); if (!fileMetadataResult.ok) { setCoverImageFile(null); form.setValue('coverImageUrl', ''); @@ -255,6 +258,8 @@ export const CreateArticleForm = () => { label='Cover image' description='PNG, JPG, GIF, WebP accepted' error={form.formState.errors.coverImageUrl?.message} + previewUrl={coverImageUrl || undefined} + isUploading={coverImageUploading} icon={ = ({ @@ -41,6 +44,8 @@ export const FileUploadField: React.FC = ({ description, error, icon, + previewUrl, + isUploading, }) => { const handleAccept = useCallback( (files: File[]) => { @@ -90,7 +95,24 @@ export const FileUploadField: React.FC = ({ {file && ( - + ( + + ) + : previewUrl + ? () => ( + // eslint-disable-next-line @next/next/no-img-element + Uploaded image + ) + : undefined + } + />