diff --git a/pkg/interface/src/views/components/ImageInput.tsx b/pkg/interface/src/views/components/ImageInput.tsx index 9f88025a2..dc7a565e7 100644 --- a/pkg/interface/src/views/components/ImageInput.tsx +++ b/pkg/interface/src/views/components/ImageInput.tsx @@ -1,8 +1,13 @@ import { - BaseInput, Box, + BaseInput, + Box, Button, - Icon, Label, Row, StatelessTextInput as Input, - Text + Icon, + Label, + Row, + StatelessTextInput as Input, + Text, + ErrorLabel } from '@tlon/indigo-react'; import { useField } from 'formik'; import React, { ReactElement, useCallback, useRef, useState } from 'react'; @@ -20,23 +25,19 @@ const prompt = ( uploading, meta, clickUploadButton, - canUpload + canUpload, + error ) => { - if ( - !focus && - !field.value && - !uploading && - meta.error === undefined - ) { + if (!focus && !field.value && !uploading && error === undefined) { return ( e.preventDefault} @@ -50,7 +51,7 @@ const prompt = ( cursor="pointer" color="blue" style={{ pointerEvents: 'all' }} - mx='0.5ch' + mx="0.5ch" onClick={clickUploadButton} > upload @@ -70,9 +71,9 @@ const uploadingStatus = (uploading, meta) => { e.preventDefault} @@ -84,27 +85,27 @@ const uploadingStatus = (uploading, meta) => { return null; }; -const errorRetry = (meta, focus, uploading, clickUploadButton) => { - if (!focus && meta.error !== undefined) { +const errorRetry = (meta, error, focus, uploading, clickUploadButton) => { + if (!focus && error !== undefined && meta.touched) { return ( e.preventDefault} > - {meta.error} + {error} {', '}please{' '} @@ -143,7 +144,8 @@ export const clearButton = (field, uploading, clearEvt) => { export function ImageInput(props: ImageInputProps): ReactElement { const { id, label, caption } = props; const { uploadDefault, canUpload, uploading } = useStorage(); - const [field, meta, { setValue, setError }] = useField(id); + const [field, meta, { setValue, setTouched }] = useField(id); + const [uploadError, setUploadError] = useState(); const [focus, setFocus] = useState(false); const ref = useRef(null); @@ -170,9 +172,10 @@ export function ImageInput(props: ImageInputProps): ReactElement { const url = await uploadDefault(file); setFocus(false); setValue(url); + setTouched(true); } catch (e) { setFocus(false); - setError(e.message); + setUploadError(e); } }, [ref.current, uploadDefault, canUpload, setValue]); @@ -184,19 +187,19 @@ export function ImageInput(props: ImageInputProps): ReactElement { {caption} ) : null} - - {prompt(field, focus, uploading, meta, clickUploadButton, canUpload)} + + {prompt(field, focus, uploading, meta, clickUploadButton, canUpload, uploadError)} {clearButton(field, uploading, clearEvt)} {uploadingStatus(uploading, meta)} - {errorRetry(meta, focus, uploading, clickUploadButton)} - + {errorRetry(meta, uploadError, focus, uploading, clickUploadButton)} + setFocus(true)} onBlur={e => handleBlur(e)} - hasError={meta.touched && meta.error !== undefined} + hasError={Boolean(uploadError)} /> {canUpload && ( @@ -213,6 +216,9 @@ export function ImageInput(props: ImageInputProps): ReactElement { )} + + {meta.error} + ); }