Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ const AddProjectPopup = React.memo(props => {
await dispatch(assignProject(selectedProject._id, props.userId, 'Assign'));
// ✅ Make sure we're passing the complete project object
props.onSelectAssignProject?.(selectedProject);
toast.success(`Assigned to "${selectedProject.projectName}".`);
props.onClose?.();
} catch (e) {
// eslint-disable-next-line no-console
Expand Down
68 changes: 44 additions & 24 deletions src/components/UserProfile/TeamsAndProjects/UserProjectsTable.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, useEffect, useMemo } from 'react';
import { Button, Col, Tooltip , NavItem, UncontrolledTooltip } from 'reactstrap';
import PropTypes from "prop-types";
import { Button, Col, UncontrolledTooltip } from 'reactstrap';
import './TeamsAndProjects.css';
import hasPermission from '../../../utils/permissions';
// import styles from './UserProjectsTable.css';
Expand All @@ -18,7 +19,7 @@
return fallback;
};

const [tooltipOpen, setTooltip] = useState(false);

Check warning on line 22 in src/components/UserProfile/TeamsAndProjects/UserProjectsTable.jsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this useless assignment to variable "setTooltip".

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZrgt2y4gX_j7GZ0Vwr1&open=AZrgt2y4gX_j7GZ0Vwr1&pullRequest=4453

Check warning on line 22 in src/components/UserProfile/TeamsAndProjects/UserProjectsTable.jsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this useless assignment to variable "tooltipOpen".

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZrgt2y4gX_j7GZ0Vwrz&open=AZrgt2y4gX_j7GZ0Vwrz&pullRequest=4453

Check warning on line 22 in src/components/UserProfile/TeamsAndProjects/UserProjectsTable.jsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the declaration of the unused 'setTooltip' variable.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZrgt2y4gX_j7GZ0Vwr0&open=AZrgt2y4gX_j7GZ0Vwr0&pullRequest=4453

Check warning on line 22 in src/components/UserProfile/TeamsAndProjects/UserProjectsTable.jsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the declaration of the unused 'tooltipOpen' variable.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZrgt2y4gX_j7GZ0Vwry&open=AZrgt2y4gX_j7GZ0Vwry&pullRequest=4453
const canAssignProjectToUsers = props.hasPermission('assignProjectToUsers');
const canUpdateTask = props.hasPermission('updateTask');
const canDeleteProjects = props.hasPermission('deleteProject');
Expand All @@ -33,7 +34,7 @@
const currentRoute = location.pathname;
const isUserProfilePage = currentRoute === '/usermanagement';

const toggleTooltip = () => setTooltip(!tooltipOpen);
// const toggleTooltip = () => setTooltip(!tooltipOpen);

Check warning on line 37 in src/components/UserProfile/TeamsAndProjects/UserProjectsTable.jsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZrgt2y4gX_j7GZ0Vwr2&open=AZrgt2y4gX_j7GZ0Vwr2&pullRequest=4453

//Situation can be all, active, or complete
const filterTasksAndUpdateFilter = situation => {
Expand All @@ -42,7 +43,7 @@

const sortedTasksByNumber = useMemo(() => {
return userTasks?.sort((task1, task2) => task1.num - task2.num);
}, [userTasks]);
}, [userTasks]);

const tasksByProject = userProjects?.map(project => {
const tasks = sortedTasksByNumber?.filter(task => task.projectId?.includes(project.projectId));
Expand Down Expand Up @@ -81,34 +82,43 @@
setFilteredTasks(() => filterTasksByUserTaskSituation(actualType));
}, [sortedTasksByNumber, actualType]);

const removeOrAddTaskFromUser = (task, method) => {
const newResources = task.resources?.map(resource => {
if (resource.userID === props.userId) {
return {
...resource,
completedTask: method === 'remove'
};
const removeOrAddTaskFromUser = (task, method) => {

let newResources = task.resources;

if (method === "remove") {
// REMOVE the user completely
newResources = task.resources.filter(r => r.userID !== props.userId);
} else if (method === "add") {
// RE-ADD user as uncompleted resource
newResources = [
...task.resources,
{
userID: props.userId,
completedTask: method === 'remove',
reviewStatus: "Unsubmitted",
startedDatetime: new Date().toISOString()
}
return resource;
});
const updatedTask = {
...task,
];
}

const updatedTask = {
...task,
resources: newResources,
status: method === 'remove' ? 'Complete' : 'Started'
};

props.updateTask(task._id, updatedTask, method);
};

props.updateTask(task._id, updatedTask, method);
};

//For updating tasks visually but not saving until user clicks save changes
const deleteTasksTemporarily = (project_id) => {
setFilteredTasks(filteredTasks?.filter(project => project.projectId !== project_id ));
}

useEffect(()=>{
setFilteredTasks(() => filterTasksByUserTaskSituation('active'));
}, [props.userProjectsById])
}, [props.userProjectsById], filteredTasks);


