Refactor_Expenses #321
@ -1,5 +1,9 @@
|
|||||||
import React, { useState, useMemo } from "react";
|
import React, { useState, useMemo } from "react";
|
||||||
import { useActionOnExpense, useExpense, useHasAnyPermission } from "../../hooks/useExpense";
|
import {
|
||||||
|
useActionOnExpense,
|
||||||
|
useExpense,
|
||||||
|
useHasAnyPermission,
|
||||||
|
} from "../../hooks/useExpense";
|
||||||
import { formatUTCToLocalTime } from "../../utils/dateUtils";
|
import { formatUTCToLocalTime } from "../../utils/dateUtils";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
@ -11,12 +15,14 @@ import { useHasUserPermission } from "../../hooks/useHasUserPermission";
|
|||||||
import { REVIEW_EXPENSE } from "../../utils/constants";
|
import { REVIEW_EXPENSE } from "../../utils/constants";
|
||||||
import { useProfile } from "../../hooks/useProfile";
|
import { useProfile } from "../../hooks/useProfile";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
const ViewExpense = ({ ExpenseId }) => {
|
const ViewExpense = ({ ExpenseId }) => {
|
||||||
const { data, isLoading, isError, error } = useExpense(ExpenseId);
|
const { data, isLoading, isError, error } = useExpense(ExpenseId);
|
||||||
const IsReview = useHasUserPermission(REVIEW_EXPENSE);
|
const IsReview = useHasUserPermission(REVIEW_EXPENSE);
|
||||||
const [imageLoaded, setImageLoaded] = useState({});
|
const [imageLoaded, setImageLoaded] = useState({});
|
||||||
const { setDocumentView } = useExpenseContext();
|
const { setDocumentView } = useExpenseContext();
|
||||||
|
const navigate = useNavigate();
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
@ -31,12 +37,11 @@ const ViewExpense = ({ ExpenseId }) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const userPermissions = useSelector(
|
const userPermissions = useSelector(
|
||||||
(state) => state?.globalVariables?.loginUser?.featurePermissions || []
|
(state) => state?.globalVariables?.loginUser?.featurePermissions || []
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const nextStatusWithPermission = useMemo(() => {
|
||||||
const nextStatusWithPermission = useMemo(() => {
|
|
||||||
if (!Array.isArray(data?.nextStatus)) return [];
|
if (!Array.isArray(data?.nextStatus)) return [];
|
||||||
|
|
||||||
return data.nextStatus.filter((status) => {
|
return data.nextStatus.filter((status) => {
|
||||||
@ -48,9 +53,7 @@ const nextStatusWithPermission = useMemo(() => {
|
|||||||
|
|
||||||
return permissionIds.some((id) => userPermissions.includes(id));
|
return permissionIds.some((id) => userPermissions.includes(id));
|
||||||
});
|
});
|
||||||
}, [data, userPermissions]);
|
}, [data, userPermissions]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const { mutate: MakeAction } = useActionOnExpense(() => reset());
|
const { mutate: MakeAction } = useActionOnExpense(() => reset());
|
||||||
|
|
||||||
@ -189,12 +192,27 @@ const nextStatusWithPermission = useMemo(() => {
|
|||||||
<label className="form-label me-2 mb-0 fw-semibold">Description:</label>
|
<label className="form-label me-2 mb-0 fw-semibold">Description:</label>
|
||||||
<div className="text-muted">{data?.description}</div>
|
<div className="text-muted">{data?.description}</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="col-12 my-2 text-start ">
|
<div className="col-12 my-2 text-start">
|
||||||
<label className="form-label me-2 mb-0 fw-semibold">
|
<label className="form-label me-2 mb-0 fw-semibold">Attachment:</label>
|
||||||
Attachement :
|
|
||||||
</label>
|
{data?.documents?.map((doc) => {
|
||||||
{data?.documents &&
|
const getIconByType = (type) => {
|
||||||
data?.documents?.map((doc) => (
|
if (!type) return "bx bx-file";
|
||||||
|
|
||||||
|
if (type.includes("pdf")) return "bxs-file-pdf";
|
||||||
|
if (type.includes("word")) return "bxs-file-doc";
|
||||||
|
if (type.includes("excel") || type.includes("spreadsheet"))
|
||||||
|
return "bxs-file-xls";
|
||||||
|
if (type.includes("image")) return "bxs-file-image";
|
||||||
|
if (type.includes("zip") || type.includes("rar"))
|
||||||
|
return "bxs-file-archive";
|
||||||
|
|
||||||
|
return "bx bx-file"; // Default
|
||||||
|
};
|
||||||
|
|
||||||
|
const isImage = doc.contentType?.includes("image");
|
||||||
|
|
||||||
|
return (
|
||||||
<div
|
<div
|
||||||
className="list-group-item list-group-item-action d-flex align-items-center"
|
className="list-group-item list-group-item-action d-flex align-items-center"
|
||||||
key={doc.id}
|
key={doc.id}
|
||||||
@ -202,52 +220,37 @@ const nextStatusWithPermission = useMemo(() => {
|
|||||||
<div
|
<div
|
||||||
className="rounded me-1 d-flex align-items-center justify-content-center cursor-pointer"
|
className="rounded me-1 d-flex align-items-center justify-content-center cursor-pointer"
|
||||||
style={{ height: "50px", width: "80px", position: "relative" }}
|
style={{ height: "50px", width: "80px", position: "relative" }}
|
||||||
>
|
onClick={() => {
|
||||||
{doc.contentType === "application/pdf" ? (
|
if (isImage) {
|
||||||
<div>
|
|
||||||
<i
|
|
||||||
className="bx bxs-file-pdf"
|
|
||||||
style={{ fontSize: "45px" }}
|
|
||||||
></i>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
{!imageLoaded[doc.id] && (
|
|
||||||
<div className="position-absolute text-secondary">
|
|
||||||
Loading...
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<img
|
|
||||||
src={doc.thumbPreSignedUrl}
|
|
||||||
alt={doc.fileName}
|
|
||||||
className="img-fluid rounded"
|
|
||||||
style={{
|
|
||||||
maxHeight: "100%",
|
|
||||||
maxWidth: "100%",
|
|
||||||
objectFit: "cover",
|
|
||||||
opacity: imageLoaded[doc.id] ? 1 : 0,
|
|
||||||
transition: "opacity 0.3s ease-in-out",
|
|
||||||
}}
|
|
||||||
onLoad={() => handleImageLoad(doc.id)}
|
|
||||||
onClick={() =>
|
|
||||||
setDocumentView({
|
setDocumentView({
|
||||||
IsOpen: true,
|
IsOpen: true,
|
||||||
Image: doc.preSignedUrl,
|
Image: doc.preSignedUrl,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
/>
|
}}
|
||||||
</>
|
>
|
||||||
)}
|
<i
|
||||||
|
className={`bx ${getIconByType(
|
||||||
|
doc.contentType
|
||||||
|
)} text-primary`}
|
||||||
|
style={{ fontSize: "45px" }}
|
||||||
|
></i>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="w-100">
|
<div className="w-100">
|
||||||
<small className="mb-0 small">{doc.fileName}</small>
|
<small className="mb-0 small">{doc.fileName}</small>
|
||||||
<div className="d">
|
<div className="d">
|
||||||
<i className="bx bx-cloud-download cursor-pointer"></i>
|
<a
|
||||||
|
href={doc.preSignedUrl}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="bx bx-cloud-download cursor-pointer"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
);
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr className="divider my-1" />
|
<hr className="divider my-1" />
|
||||||
@ -285,8 +288,7 @@ const nextStatusWithPermission = useMemo(() => {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user