diff --git a/src/components/ImageDropzone/ImageDropzone.tsx b/src/components/ImageDropzone/ImageDropzone.tsx index 5aaa35e..6bc11f3 100644 --- a/src/components/ImageDropzone/ImageDropzone.tsx +++ b/src/components/ImageDropzone/ImageDropzone.tsx @@ -1,6 +1,6 @@ import {Dropzone, DropzoneProps, FileWithPath} from "@mantine/dropzone"; import {FC, useState} from "react"; -import {Button, Fieldset, Flex, Group, Image, rem, Text} from "@mantine/core"; +import {Button, Fieldset, Flex, Group, Image, Loader, rem, Text} from "@mantine/core"; import {IconPhoto, IconUpload, IconX} from "@tabler/icons-react"; import {omit} from "lodash"; import {BaseFormInputProps} from "../../types/utils.ts"; @@ -20,8 +20,7 @@ const ImageDropzone: FC = (props: Props) => { !(typeof props.imageUrlInputProps?.value === 'string' && props.imageUrlInputProps.value.trim() !== '') ); - console.log(props.imageUrlInputProps); - console.log(showDropzone); + const [isLoading, setIsLoading] = useState(false); const restProps = omit(props, ['imageUrl', 'productId', 'imageUrlInputProps']); const onDrop = (files: FileWithPath[]) => { if (!props.productId || !props.imageUrlInputProps) return; @@ -31,6 +30,7 @@ const ImageDropzone: FC = (props: Props) => { } const file = files[0]; // TODO check if file is image + setIsLoading(true); ProductService.uploadProductImage({ productId: props.productId, formData: { @@ -38,19 +38,37 @@ const ImageDropzone: FC = (props: Props) => { } }).then(({ok, message, imageUrl}) => { notifications.guess(ok, {message}); + setIsLoading(false); + if (!ok || !imageUrl) { + setShowDropzone(true); + return; } props.imageUrlInputProps?.onChange(imageUrl); setShowDropzone(false); + }).catch(error => { + notifications.error({message: error.toString()}); + setShowDropzone(true); + setIsLoading(false); }); } const getBody = () => { - return props.imageUrlInputProps?.value && showDropzone ? ( + return props.imageUrlInputProps?.value && !showDropzone ? ( ) : ( + = (props: Props) => { /> -
+
- Drag images here or click to select files - - - Attach as many files as you like, each file should not exceed 5mb + Перенесите изображение или нажмите чтоб выбрать файл +
); } return ( - -
- {getBody()} + +
+ + {isLoading ? : getBody()} +
{!showDropzone && - + }
) diff --git a/src/shared/lib/utils.ts b/src/shared/lib/utils.ts index 245e577..a91dfe2 100644 --- a/src/shared/lib/utils.ts +++ b/src/shared/lib/utils.ts @@ -24,4 +24,24 @@ export const getPluralForm = (amount: number, one: string, twoFour: string, many default: return many; } -}; \ No newline at end of file +}; + +type KeysOf = (keyof T)[]; + +// Универсальная функция для получения ключей интерфейса +export function getKeys(): KeysOf { + return Object.keys({} as T) as KeysOf; +} + + +export const IMAGE_MIME_TYPES = [ + "image/png", + "image/jpeg", + "image/gif", + "image/bmp", + "image/tiff", + "image/x-icon", + "image/webp", + "image/svg+xml", + "image/heic" +];