Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

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