From 1e40e79be19371718864d7e79594f3a895927db3 Mon Sep 17 00:00:00 2001 From: fakz9 Date: Sat, 25 May 2024 11:35:24 +0300 Subject: [PATCH] temp image upload --- src/client/models/ProductCreateRequest.ts | 1 + src/client/models/ProductSchema.ts | 1 + .../ImageDropzone/ImageDropzone.tsx | 132 ++++++++++++------ .../CreateProductModal/CreateProductModal.tsx | 20 ++- 4 files changed, 105 insertions(+), 49 deletions(-) diff --git a/src/client/models/ProductCreateRequest.ts b/src/client/models/ProductCreateRequest.ts index 2c50648..bff4025 100644 --- a/src/client/models/ProductCreateRequest.ts +++ b/src/client/models/ProductCreateRequest.ts @@ -14,5 +14,6 @@ export type ProductCreateRequest = { composition?: (string | null); size?: (string | null); additionalInfo?: (string | null); + imageUrl?: (string | null); }; diff --git a/src/client/models/ProductSchema.ts b/src/client/models/ProductSchema.ts index 168ae41..a520df9 100644 --- a/src/client/models/ProductSchema.ts +++ b/src/client/models/ProductSchema.ts @@ -14,6 +14,7 @@ export type ProductSchema = { composition?: (string | null); size?: (string | null); additionalInfo?: (string | null); + imageUrl?: (string | null); id: number; }; diff --git a/src/components/ImageDropzone/ImageDropzone.tsx b/src/components/ImageDropzone/ImageDropzone.tsx index a78149e..5aaa35e 100644 --- a/src/components/ImageDropzone/ImageDropzone.tsx +++ b/src/components/ImageDropzone/ImageDropzone.tsx @@ -1,53 +1,101 @@ -import {Dropzone, DropzoneProps} from "@mantine/dropzone"; -import {FC} from "react"; -import {Group, rem, Text} from "@mantine/core"; +import {Dropzone, DropzoneProps, FileWithPath} from "@mantine/dropzone"; +import {FC, useState} from "react"; +import {Button, Fieldset, Flex, Group, Image, rem, Text} from "@mantine/core"; import {IconPhoto, IconUpload, IconX} from "@tabler/icons-react"; import {omit} from "lodash"; +import {BaseFormInputProps} from "../../types/utils.ts"; +import {notifications} from "../../shared/lib/notifications.ts"; +import {ProductService} from "../../client"; -type RestProps = { - imageUrl?: string; -}; -const ALL_PROPERTIES = Object.keys(colDefProperties) as (keyof ColDef)[]; +interface RestProps { + imageUrlInputProps?: BaseFormInputProps; + productId?: number; +} -type Props = DropzoneProps & RestProps; +type Props = Omit & RestProps; const ImageDropzone: FC = (props: Props) => { - const restProps = omit() - return ( - - - - - - - - - - - + const [showDropzone, setShowDropzone] = useState( + !(typeof props.imageUrlInputProps?.value === 'string' && + props.imageUrlInputProps.value.trim() !== '') + ); + console.log(props.imageUrlInputProps); + console.log(showDropzone); + const restProps = omit(props, ['imageUrl', 'productId', 'imageUrlInputProps']); + const onDrop = (files: FileWithPath[]) => { + if (!props.productId || !props.imageUrlInputProps) return; + if (files.length > 1) { + notifications.error({message: "Прикрепите одно изображение"}); + return; + } + const file = files[0]; + // TODO check if file is image + ProductService.uploadProductImage({ + productId: props.productId, + formData: { + upload_file: file + } + }).then(({ok, message, imageUrl}) => { + notifications.guess(ok, {message}); + if (!ok || !imageUrl) { + return; + } + props.imageUrlInputProps?.onChange(imageUrl); + setShowDropzone(false); + }); + } + const getBody = () => { + return props.imageUrlInputProps?.value && showDropzone ? ( + + ) : ( + - - Drag images here or click to select files - - - Attach as many files as you like, each file should not exceed 5mb - - - - + > + + + + + + + + + + + +
+ + Drag images here or click to select files + + + Attach as many files as you like, each file should not exceed 5mb + +
+
+ + ); + } + return ( + +
+ {getBody()} +
+ {!showDropzone && + + } +
) } diff --git a/src/pages/ProductsPage/modals/CreateProductModal/CreateProductModal.tsx b/src/pages/ProductsPage/modals/CreateProductModal/CreateProductModal.tsx index 93acd70..eb8a8d2 100644 --- a/src/pages/ProductsPage/modals/CreateProductModal/CreateProductModal.tsx +++ b/src/pages/ProductsPage/modals/CreateProductModal/CreateProductModal.tsx @@ -5,6 +5,7 @@ import {BaseProduct, CreateProductRequest} from "../../types.ts"; import {ProductSchema} from "../../../../client"; import BarcodeTemplateSelect from "../../../../components/Selects/BarcodeTemplateSelect/BarcodeTemplateSelect.tsx"; import ImageDropzone from "../../../../components/ImageDropzone/ImageDropzone.tsx"; +import {BaseFormInputProps} from "../../../../types/utils.ts"; type CreateProps = { clientId: number; @@ -25,12 +26,13 @@ const CreateProductModal = ({ }: ContextModalProps) => { const isEditProps = 'product' in innerProps; const isCreatingProps = 'clientId' in innerProps; - const initialValues = isEditProps ? innerProps.product : { + const initialValues: Omit = isEditProps ? innerProps.product : { name: '', article: '', - barcodes: [] + barcodes: [], + clientId: innerProps.clientId }; - const form = useForm({ + const form = useForm>({ initialValues: initialValues, validate: { name: (name) => name.trim() !== '' ? null : "Необходимо ввести название товара", @@ -99,11 +101,15 @@ const CreateProductModal = ({ label={"Доп. информация"} {...form.getInputProps('additionalInfo')} /> -
- { - }}/> -
+ {isEditProps && + //
+ } + productId={innerProps.product.id} + /> + //
+ } {isEditProps ?