diff --git a/src/ModalProvider.jsx b/src/ModalProvider.jsx index 26fd1821..bb217139 100644 --- a/src/ModalProvider.jsx +++ b/src/ModalProvider.jsx @@ -4,7 +4,7 @@ import OrganizationModal from "./components/Organization/OrganizationModal"; import { useAuthModal, useModal } from "./hooks/useAuth"; import SwitchTenant from "./pages/authentication/SwitchTenant"; import ChangePasswordPage from "./pages/authentication/ChangePassword"; -import NewCollection from "./components/collections/NewCollection"; +import NewCollection from "./components/collections/ManageCollection"; const ModalProvider = () => { const { isOpen, onClose } = useOrganizationModal(); diff --git a/src/components/collections/CollectionList.jsx b/src/components/collections/CollectionList.jsx index ad4c06ae..acfffe23 100644 --- a/src/components/collections/CollectionList.jsx +++ b/src/components/collections/CollectionList.jsx @@ -78,7 +78,7 @@ const CollectionList = ({ fromDate, toDate, isPending, searchString }) => { {formatUTCToLocalTime(col.createdAt)} ), - align: "text-start", + align: "text-center", }, { key: "expectedSubmittedDate", @@ -91,7 +91,7 @@ const CollectionList = ({ fromDate, toDate, isPending, searchString }) => { {formatUTCToLocalTime(col.exceptedPaymentDate) ?? "-"} ), - align: "text-start", + align: "text-center", }, { key: "amount", diff --git a/src/components/collections/Comment.jsx b/src/components/collections/Comment.jsx index 6edbedc1..11f09829 100644 --- a/src/components/collections/Comment.jsx +++ b/src/components/collections/Comment.jsx @@ -9,7 +9,7 @@ import moment from "moment"; const Comment = ({ invoice }) => { const { - register, + register,reset, handleSubmit, formState: { errors }, } = useForm({ @@ -17,7 +17,7 @@ const Comment = ({ invoice }) => { defaultValues: { comment: "" }, }); - const { mutate: AddComment, isPending } = useAddComment(() => {}); + const { mutate: AddComment, isPending } = useAddComment(() => {reset()}); const onSubmit = (formData) => { const payload = { ...formData, invoiceId: invoice?.id }; diff --git a/src/components/collections/NewCollection.jsx b/src/components/collections/ManageCollection.jsx similarity index 86% rename from src/components/collections/NewCollection.jsx rename to src/components/collections/ManageCollection.jsx index 8420f1b3..7f6b0ac6 100644 --- a/src/components/collections/NewCollection.jsx +++ b/src/components/collections/ManageCollection.jsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import { useModal } from "../../hooks/useAuth"; import Modal from "../common/Modal"; import { FormProvider, useForm } from "react-hook-form"; @@ -8,11 +8,17 @@ import { defaultCollection, newCollection } from "./collectionSchema"; import SelectMultiple from "../common/SelectMultiple"; import { useProjectName } from "../../hooks/useProjects"; import DatePicker from "../common/DatePicker"; -import { useCreateCollection } from "../../hooks/useCollections"; +import { + useCollection, + useCreateCollection, + useUpdateCollection, +} from "../../hooks/useCollections"; import { formatFileSize, localToUtc } from "../../utils/appUtils"; +import { useCollectionContext } from "../../pages/collections/CollectionPage"; +import { formatDate } from "../../utils/dateUtils"; -const NewCollection = ({ collectionId, onClose }) => { - +const ManageCollection = ({ collectionId, onClose }) => { + const { data, isError, isLoading, error } = useCollection(collectionId); const { projectNames, projectLoading } = useProjectName(); const methods = useForm({ resolver: zodResolver(newCollection), @@ -28,8 +34,11 @@ const NewCollection = ({ collectionId, onClose }) => { formState: { errors }, } = methods; - const { mutate: createNewCollection, isPending } = useCreateCollection(()=>{ - handleClose() + const { mutate: createNewCollection, isPending } = useCreateCollection(() => { + handleClose(); + }); + const { mutate: UpdateCollection } = useUpdateCollection(() => { + handleClose(); }); const files = watch("attachments"); @@ -100,17 +109,58 @@ const NewCollection = ({ collectionId, onClose }) => { invoiceDate: localToUtc(formData.invoiceDate), exceptedPaymentDate: localToUtc(formData.exceptedPaymentDate), }; - createNewCollection(payload); + + if (collectionId) { + UpdateCollection({ + collectionId, + payload: { ...payload, id: collectionId }, + }); + } else { + createNewCollection(payload); + } }; + const handleClose = () => { reset(defaultCollection); onClose(); }; + useEffect(() => { + if (data && collectionId) { + reset({ + projectId: data?.project?.id, + invoiceNumber: data?.invoiceNumber, + eInvoiceNumber: data?.eInvoiceNumber, + title: data?.title, + clientSubmitedDate: formatDate(data?.clientSubmitedDate), + invoiceDate: formatDate(data?.invoiceDate), + exceptedPaymentDate: formatDate(data?.exceptedPaymentDate), + taxAmount: data?.taxAmount, + basicAmount: data?.basicAmount, + description: data?.description, + attachments: data?.attachments, + attachments: data.attachments + ? data.attachments.map((doc) => ({ + fileName: doc.fileName, + base64Data: null, + contentType: doc.contentType, + documentId: doc.documentId, + fileSize: 0, + description: "", + preSignedUrl: doc.preSignedUrl, + isActive: doc.isActive ?? true, + })) + : [], + }); + } + }, [data]); + if (isLoading) return