reafactor code for reuasability

This commit is contained in:
Pramod Mahajan 2025-04-23 11:08:02 +05:30
parent c38a92b8b7
commit 38fbc6dc84

View File

@ -1,9 +1,38 @@
import React from "react";
import React, { useState, useEffect } from "react";
import moment from "moment";
import {useProjects} from "../../hooks/useProjects";
import { getProjectStatusName,getProjectStatusColor } from "../../utils/projectStatus";
import ProgressBar from "../../components/common/ProgressBar";
import {useNavigate} from "react-router-dom";
import ManageProject from "../../components/Project/ManageProject";
import ProjectRepository from "../../repositories/ProjectRepository";
import {MANAGE_PROJECT} from "../../utils/constants";
import { useHasUserPermission } from "../../hooks/useHasUserPermission";
import ManageProjectInfo from "../../components/Project/ManageProjectInfo";
import showToast from "../../services/toastService";
import { getCachedData,cacheData } from "../../slices/apiDataManager";
const ProjectListView = () => {
const { projects } = useProjects();
const ProjectListView = ( {projectsData, onStatusFilterChange} ) =>{
const [ projectDetails, setProjectDetails ] = useState( null );
const[projectInfo,setProjectInfo] = useState(null)
const [showModal, setShowModal] = useState(false);
const projects = Array.isArray(projectsData) ? projectsData : [];
const [ selectedStatuses, setSelectedStatuses ] = useState( [ 1, 2, 3, 4 ] );
const navigate = useNavigate()
const handleStatusChange = (statusId) => {
const updated = selectedStatuses.includes(statusId)
? selectedStatuses.filter((id) => id !== statusId)
: [...selectedStatuses, statusId];
setSelectedStatuses(updated);
onStatusFilterChange(updated);
};
useEffect(() => {
onStatusFilterChange(selectedStatuses);
}, []);
const getProgress = (planned, completed) => {
return (completed * 100) / planned + "%";
@ -11,39 +40,77 @@ const ProjectListView = () => {
const getProgressInNumber = (planned, completed) => {
return (completed * 100) / planned;
};
const filteredProjects = projects.filter((project) =>
selectedStatuses.includes(project.projectStatusId)
);
const getProjectStatusName = (statusId) => {
switch (statusId) {
case 1:
return "Active";
case 2:
return "On Hold";
// case 3:
// return "Suspended";
case 3:
return "Inactive";
case 4:
return "Completed";
const handleShow = async ( project) =>
{
setProjectInfo(project)
try {
const response = await ProjectRepository.getProjectByprojectId(
project.id
);
setProjectDetails(response.data);
setShowModal(true);
} catch (error) {
showToast("Failed to load project details", "error");
}
};
const handleClose = () => setShowModal(false);
const handleFormSubmit = (updatedProject,projectInfo) => {
if (projectInfo?.id) {
ProjectRepository.updateProject(projectInfo.id, updatedProject)
.then((response) => {
const updatedProjectData = {
...projectInfo,
...response.data,
building: projectDetails?.building,
};
setProjectInfo(updatedProject);
if (getCachedData(`projectinfo-${projectInfo.id}`)) {
cacheData(`projectinfo-${projectInfo.id}`, updatedProjectData);
}
const projects_list = getCachedData("projectslist");
if (projects_list) {
const updatedProjectsList = projects_list.map((project) =>
project.id === projectInfo.id
? { ...project, ...response.data, tenant: project.tenant }
: project
);
cacheData("projectslist", updatedProjectsList);
}
showToast("Project updated successfully.", "success");
setShowModal(false);
})
.catch((error) => {
showToast(error.message, "error");
});
}
};
const getProjectStatusColor = (statusId) => {
switch (statusId) {
case 1:
return "bg-label-success";
case 2:
return "bg-label-warning";
case 3:
return "bg-label-info";
case 4:
return "bg-label-secondary";
case 5:
return "bg-label-dark";
}
};
return (<>
return (
<div className="table-responsive text-nowrap py-2 overflow-auto">
{showModal && projectDetails && (
<div
className="modal fade show"
tabIndex="-1"
role="dialog"
style={{ display: "block" }}
aria-hidden="false"
>
<ManageProjectInfo
project={projectDetails}
handleSubmitForm={handleFormSubmit}
onClose={handleClose}
/>
</div>
)}
<div className="table-responsive text-nowrap py-2 ">
<table className="table px-2">
<thead>
<tr>
@ -56,140 +123,144 @@ const ProjectListView = () => {
<th className="mx-2">Task</th>
<th className="mx-2">Progress</th>
<th className="mx-2">
<div class="dropdown">
<div className="dropdown">
<a
class="dropdown-toggle hide-arrow cursor-pointer"
className="dropdown-toggle hide-arrow cursor-pointer"
data-bs-toggle="dropdown"
aria-expanded="false"
>
Status <i class="bx bx-filter"></i>
Status <i className="bx bx-filter"></i>
</a>
<ul class="dropdown-menu px-3 py-2">
<li>
<div class="form-check form-check-success mb-1">
<ul className="dropdown-menu p-2 text-capitalize">
{[
{ id: 1, label: "Active" },
{ id: 2, label: "On Hold" },
{ id: 3, label: "Inactive" },
{ id: 4, label: "Completed" },
].map(({ id, label }) => (
<li key={id}>
<div className="form-check">
<input
name="statusRadio"
class="form-check-input"
type="radio"
value="active"
id="statusActive"
checked
className="form-check-input "
type="checkbox"
checked={selectedStatuses.includes(id)}
onChange={() => handleStatusChange( id )}
/>
<label class="form-check-label text-success" for="statusActive">
Active
</label>
</div>
</li>
<li>
<div class="form-check form-check-warning mb-1">
<input
name="statusRadio"
class="form-check-input"
type="radio"
value="onhold"
id="statusOnHold"
checked
/>
<label class="form-check-label text-warning" for="statusOnHold">
On-Hold
</label>
</div>
</li>
<li>
<div class="form-check form-check-info mb-1">
<input
name="statusRadio"
class="form-check-input"
type="radio"
value="inactive"
id="statusInactive"
checked
/>
<label class="form-check-label text-info" for="statusInactive">
Inactive
</label>
</div>
</li>
<li>
<div class="form-check form-check-secondary mb-1">
<input
name="customRadioSecondary" class="form-check-input" type="radio" value="" id="customRadioSecondary" checked
/>
<label class="form-check-label text-secondary" for="statusCompleted">
Completed
</label>
<label className="form-check-label">{label}</label>
</div>
</li>
))}
</ul>
</div>
</th>
<th className="mx-2">Action</th>
</tr>
</thead>
<tbody className="table-border-bottom-0">
{projects.map((project) => (
<tr className="py-8">
<td className="text-start" colSpan={5}>
<strong className="text-primary"> {project.name}</strong>
<tbody
className="table-border-bottom-0 overflow-auto "
style={{ maxHeight: "50%", minHeight: "40%" }}
>
{filteredProjects.length === 0 ? (
<tr>
<td colSpan="12" className="text-center py-4">
No projects found
</td>
<td className="text-start">{project.contactPerson}</td>
</tr>
) : (
filteredProjects.map((project) => (
<tr key={project.id} className="py-8">
<td className="text-start" colSpan={5}>
<strong className="text-primary cursor-pointer" onClick={()=>navigate(`/projects/${project.id}`)}>{project.name}</strong>
</td>
<td className="text-start small">{project.contactPerson}</td>
<td className="small text-start">
<small>
{" "}
{project.startDate
? moment(project.startDate).format("DD-MMM-YYYY")
: "NA"}
</small>
</td>
<td className="mx-2 text-start">
<td className="mx-2 text-start small">
{project.endDate
? moment(project.endDate).format("DD-MMM-YYYY")
: "NA"}
</td>
<td className="mx-2 text-start">{project.plannedWork}</td>
<td className="py-6 mx-2 text-start">
<div
className="progress rounded align-items-center"
style={{ height: "8px" }}
>
<div
className="progress-bar rounded"
role="progressbar"
style={{
width: getProgress(
project.plannedWork,
project.completedWork
),
}}
aria-valuenow={project.completedWork}
aria-valuemin="0"
aria-valuemax={project.plannedWork}
></div>
</div>
<td className="mx-2 text-start small">{project.plannedWork}</td>
<td className="py-6 mx-2 text-start small align-items-center">
<ProgressBar plannedWork={project.plannedWork} completedWork={project.completedWork} className="mb-0" height="4px"/>
</td>
<td className="mx-6">
<p className="mb-0">
<span
className={
`badge ` + getProjectStatusColor(project.projectStatusId)
}
className={`badge ${getProjectStatusColor(
project.projectStatusId
)}`}
>
{getProjectStatusName(project.projectStatusId)}
</span>
</p>{" "}
</p>
</td>
<td className="mx-2">
{" "}
<i className="bx bx-dots-vertical-rounded bx-sm text-muted"></i>
<div className="dropdown z-2">
<button
type="button"
className="btn btn-icon btn-text-secondary rounded-pill dropdown-toggle hide-arrow p-0"
data-bs-toggle="dropdown"
aria-expanded="false"
>
<i
className="bx bx-dots-vertical-rounded bx-sm text-muted"
data-bs-toggle="tooltip"
data-bs-offset="0,8"
data-bs-placement="top"
data-bs-custom-class="tooltip-dark"
title="More Action"
></i>
</button>
<ul className="dropdown-menu dropdown-menu-end">
<li>
<a
aria-label="click to View details"
className="dropdown-item"
onClick={()=> navigate(`/projects/${project.id}`)}
>
<i className="bx bx-detail me-2"></i>
<span className="align-left">View details</span>
</a>
</li>
<li onClick={()=>handleShow(project)}>
<a className="dropdown-item">
<i className="bx bx-pencil me-2"></i>
<span className="align-left">Modify</span>
</a>
</li>
<li
onClick={() =>
navigate(
`/activities/records?project=${project.id}`
)
}
>
<a className="dropdown-item">
<i className="bx bx-task me-2"></i>
<span className="align-left">Activities</span>
</a>
</li>
</ul>
</div>
</td>
</tr>
))}
))
)}
</tbody>
</table>
</div>
</>
);
};