From f8a00121b0b4e9387ba8c23a9168609e6d0c9641 Mon Sep 17 00:00:00 2001 From: Eduardo Oliveira Date: Sun, 10 Dec 2023 18:58:01 +0000 Subject: [PATCH] Implemented add asset to project #3 #10 --- .../parts/add-asset/AddAsset.tsx | 57 +++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/projects/components/project-page/parts/project-page-body/parts/add-asset/AddAsset.tsx b/src/projects/components/project-page/parts/project-page-body/parts/add-asset/AddAsset.tsx index f1d5f00..ab1b08c 100644 --- a/src/projects/components/project-page/parts/project-page-body/parts/add-asset/AddAsset.tsx +++ b/src/projects/components/project-page/parts/project-page-body/parts/add-asset/AddAsset.tsx @@ -1,51 +1,74 @@ -import {Dropzone} from "@mantine/dropzone"; -import {Container, Group, rem, Text} from "@mantine/core"; -import {IconPhoto, IconUpload, IconX} from "@tabler/icons-react"; +import { Dropzone } from "@mantine/dropzone"; +import { Container, Group, rem, Text } from "@mantine/core"; +import { IconPhoto, IconUpload, IconX } from "@tabler/icons-react"; import useAxios from "axios-hooks"; import { useContext } from "react"; import { SettingsContext } from "@/core/utils/settingsContext"; +import { notifications } from "@mantine/notifications"; type AddAssetProps = { projectUuid: string } -export function AddAsset({projectUuid}: AddAssetProps) { - const {local_backend} = useContext(SettingsContext); - const [{loading: saving, error}, executeSave] = useAxios( +export function AddAsset({ projectUuid }: AddAssetProps) { + const { local_backend } = useContext(SettingsContext); + const [{ loading }, executeSave] = useAxios( { - url: `${local_backend}/projects`, + url: `${local_backend}/assets`, method: 'POST' }, - {manual: true} + { + manual: true, + autoCancel: false + } ) const onDrop = (files: File[]) => { console.log(files); - const formData = new FormData(); - formData.append("project", projectUuid); - files.forEach((file) => formData.append("files", file)); - executeSave({data: formData}); + for (const i in files) { + const formData = new FormData(); + formData.append("project_uuid", projectUuid); + formData.append("files", files[i]); + executeSave({ data: formData }) + .then(({ data }) => { + console.log(data); + notifications.show({ + title: 'Great Success!', + message: `${data.name} as added to your project!`, + color: 'indigo', + }) + }) + .catch(({ message }) => { + console.log(message) + notifications.show({ + title: 'Ops... Error adding asset!', + message, + color: 'red', + autoClose: false + }) + }); + } }; return ( <> - - + +