return (
Expand All @@ -126,9 +136,9 @@
<Col md="4" className='p-0'>
{props.disabled ? (
<>
<Tooltip placement="bottom" isOpen={tooltipOpen} target="btn-assignproject" toggle={toggleTooltip}>
{/* <Tooltip placement="bottom" isOpen={tooltipOpen} target="btn-assignproject" toggle={toggleTooltip}>
Please save changes before assign project
</Tooltip>
</Tooltip> */}
<Button className="btn-addproject mt-2" id="btn-assignproject" color="primary" style={darkMode ? boxStyleDark : boxStyle} disabled>
Assign Project
</Button>
Expand Down Expand Up @@ -380,7 +390,7 @@
</thead>
<tbody>
{props.userProjectsById.length > 0 ? (
tasksByProject?.map((project, index) => (
filteredTasks?.map((project, index) => (
<tr key={project.projectId}>
<td>{index + 1}</td>
<td className="taskName">{`${project.projectName}`}</td>
Expand All @@ -391,7 +401,6 @@
disabled={!canUpdateTask}
onClick={e => {
props.onDeleteClick(project.projectId);
deleteTasksTemporarily(project.projectId);
}}
style={darkMode ? boxStyleDark : boxStyle}
>
Expand Down Expand Up @@ -513,4 +522,15 @@
);
});

export default connect(null, { hasPermission })(UserProjectsTable);
export default connect(null, { hasPermission })(UserProjectsTable);
UserProjectsTable.propTypes = {
userId: PropTypes.string,
userProjectsById: PropTypes.array,
userTasks: PropTypes.array,
role: PropTypes.string,
edit: PropTypes.bool,
hasPermission: PropTypes.func,
onDeleteClick: PropTypes.func,
updateTask: PropTypes.func,
darkMode: PropTypes.bool,
};
177 changes: 109 additions & 68 deletions src/components/UserProfile/UserProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -555,62 +555,111 @@ function UserProfile(props) {
setTeams(prevTeams => prevTeams.filter(team => team._id !== deletedTeamId));
};

const onDeleteProject = deletedProjectId => {
setProjects(prevProject => prevProject.filter(project => project._id !== deletedProjectId));
const onDeleteProject = async (deletedProjectId) => {

const removedProject = projects.find(p => (p._id || p.projectId) === deletedProjectId);

const updatedProjects = projects.filter(p => {
return (p._id || p.projectId) !== deletedProjectId;
});

setProjects(updatedProjects);

// Prepare backend payload
const updatedUserProfile = {
...userProfileRef.current,
projects: updatedProjects.map(p => String(p._id || p.projectId)),
};

try {
await handleSubmit(updatedUserProfile); // this already toasts success
toast.success(`User removed from Project "${removedProject?.projectName || 'Unknown'}"`);
} catch (e) {
toast.error('Failed to remove project, please try again.');
console.error(e);
}
return updatedProjects;
};


const onAssignTeam = assignedTeam => {
setTeams(prevState => [...prevState, assignedTeam]);
};

const onAssignProject = assignedProject => {
// eslint-disable-next-line no-console
console.log("Adding project to state:", assignedProject);
const onAssignProject = async (assignedProject) => {
const projectId = assignedProject._id || assignedProject.projectId;

// Avoid duplicates
const currentProjects = Array.isArray(projects) ? projects : [];
if (currentProjects.some(p => (p._id || p.projectId) === projectId)) {
toast.info(`Project "${assignedProject.projectName || 'Unknown'}" already assigned to this user`);
return;
}

const updatedProjects = [...currentProjects, assignedProject];
setProjects(updatedProjects);

const updatedUserProfile = {
...userProfileRef.current,
projects: updatedProjects.map(p => String(p._id || p.projectId)),
};

try {
await handleSubmit(updatedUserProfile); // reuses same pipeline
toast.success(`User assigned to Project "${assignedProject.projectName || 'Unknown'}"`);
} catch (e) {
toast.error('Failed to assign project, please try again.');
console.error(e);
}
return updatedProjects;
};

const onUpdateTask = async (taskId, updatedTask, method) => {

// Always create a new array to trigger React re-render
setProjects(prevProjects =>
{
// Ensure prevProjects is an array
const currentProjects = Array.isArray(prevProjects) ? prevProjects : [];

if (currentProjects.some(proj => proj._id === assignedProject._id)) {
// eslint-disable-next-line no-console
console.log("Project already exists, not adding duplicate");
return currentProjects;
let newTasks;

if (method === 'remove') {
try{
newTasks = tasks.filter(t => t._id !== taskId);
const res = await axios.delete(ENDPOINTS.TASK_DELETE_BY_ID(taskId, userProfile._id));
if (res.status === 200) {
setTasks(newTasks);
toast.success('Task removed successfully');
} else {
toast.error('Failed to remove task');
}

// Add project and log the new state
// eslint-disable-next-line no-console
console.log("Adding new project:", assignedProject.projectName);
const newProjects = [...currentProjects, assignedProject];
// eslint-disable-next-line no-console
console.log("Updated projects state:", newProjects);
return newProjects; // Return the new array with the project added
});
return newTasks;
} catch (e) {
toast.error('Failed to remove task, please try again.');
console.error(e);
return tasks;
}
} else {
// UPDATE the task normally
newTasks = tasks.map(t =>
t._id === taskId ? updatedTask : t
);
}
setTasks(newTasks);

const updatedUserProfile = {
...userProfileRef.current,
tasks: newTasks
};
const onUpdateTask = (taskId, updatedTask) => {
const newTask = {
updatedTask,
taskId,
};

setTasks(tasks => {
const tasksWithoutTheUpdated = [...tasks];
const taskIndex = tasks.findIndex(task => task._id === taskId);
tasksWithoutTheUpdated[taskIndex] = updatedTask;
return tasksWithoutTheUpdated;
});
setUpdatedTasks(prev => {
const others = prev.filter(t => t.taskId !== taskId);
return [...others, { taskId, updatedTask }];
});

if (updatedTasks.findIndex(task => task.taskId === taskId) !== -1) {
const taskIndex = updatedTasks.findIndex(task => task.taskId === taskId);
const tasksToUpdate = updatedTasks;
tasksToUpdate.splice(taskIndex, 1);
tasksToUpdate.splice(taskIndex, 0, newTask);
setUpdatedTasks(tasksToUpdate);
} else {
setUpdatedTasks(tasks => [...tasks, newTask]);
}
};
try {
await handleSubmit(updatedUserProfile);
toast.success("Task updated");
} catch (e) {
toast.error("Failed to update task");
console.error(e);
}
};

const handleImageUpload = async evt => {
if (evt) evt.preventDefault();
Expand Down Expand Up @@ -1382,15 +1431,6 @@ const onAssignProject = assignedProject => {
</div>

<div className="right-column">
{/* }
{!isProfileEqual ||
!isTasksEqual ||
!isProjectsEqual ? (
<Alert color="warning">
Please click on &quot;Save changes&quot; to save the changes you have made.{' '}
</Alert>
) : null}
*/}
{!codeValid ? (
<Alert color="danger">
NOT SAVED! The code must be between 5 and 7 characters long
Expand Down Expand Up @@ -1741,12 +1781,13 @@ const onAssignProject = assignedProject => {
userId={props.match.params.userId}
updateTask={onUpdateTask}
handleSubmit={handleSubmit}
disabled={
!formValid.firstName ||
!formValid.lastName ||
!formValid.email ||
!(isProfileEqual && isTasksEqual && isProjectsEqual)
}
// disabled={
// !formValid.firstName ||
// !formValid.lastName ||
// !formValid.email ||
// !(isProfileEqual && isTasksEqual && isProjectsEqual)
// }
disabled={false}
darkMode={darkMode}
/>
)
Expand Down Expand Up @@ -1797,7 +1838,7 @@ const onAssignProject = assignedProject => {
</Button>
</Link>
)}
{((canEdit && activeTab) || canEditTeamCode) && (
{((canEdit && activeTab) || canEditTeamCode) && activeTab !== '4' && (
<>
<SaveButton
className="mr-1 btn-bottom"
Expand All @@ -1808,7 +1849,6 @@ const onAssignProject = assignedProject => {
!formValid.email ||
!codeValid ||
(userStartDate > userEndDate && userEndDate !== '') ||
// titleOnSet ||
(isProfileEqual && isTasksEqual && isProjectsEqual)
}
userProfile={userProfile}
Expand Down Expand Up @@ -2184,12 +2224,13 @@ const onAssignProject = assignedProject => {
userId={props.match.params.userId}
updateTask={onUpdateTask}
handleSubmit={handleSubmit}
disabled={
!formValid.firstName ||
!formValid.lastName ||
!formValid.email ||
!(isProfileEqual && isTasksEqual && isProjectsEqual)
}
// disabled={
// !formValid.firstName ||
// !formValid.lastName ||
// !formValid.email ||
// !(isProfileEqual && isTasksEqual && isProjectsEqual)
// }
disabled={false}
darkMode={darkMode}
/>
</ModalBody>
Expand Down
1 change: 1 addition & 0 deletions src/utils/URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const ENDPOINTS = {
TASK_DEL: (taskId, motherId) => `${APIEndpoint}/task/del/${taskId}/${motherId}`,
GET_TASK: taskId => `${APIEndpoint}/task/${taskId}`,
TASK_UPDATE: taskId => `${APIEndpoint}/task/update/${taskId}`,
TASK_DELETE_BY_ID: (taskId, userId) => `${APIEndpoint}/task/deleteTask/${taskId}/${userId}`,
TASK_UPDATE_STATUS: taskId => `${APIEndpoint}/task/updateStatus/${taskId}`,
TASK_CHANGE_LOGS: taskId => `${APIEndpoint}/task/${taskId}/changeLogs`,
DELETE_CHILDREN: taskId => `${APIEndpoint}/task/delete/children/${taskId}`,
Expand Down
Loading