Skip to content

Commit 554843f

Browse files
committed
fix: Add error handling for empty preview URL in EditImageFormField
1 parent cb8f265 commit 554843f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/Shared/Components/EditImageFormField/EditImageFormField.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ButtonWithLoader, ImageWithFallback } from '@Shared/Components'
66
import { validateIfImageExist, validateURL } from '@Shared/validations'
77
import { ReactComponent as ICPencil } from '@Icons/ic-pencil.svg'
88
import { EditImageFormFieldProps, FallbackImageProps } from './types'
9-
import { DEFAULT_IMAGE_DIMENSIONS, DEFAULT_MAX_IMAGE_SIZE } from './constants'
9+
import { DEFAULT_IMAGE_DIMENSIONS, DEFAULT_MAX_IMAGE_SIZE, EMPTY_PREVIEW_URL_ERROR_MESSAGE } from './constants'
1010
import './EditImageFormField.scss'
1111

1212
const FallbackImage = ({ showEditIcon, defaultIcon }: FallbackImageProps) => (
@@ -36,6 +36,8 @@ const EditImageFormField = ({
3636
const [lastPreviewedURL, setLastPreviewedURL] = useState<string>(url)
3737
const [isEditing, setIsEditing] = useState<boolean>(false)
3838
const [isLoading, setIsLoading] = useState<boolean>(false)
39+
// Since we need to show error message for empty preview URL but not propagate it to parent, we need to maintain a separate state
40+
const [emptyPreviewURLErrorMessage, setEmptyPreviewURLErrorMessage] = useState<string>('')
3941

4042
const handleEnableEditing = () => {
4143
setIsEditing(true)
@@ -47,6 +49,7 @@ const EditImageFormField = ({
4749

4850
const handleReset = (newURL?: string) => {
4951
handleLastPreviewedURLChange(newURL ?? lastPreviewedURL)
52+
handleURLChange(newURL ?? lastPreviewedURL)
5053
handleError('')
5154
setIsEditing(false)
5255
setIsLoading(false)
@@ -63,15 +66,19 @@ const EditImageFormField = ({
6366
const handleChange = (event: SyntheticEvent) => {
6467
const { value } = event.target as HTMLInputElement
6568
handleURLChange(value)
66-
if (value.trim()) {
67-
handleError(validateURL(value, false).message)
69+
if (!value.trim()) {
70+
handleError('')
71+
return
6872
}
73+
setEmptyPreviewURLErrorMessage('')
74+
handleError(validateURL(value, false).message)
6975
}
7076

7177
const handlePreviewImage = async () => {
7278
if (!url) {
7379
// Not setting the error since can save without image
74-
toast.error('Please enter a valid image URL')
80+
setEmptyPreviewURLErrorMessage(EMPTY_PREVIEW_URL_ERROR_MESSAGE)
81+
toast.error(EMPTY_PREVIEW_URL_ERROR_MESSAGE)
7582
return
7683
}
7784

@@ -159,10 +166,11 @@ const EditImageFormField = ({
159166
placeholder="Enter image url"
160167
value={url}
161168
onChange={handleChange}
162-
error={errorMessage}
169+
error={errorMessage || emptyPreviewURLErrorMessage}
163170
inputWrapClassName="w-100"
164171
dataTestid={`${dataTestIdPrefix}-input`}
165172
onKeyDown={handleKeyDown}
173+
autoFocus
166174
/>
167175
</div>
168176

src/Shared/Components/EditImageFormField/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export const DEFAULT_IMAGE_DIMENSIONS = {
33
width: 72,
44
height: 72,
55
} as const
6+
export const EMPTY_PREVIEW_URL_ERROR_MESSAGE = 'Please provide image URL to preview'

0 commit comments

Comments
 (0)