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
Loading....
; + if (isError) return
{error.message}
; return (
- -
New Collection
+
+ {collectionId ? "Update" : "New"} Collection +
@@ -170,7 +220,6 @@ const NewCollection = ({ collectionId, onClose }) => { name="exceptedPaymentDate" control={control} minDate={watch("invoiceDate")} - /> {errors.exceptedPaymentDate && ( @@ -183,7 +232,6 @@ const NewCollection = ({ collectionId, onClose }) => { {errors.exceptedPaymentDate && ( @@ -282,9 +330,7 @@ const NewCollection = ({ collectionId, onClose }) => {
- document.getElementById("attachments").click() - } + onClick={() => document.getElementById("attachments").click()} > @@ -382,11 +428,7 @@ const NewCollection = ({ collectionId, onClose }) => {
- ); - - - }; -export default NewCollection; +export default ManageCollection; diff --git a/src/components/collections/PaymentHistoryTable.jsx b/src/components/collections/PaymentHistoryTable.jsx index 6ce11909..cc19bcba 100644 --- a/src/components/collections/PaymentHistoryTable.jsx +++ b/src/components/collections/PaymentHistoryTable.jsx @@ -9,7 +9,6 @@ const PaymentHistoryTable = ({data}) => { {data?.receivedInvoicePayments?.length > 0 && (
-

Received Payments

diff --git a/src/components/collections/ViewCollection.jsx b/src/components/collections/ViewCollection.jsx index c86fe308..be1749fe 100644 --- a/src/components/collections/ViewCollection.jsx +++ b/src/components/collections/ViewCollection.jsx @@ -2,16 +2,23 @@ import React from "react"; import { useCollectionContext } from "../../pages/collections/CollectionPage"; import { useCollection } from "../../hooks/useCollections"; import { formatUTCToLocalTime } from "../../utils/dateUtils"; -import { formatFigure } from "../../utils/appUtils"; +import { formatFigure, getIconByFileType } from "../../utils/appUtils"; import Avatar from "../common/Avatar"; import PaymentHistoryTable from "./PaymentHistoryTable"; import Comment from "./Comment"; -const ViewCollection = () => { - const { viewCollection } = useCollectionContext(); +const ViewCollection = ({ onClose }) => { + const { viewCollection, setCollection , setDocumentView} = useCollectionContext(); const { data, isLoading, isError, error } = useCollection(viewCollection); + + const handleEdit = () => { + setCollection({ isOpen: true, invoiceId: viewCollection }); + onClose(); + }; + if (isLoading) return
isLoading...
; if (isError) return
{error.message}
; + return (

Collection Details

@@ -21,19 +28,26 @@ const ViewCollection = () => {

{data?.title}

- + {data?.isActive ? "Active" : "Inactive"} + {!data?.receivedInvoicePayments && ( + + )}
-
+

Project:

{" "} {data?.project?.name}
-
+

Invoice Number:

{" "} {data?.invoiceNumber}
@@ -44,7 +58,7 @@ const ViewCollection = () => {
-
+

Invoice Date:

{" "} {formatUTCToLocalTime(data?.invoiceDate)}
@@ -55,7 +69,7 @@ const ViewCollection = () => {
-
+

Expected Payment Date:

{" "} {formatUTCToLocalTime(data?.exceptedPaymentDate)}
@@ -66,7 +80,7 @@ const ViewCollection = () => {
-
+

Basic Amount:

{" "} {formatFigure(data?.basicAmount, { type: "currency", @@ -83,7 +97,7 @@ const ViewCollection = () => {
-
+

Balance Amount:

{" "} {formatFigure(data?.balanceAmount, { type: "currency", @@ -97,7 +111,7 @@ const ViewCollection = () => {
-
+

Created By:

{" "}
{ {data?.description}
+
+ + +
+ {data?.attachments?.map((doc) => { + const isImage = doc.contentType?.includes("image"); + + return ( +
{ + if (isImage) { + setDocumentView({ + IsOpen: true, + Image: doc.preSignedUrl, + }); + } + }} + > + + + {doc.fileName} + +
+ ); + })} +
+
{/* Tabs Navigation */} @@ -127,7 +182,8 @@ const ViewCollection = () => { type="button" role="tab" > - Comments ({data?.comments?.length ?? '0'}) + Comments ( + {data?.comments?.length ?? "0"})
  • @@ -139,7 +195,7 @@ const ViewCollection = () => { type="button" role="tab" > - Payments History + Payments History
  • @@ -150,8 +206,7 @@ const ViewCollection = () => { id="details" role="tabpanel" > - - +
    {/* Payments History Tab Content */} diff --git a/src/hooks/useCollections.js b/src/hooks/useCollections.js index fd4e51bb..07da685a 100644 --- a/src/hooks/useCollections.js +++ b/src/hooks/useCollections.js @@ -128,4 +128,22 @@ export const useAddComment = (onSuccessCallBack) => { ); }, }); -}; \ No newline at end of file +}; + +export const useUpdateCollection =(onSuccessCallBack)=>{ + const client = useQueryClient(); + return useMutation({ + mutationFn:async({collectionId,payload})=> await CollectionRepository.updateCollection(collectionId,payload), + onSuccess: () => { + client.invalidateQueries({ queryKey: ["collections"] }); + client.invalidateQueries({ queryKey: ["collection"] }); + showToast("Collection Updated Successfully", "success"); + if (onSuccessCallBack) onSuccessCallBack(); + }, + onError: (error) => { + showToast( + error?.response?.data?.message || error.message || "Something Went wrong" + ); + }, + }) +} \ No newline at end of file diff --git a/src/pages/collections/CollectionPage.jsx b/src/pages/collections/CollectionPage.jsx index 964cc108..126080a5 100644 --- a/src/pages/collections/CollectionPage.jsx +++ b/src/pages/collections/CollectionPage.jsx @@ -10,10 +10,11 @@ import { isPending } from "@reduxjs/toolkit"; import ConfirmModal from "../../components/common/ConfirmModal"; import showToast from "../../services/toastService"; import { useMarkedPaymentReceived } from "../../hooks/useCollections"; -import NewCollection from "../../components/collections/NewCollection"; import GlobalModel from "../../components/common/GlobalModel"; import AddPayment from "../../components/collections/AddPayment"; import ViewCollection from "../../components/collections/ViewCollection"; +import ManageCollection from "../../components/collections/ManageCollection"; +import PreviewDocument from "../../components/Expenses/PreviewDocument"; const CollectionContext = createContext(); export const useCollectionContext = () => { @@ -30,6 +31,10 @@ const CollectionPage = () => { isOpen: false, invoiceId: null, }); + const [ViewDocument, setDocumentView] = useState({ + IsOpen: false, + Image: null, + }); const [processedPayment, setProcessedPayment] = useState(null); const [addPayment, setAddPayment] = useState({ isOpen: false, @@ -49,10 +54,12 @@ const CollectionPage = () => { const contextMassager = { setProcessedPayment, + setCollection, setAddPayment, addPayment, setViewCollection, - viewCollection + viewCollection, + setDocumentView }; const { mutate: MarkedReceived, isPending } = useMarkedPaymentReceived(() => { setProcessedPayment(null); @@ -129,8 +136,8 @@ const CollectionPage = () => { size="lg" closeModal={() => setCollection({ isOpen: false, invoiceId: null })} > - setCollection({ isOpen: false, invoiceId: null })} /> @@ -154,6 +161,16 @@ const CollectionPage = () => { )} + {ViewDocument.IsOpen && ( + setDocumentView({ IsOpen: false, Image: null })} + > + + )} + api.post(`/api/Collection/invoice/create`, data), + updateCollection:(id,data)=>{ + api.put(`/api/Collection/invoice/edit/${id}`,data) + }, getCollections: (pageSize, pageNumber,fromDate,toDate, isPending,isActive, searchString) => { let url = `/api/Collection/invoice/list?pageSize=${pageSize}&pageNumber=${pageNumber}&isPending=${isPending}&isActive=${isActive}&searchString=${searchString}`;