From fd66cb8e2cf91fdffffad8c97a6221646940bdf6 Mon Sep 17 00:00:00 2001 From: Guilherme Baufaker Date: Tue, 16 Apr 2024 15:30:52 -0300 Subject: [PATCH] implemented association upload --- components/ui/AdminNewDocModal.tsx | 143 +++++++++++++++++++++++++++++ components/ui/AssociationAdmin.tsx | 20 +++- fresh.gen.ts | 2 + islands/AdminNewDocModal.tsx | 9 ++ sdk/useUI.ts | 7 ++ 5 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 components/ui/AdminNewDocModal.tsx create mode 100644 islands/AdminNewDocModal.tsx diff --git a/components/ui/AdminNewDocModal.tsx b/components/ui/AdminNewDocModal.tsx new file mode 100644 index 0000000..90eade3 --- /dev/null +++ b/components/ui/AdminNewDocModal.tsx @@ -0,0 +1,143 @@ +import { useUI } from "../../sdk/useUI.ts"; +import Modal from "./Modal.tsx"; +import { useState } from "preact/hooks"; +import { h } from "preact"; +import { invoke } from "../../runtime.ts"; +import Icon from "./Icon.tsx"; + +export interface Props { + createType: "association" | "user"; +} + +const AdminNewDocModal = ( + { createType }: Props, +) => { + const [file, setFile] = useState(); + const [docTitle, setDocTitle] = useState(""); + const [docCategory, setDocCategory] = useState(""); + const { displayAssociationAdminNewDoc, userToAdminCreateDoc } = useUI(); + const [isUploading, setIsUploading] = useState(false); + + const handleStoreDocument = ( + event: h.JSX.TargetedEvent, + ) => { + const fileInput = event.target as HTMLInputElement; + if (fileInput.files) { + setFile(fileInput.files[0]); + } + }; + + const handleCreate = async () => { + setIsUploading(true); + + const formData = new FormData(); + formData.append("user", userToAdminCreateDoc.value._id); + formData.append("file", file!); + formData.append("title", docTitle); + formData.append("category", docCategory); + + for (const entry of formData.entries()) { + console.log(entry[0], entry[1]); + } + + try { + const response = await fetch( + "http://localhost:3000/documents/admin/user", + { + method: "POST", + body: formData, + headers: { + ContentType: "multipart/form-data", + Authorization: localStorage.getItem("AccessToken") || "", + }, + }, + ); + + const res = await response.json(); + + console.log({ res }); + + const resp = res as { errors?: unknown[]; message?: string }; + + if (resp.errors || resp.message) { + if (resp.errors) { + throw new Error(resp.errors.toString()); + } else if (resp.message) { + throw new Error(resp.message); + } + } + + setIsUploading(false); + displayAssociationAdminNewDoc.value = false; + alert("Upload concluído com sucesso"); + } catch (e) { + alert("Ocorreu um erro ao subir o documento. Contacte o suporte"); + console.log({ erroUpload: e }); + setIsUploading(false); + } + }; + + return ( + displayAssociationAdminNewDoc.value = false} + > +
+

+ Novo documento para{" "}{createType === "user" + ? `paciente ${userToAdminCreateDoc.value.email}` + : `Associação`} +

+ { + setDocTitle(e.currentTarget.value); + }} + /> + + + + + + + +
+
+ ); +}; + +export default AdminNewDocModal; diff --git a/components/ui/AssociationAdmin.tsx b/components/ui/AssociationAdmin.tsx index 9b3a636..c9919db 100644 --- a/components/ui/AssociationAdmin.tsx +++ b/components/ui/AssociationAdmin.tsx @@ -7,6 +7,7 @@ import { useEffect, useState } from "preact/hooks"; import { invoke } from "../../runtime.ts"; import PageWrap from "../../components/ui/PageWrap.tsx"; import Icon from "../../components/ui/Icon.tsx"; +import AdminNewDocModal from "../../islands/AdminNewDocModal.tsx"; import PreSignupUsersModal from "../../islands/PreSignupUsersModal.tsx"; import type { AssociationUsers, @@ -37,7 +38,11 @@ function MyAccount() { const [page, setPage] = useState(); const [totalPages, setTotalPages] = useState(); const [associationUsers, setAssociationUsers] = useState(); - const { displayPreSignupUsersModal } = useUI(); + const { + displayPreSignupUsersModal, + displayAssociationAdminNewDoc, + userToAdminCreateDoc, + } = useUI(); const handleUploadSelfie = async ( event: h.JSX.TargetedEvent, @@ -291,6 +296,7 @@ function MyAccount() { console.log("on finish")} /> + {isLoadingUsers ? : ( @@ -349,7 +355,17 @@ function MyAccount() { Baixar QR Code
  • - Subir Documento + { + displayAssociationAdminNewDoc.value = true; + userToAdminCreateDoc.value = { + _id: u._id, + email: u.email, + }; + }} + > + Subir Documento +
  • diff --git a/fresh.gen.ts b/fresh.gen.ts index 5ea5a6e..0ebb242 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -9,6 +9,7 @@ import * as $AddToCartButton_shopify from "./islands/AddToCartButton/shopify.tsx import * as $AddToCartButton_vnda from "./islands/AddToCartButton/vnda.tsx"; import * as $AddToCartButton_vtex from "./islands/AddToCartButton/vtex.tsx"; import * as $AddToCartButton_wake from "./islands/AddToCartButton/wake.tsx"; +import * as $AdminNewDocModal from "./islands/AdminNewDocModal.tsx"; import * as $AssociationAdmin from "./islands/AssociationAdmin.tsx"; import * as $CheckoutForm from "./islands/CheckoutForm.tsx"; import * as $CheckoutUpsellModal from "./islands/CheckoutUpsellModal.tsx"; @@ -61,6 +62,7 @@ const manifest = { "./islands/AddToCartButton/vnda.tsx": $AddToCartButton_vnda, "./islands/AddToCartButton/vtex.tsx": $AddToCartButton_vtex, "./islands/AddToCartButton/wake.tsx": $AddToCartButton_wake, + "./islands/AdminNewDocModal.tsx": $AdminNewDocModal, "./islands/AssociationAdmin.tsx": $AssociationAdmin, "./islands/CheckoutForm.tsx": $CheckoutForm, "./islands/CheckoutUpsellModal.tsx": $CheckoutUpsellModal, diff --git a/islands/AdminNewDocModal.tsx b/islands/AdminNewDocModal.tsx new file mode 100644 index 0000000..720b503 --- /dev/null +++ b/islands/AdminNewDocModal.tsx @@ -0,0 +1,9 @@ +import Component from "deco-sites/ecannadeco/components/ui/AdminNewDocModal.tsx"; +import type { Props } from "deco-sites/ecannadeco/components/ui/AdminNewDocModal.tsx"; + +function Island(props: Props) { + // return ; + return ; +} + +export default Island; diff --git a/sdk/useUI.ts b/sdk/useUI.ts index f237bdc..d28e682 100644 --- a/sdk/useUI.ts +++ b/sdk/useUI.ts @@ -22,10 +22,15 @@ const displayConfirmDeleteDoc = signal(false); const displayConfirmCancelSubscription = signal(false); const displayCheckoutUpsellModal = signal(false); const displayPreSignupUsersModal = signal(false); +const displayAssociationAdminNewDoc = signal(false); const displayPlanLimit = signal(false); const updatedData = signal(true); const uploadedFile = signal(true); const user = signal(null); +const userToAdminCreateDoc = signal({ + email: "", + _id: "", +}); const state = { displayCart, @@ -41,6 +46,8 @@ const state = { displayConfirmCancelSubscription, displayCheckoutUpsellModal, displayPreSignupUsersModal, + displayAssociationAdminNewDoc, + userToAdminCreateDoc, user, };