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 moment from "moment";
import { useProjects } from "../../hooks/useProjects"; 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 ProjectListView = ( {projectsData, onStatusFilterChange} ) =>{
const { projects } = useProjects(); 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) => { const getProgress = (planned, completed) => {
return (completed * 100) / planned + "%"; return (completed * 100) / planned + "%";
@ -11,39 +40,77 @@ const ProjectListView = () => {
const getProgressInNumber = (planned, completed) => { const getProgressInNumber = (planned, completed) => {
return (completed * 100) / planned; return (completed * 100) / planned;
}; };
const filteredProjects = projects.filter((project) =>
selectedStatuses.includes(project.projectStatusId)
);
const getProjectStatusName = (statusId) => { const handleShow = async ( project) =>
switch (statusId) { {
case 1: setProjectInfo(project)
return "Active"; try {
case 2: const response = await ProjectRepository.getProjectByprojectId(
return "On Hold"; project.id
// case 3: );
// return "Suspended"; setProjectDetails(response.data);
case 3: setShowModal(true);
return "Inactive"; } catch (error) {
case 4: showToast("Failed to load project details", "error");
return "Completed"; }
};
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) => { return (<>
switch (statusId) {
case 1: {showModal && projectDetails && (
return "bg-label-success"; <div
case 2: className="modal fade show"
return "bg-label-warning"; tabIndex="-1"
case 3: role="dialog"
return "bg-label-info"; style={{ display: "block" }}
case 4: aria-hidden="false"
return "bg-label-secondary"; >
case 5: <ManageProjectInfo
return "bg-label-dark"; project={projectDetails}
} handleSubmitForm={handleFormSubmit}
}; onClose={handleClose}
/>
return ( </div>
<div className="table-responsive text-nowrap py-2 overflow-auto"> )}
<div className="table-responsive text-nowrap py-2 ">
<table className="table px-2"> <table className="table px-2">
<thead> <thead>
<tr> <tr>
@ -56,140 +123,144 @@ const ProjectListView = () => {
<th className="mx-2">Task</th> <th className="mx-2">Task</th>
<th className="mx-2">Progress</th> <th className="mx-2">Progress</th>
<th className="mx-2"> <th className="mx-2">
<div class="dropdown"> <div className="dropdown">
<a <a
class="dropdown-toggle hide-arrow cursor-pointer" className="dropdown-toggle hide-arrow cursor-pointer"
data-bs-toggle="dropdown" data-bs-toggle="dropdown"
aria-expanded="false" aria-expanded="false"
> >
Status <i class="bx bx-filter"></i> Status <i className="bx bx-filter"></i>
</a> </a>
<ul class="dropdown-menu px-3 py-2"> <ul className="dropdown-menu p-2 text-capitalize">
<li> {[
<div class="form-check form-check-success mb-1"> { id: 1, label: "Active" },
<input { id: 2, label: "On Hold" },
name="statusRadio" { id: 3, label: "Inactive" },
class="form-check-input" { id: 4, label: "Completed" },
type="radio" ].map(({ id, label }) => (
value="active" <li key={id}>
id="statusActive" <div className="form-check">
checked <input
/> className="form-check-input "
<label class="form-check-label text-success" for="statusActive"> type="checkbox"
Active checked={selectedStatuses.includes(id)}
</label> onChange={() => handleStatusChange( id )}
</div>
</li> />
<li> <label className="form-check-label">{label}</label>
<div class="form-check form-check-warning mb-1"> </div>
<input </li>
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>
</div>
</li>
</ul> </ul>
</div> </div>
</th> </th>
<th className="mx-2">Action</th> <th className="mx-2">Action</th>
</tr> </tr>
</thead> </thead>
<tbody className="table-border-bottom-0"> <tbody
{projects.map((project) => ( className="table-border-bottom-0 overflow-auto "
<tr className="py-8"> style={{ maxHeight: "50%", minHeight: "40%" }}
<td className="text-start" colSpan={5}> >
<strong className="text-primary"> {project.name}</strong> {filteredProjects.length === 0 ? (
</td> <tr>
<td className="text-start">{project.contactPerson}</td> <td colSpan="12" className="text-center py-4">
<td className="small text-start"> No projects found
<small>
{" "}
{project.startDate
? moment(project.startDate).format("DD-MMM-YYYY")
: "NA"}
</small>
</td>
<td className="mx-2 text-start">
{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>
<td className="mx-6 ">
<p className="mb-0">
<span
className={
`badge ` + getProjectStatusColor(project.projectStatusId)
}
>
{getProjectStatusName(project.projectStatusId)}
</span>
</p>{" "}
</td>
<td className="mx-2 ">
{" "}
<i className="bx bx-dots-vertical-rounded bx-sm text-muted"></i>
</td> </td>
</tr> </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 small">
{project.endDate
? moment(project.endDate).format("DD-MMM-YYYY")
: "NA"}
</td>
<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
)}`}
>
{getProjectStatusName(project.projectStatusId)}
</span>
</p>
</td>
<td className="mx-2">
<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> </tbody>
</table> </table>
</div> </div>
</>
); );
}; };