Skip to content

Commit

Permalink
Merge pull request #292 from goinvo/deleteIfNoHours
Browse files Browse the repository at this point in the history
Delete assignment if no hours
  • Loading branch information
zhukovdigital authored Oct 31, 2024
2 parents 8b69fb0 + a8d071a commit 7a3928b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
9 changes: 8 additions & 1 deletion app/components/scrollingCalendar/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,9 +976,16 @@ export const updateOrInsertWorkWeekInProject = (
} else {
updatedWorkWeeks = [...assignment.workWeeks, workWeek];
}
const canBeDeleted = !updatedWorkWeeks.some(
(week) => (week.actualHours ?? 0) > 0
);
const updatedFields = {
workWeeks: updatedWorkWeeks,
canBeDeleted:canBeDeleted
}
return {
...assignment,
workWeeks: updatedWorkWeeks,
...updatedFields
};
}
return assignment;
Expand Down
43 changes: 34 additions & 9 deletions app/components/userSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'
import React from "react";
import { useMutation } from "@apollo/client";
import { ArchiveBoxIcon } from "@heroicons/react/24/outline";
import { ArchiveBoxIcon, XCircleIcon } from "@heroicons/react/24/outline";

import { AssignmentType, UserSummaryProps } from "../typeInterfaces";
import IconButton from "./iconButton";
Expand Down Expand Up @@ -103,13 +103,6 @@ const UserSummary: React.FC<UserSummaryProps> = ({ assignment, selectedUser, pro
setSingleUserPage(selectedUserData);
return;
}
if (assignment.assignedUser === null) {
const variables = {
assignmentId: assignment.id,
};
deleteAssignment({ variables });
return;
}
if (assignment.status !== 'archived') {
const projectId = project ? project.id : assignment.project.id;
const variables = {
Expand All @@ -122,11 +115,35 @@ const UserSummary: React.FC<UserSummaryProps> = ({ assignment, selectedUser, pro
}
};

const handleDeleteAssignmentClick = () => {
if (assignment.canBeDeleted) {
const deletedAssignment = selectedUser?.assignments.filter((a: AssignmentType) => a.id !== assignment.id);
const selectedUserData = {
...selectedUser,
assignments: deletedAssignment || [],
name: selectedUser?.name || "Default Name",
avatarUrl: selectedUser?.avatarUrl || "defaultAvatarUrl.png",
};
setSingleUserPage(selectedUserData);
const variables = {
assignmentId: assignment.id,
};
deleteAssignment({ variables });
return;

};
}
const summaries = [
{ label: 'future plan', value: calculatePlan(assignment, { isFuture: true }), unit: 'hrs' },
{ label: 'burned', value: burnedHours, unit: 'hrs', alwaysShow: true },
{ label: 'past plan', value: calculatePlan(assignment, { isFuture: false }), unit: 'hrs' },
];
const canAssignmentBeDeleted = !assignment.workWeeks.some(
(week) => (week.actualHours ?? 0) > 0
);
const showArchiveButton = viewer?.id === assignment.assignedUser?.id && !canAssignmentBeDeleted
const showDeleteButton = !assignment.assignedUser || viewer?.id === assignment.assignedUser?.id && assignment.canBeDeleted && canAssignmentBeDeleted


return (
<td className="font-normal py-2 sm:pl-4 pl-0 pr-0 ml-1 sm:ml-0 w-1/2 sm:w-1/6">
Expand All @@ -146,14 +163,22 @@ const UserSummary: React.FC<UserSummaryProps> = ({ assignment, selectedUser, pro
)}
</div>
)}
{(viewer?.id === assignment.assignedUser?.id || !assignment.assignedUser) && <div className="sm:flex hidden items-start justify-center">
{showArchiveButton && <div className="sm:flex hidden items-start justify-center">
<IconButton
className='text-black flex items-start justify-center text-transparentGrey'
onClick={handleArchiveItemClick}
Icon={ArchiveBoxIcon}
iconSize={'h6 w-6'}
/>
</div>}
{showDeleteButton && <div className="sm:flex hidden items-start justify-center">
<IconButton
className='text-black flex items-start justify-center text-transparentGrey'
onClick={handleDeleteAssignmentClick}
Icon={XCircleIcon}
iconSize={'h6 w-6'}
/>
</div>}
</div>
</td >
);
Expand Down
3 changes: 3 additions & 0 deletions app/gqlQueries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ export const GET_USER_LIST = gql`
endsOn
estimatedWeeklyHours
status
canBeDeleted
assignedUser {
id
name
}
project {
id
Expand Down Expand Up @@ -221,6 +223,7 @@ export const GET_ALL_PROJECTS_DATA = gql`
id
avatarUrl
}
canBeDeleted
id
startsOn
estimatedWeeklyHours
Expand Down
1 change: 1 addition & 0 deletions app/typeInterfaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface AllProjectRowProps {
}
export interface AssignmentType {
assignedUser: UserType;
canBeDeleted: boolean;
endsOn: string | null;
client: ClientType;
id: number;
Expand Down

0 comments on commit 7a3928b

Please sign in to comment.