Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

215 linhas
6.1 KiB

  1. "use client";
  2. import React, { useCallback, useState } from "react";
  3. import { useTranslation } from "react-i18next";
  4. import Button from "@mui/material/Button";
  5. import Stack from "@mui/material/Stack";
  6. import {
  7. CalendarMonth,
  8. EditCalendar,
  9. Luggage,
  10. MoreTime,
  11. } from "@mui/icons-material";
  12. import { Menu, MenuItem, SxProps, Typography } from "@mui/material";
  13. import AssignedProjects from "./AssignedProjects";
  14. import TimesheetModal from "../TimesheetModal";
  15. import { AssignedProject, ProjectWithTasks } from "@/app/api/projects";
  16. import {
  17. RecordLeaveInput,
  18. RecordTimesheetInput,
  19. revalidateCacheAfterAmendment,
  20. } from "@/app/api/timesheets/actions";
  21. import LeaveModal from "../LeaveModal";
  22. import { LeaveType, TeamTimeSheets } from "@/app/api/timesheets";
  23. import { CalendarIcon } from "@mui/x-date-pickers";
  24. import PastEntryCalendarModal from "../PastEntryCalendar/PastEntryCalendarModal";
  25. import { HolidaysResult } from "@/app/api/holidays";
  26. import { TimesheetAmendmentModal } from "../TimesheetAmendment/TimesheetAmendmentModal";
  27. export interface Props {
  28. leaveTypes: LeaveType[];
  29. allProjects: ProjectWithTasks[];
  30. assignedProjects: AssignedProject[];
  31. username: string;
  32. defaultLeaveRecords: RecordLeaveInput;
  33. defaultTimesheets: RecordTimesheetInput;
  34. holidays: HolidaysResult[];
  35. teamTimesheets: TeamTimeSheets;
  36. fastEntryEnabled?: boolean;
  37. }
  38. const menuItemSx: SxProps = {
  39. gap: 1,
  40. color: "neutral.700",
  41. };
  42. const UserWorkspacePage: React.FC<Props> = ({
  43. leaveTypes,
  44. allProjects,
  45. assignedProjects,
  46. username,
  47. defaultLeaveRecords,
  48. defaultTimesheets,
  49. holidays,
  50. teamTimesheets,
  51. fastEntryEnabled,
  52. }) => {
  53. const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
  54. const [isTimeheetModalVisible, setTimeheetModalVisible] = useState(false);
  55. const [isLeaveModalVisible, setLeaveModalVisible] = useState(false);
  56. const [isPastEventModalVisible, setPastEventModalVisible] = useState(false);
  57. const [isTimesheetAmendmentVisible, setisTimesheetAmendmentVisible] =
  58. useState(false);
  59. const { t } = useTranslation("home");
  60. const showTimesheetAmendment = Object.keys(teamTimesheets).length > 0;
  61. const handleOpenActionMenu = useCallback<
  62. React.MouseEventHandler<HTMLButtonElement>
  63. >((event) => {
  64. setAnchorEl(event.currentTarget);
  65. }, []);
  66. const handleCloseActionMenu = useCallback(() => {
  67. setAnchorEl(null);
  68. }, []);
  69. const handleAddTimesheetButtonClick = useCallback(() => {
  70. setAnchorEl(null);
  71. setTimeheetModalVisible(true);
  72. }, []);
  73. const handleCloseTimesheetModal = useCallback(() => {
  74. setTimeheetModalVisible(false);
  75. }, []);
  76. const handleAddLeaveButtonClick = useCallback(() => {
  77. setAnchorEl(null);
  78. setLeaveModalVisible(true);
  79. }, []);
  80. const handleCloseLeaveModal = useCallback(() => {
  81. setLeaveModalVisible(false);
  82. }, []);
  83. const handlePastEventClick = useCallback(() => {
  84. setAnchorEl(null);
  85. setPastEventModalVisible(true);
  86. }, []);
  87. const handlePastEventClose = useCallback(() => {
  88. setPastEventModalVisible(false);
  89. }, []);
  90. const handleAmendmentClick = useCallback(() => {
  91. setAnchorEl(null);
  92. setisTimesheetAmendmentVisible(true);
  93. }, []);
  94. const handleAmendmentClose = useCallback(() => {
  95. setisTimesheetAmendmentVisible(false);
  96. revalidateCacheAfterAmendment();
  97. }, []);
  98. return (
  99. <>
  100. <Stack
  101. direction="row"
  102. justifyContent="space-between"
  103. flexWrap="wrap"
  104. rowGap={2}
  105. >
  106. <Typography variant="h4" marginInlineEnd={2}>
  107. {t("User Workspace")}
  108. </Typography>
  109. <Button
  110. startIcon={<CalendarIcon />}
  111. variant="contained"
  112. onClick={handleOpenActionMenu}
  113. >
  114. {t("Timesheet Actions")}
  115. </Button>
  116. </Stack>
  117. <Menu
  118. anchorEl={anchorEl}
  119. open={Boolean(anchorEl)}
  120. onClose={handleCloseActionMenu}
  121. anchorOrigin={{
  122. vertical: "bottom",
  123. horizontal: "right",
  124. }}
  125. transformOrigin={{
  126. vertical: "top",
  127. horizontal: "right",
  128. }}
  129. >
  130. <MenuItem onClick={handleAddTimesheetButtonClick} sx={menuItemSx}>
  131. <MoreTime />
  132. {t("Enter Time")}
  133. </MenuItem>
  134. <MenuItem onClick={handleAddLeaveButtonClick} sx={menuItemSx}>
  135. <Luggage />
  136. {t("Record Leave")}
  137. </MenuItem>
  138. <MenuItem onClick={handlePastEventClick} sx={menuItemSx}>
  139. <CalendarMonth />
  140. {t("View Past Entries")}
  141. </MenuItem>
  142. {showTimesheetAmendment && (
  143. <MenuItem onClick={handleAmendmentClick} sx={menuItemSx}>
  144. <EditCalendar />
  145. {t("Timesheet Amendment")}
  146. </MenuItem>
  147. )}
  148. </Menu>
  149. <PastEntryCalendarModal
  150. open={isPastEventModalVisible}
  151. handleClose={handlePastEventClose}
  152. timesheet={defaultTimesheets}
  153. leaves={defaultLeaveRecords}
  154. allProjects={allProjects}
  155. leaveTypes={leaveTypes}
  156. />
  157. <TimesheetModal
  158. fastEntryEnabled={fastEntryEnabled}
  159. companyHolidays={holidays}
  160. isOpen={isTimeheetModalVisible}
  161. onClose={handleCloseTimesheetModal}
  162. allProjects={allProjects}
  163. assignedProjects={assignedProjects}
  164. username={username}
  165. defaultTimesheets={defaultTimesheets}
  166. leaveRecords={defaultLeaveRecords}
  167. />
  168. <LeaveModal
  169. companyHolidays={holidays}
  170. leaveTypes={leaveTypes}
  171. isOpen={isLeaveModalVisible}
  172. onClose={handleCloseLeaveModal}
  173. defaultLeaveRecords={defaultLeaveRecords}
  174. timesheetRecords={defaultTimesheets}
  175. username={username}
  176. />
  177. {assignedProjects.length > 0 ? (
  178. <AssignedProjects assignedProjects={assignedProjects} />
  179. ) : (
  180. <Typography variant="subtitle1">
  181. {t("You have no assigned projects!")}
  182. </Typography>
  183. )}
  184. {showTimesheetAmendment && (
  185. <TimesheetAmendmentModal
  186. allProjects={allProjects}
  187. companyHolidays={holidays}
  188. teamTimesheets={teamTimesheets}
  189. open={isTimesheetAmendmentVisible}
  190. onClose={handleAmendmentClose}
  191. />
  192. )}
  193. </>
  194. );
  195. };
  196. export default UserWorkspacePage;