You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

216 lines
8.1 KiB

  1. // material-ui
  2. import * as React from 'react';
  3. import {
  4. DataGrid,
  5. GridActionsCellItem,
  6. } from "@mui/x-data-grid";
  7. import AssignmentTurnedInOutlinedIcon from '@mui/icons-material/AssignmentTurnedInOutlined';
  8. import NotificationsOffRoundedIcon from '@mui/icons-material/NotificationsOffRounded';
  9. import {
  10. CustomNoRowsOverlay, getObjectById, notifySaveSuccess,
  11. } from "../../utils/CommonFunction";
  12. import {Typography} from "@mui/material";
  13. import {LIONER_BUTTON_THEME} from "../../themes/colorConst";
  14. import {ThemeProvider} from "@emotion/react";
  15. import axios from "axios";
  16. import {apiPath, getUserData} from "../../auth/utils";
  17. import {POST_TODO_MARK_READ, POST_TODO_MARK_SUPPRESS} from "../../utils/ApiPathConst";
  18. import {useContext} from "react";
  19. import AbilityContext from "../../components/AbilityProvider";
  20. export default function ReminderTable({recordList, isReaded, fetchList,value, isReminder}) {
  21. const [rows, setRows] = React.useState(recordList);
  22. const [rowModesModel] = React.useState({});
  23. const userData = getUserData();
  24. const ability = useContext(AbilityContext);
  25. const handleSuppressClick = (id) => () => {
  26. axios.post(`${apiPath}${POST_TODO_MARK_SUPPRESS}/${id}/${userData.id}`,{
  27. })
  28. .then((response) => {
  29. if (response.status === 200) {
  30. fetchList(value);
  31. notifySaveSuccess();
  32. }
  33. })
  34. .catch(error => {
  35. console.log(error);
  36. return false;
  37. });
  38. };
  39. const handleReadClick = (id) => () => {
  40. axios.post(`${apiPath}${POST_TODO_MARK_READ}/${id}/${userData.id}`,{
  41. })
  42. .then((response) => {
  43. if (response.status === 200) {
  44. setRows(rows.filter((row) => row.id !== id));
  45. notifySaveSuccess();
  46. }
  47. })
  48. .catch(error => {
  49. console.log(error);
  50. return false;
  51. });
  52. };
  53. // const getRowHeight = (params) => {
  54. // const message = params.model.message; // Assuming the 'message' field is present in the data
  55. // const lines = message.split("\\n").length;
  56. //
  57. // const SINGLE_LINE_HEIGHT = 40;
  58. // const HEIGHT_WITH_PADDING = 20;
  59. // const LINE_HEIGHT = 24; // Adjust this value based on your font size and row padding
  60. // const height = lines < 2 ? SINGLE_LINE_HEIGHT : HEIGHT_WITH_PADDING*2 + (LINE_HEIGHT * (lines-2) );
  61. // console.log(height);
  62. // return height <= SINGLE_LINE_HEIGHT ? SINGLE_LINE_HEIGHT : height;
  63. // };
  64. const columns = [
  65. {
  66. id: 'title',
  67. field: 'title',
  68. headerName: 'Title',
  69. //headerAlign: 'center',
  70. //align: 'center',
  71. flex: 2,
  72. renderCell: (params) => {
  73. return (
  74. <div style={{ alignItems: 'flex-start', justifyContent: 'flex-start' }}>
  75. <Typography
  76. component="div"
  77. variant="lionerDetailSize"
  78. style={{ whiteSpace: 'pre-wrap', wordWrap: 'break-word', textAlign: 'left', alignItems: 'flex-start' }}
  79. >
  80. {params.row.title}
  81. </Typography>
  82. </div>
  83. );
  84. }
  85. },
  86. {
  87. id: 'message',
  88. field: 'message',
  89. headerName: 'Message',
  90. flex: 3 ,
  91. renderCell: (params) => {
  92. const detail = params.row.message;
  93. const detailLines = detail.split("\\n").map((line, index) => {
  94. const lineWithTabs = line.replaceAll("\\t", '\t'); // Replace \t with four non-breaking spaces
  95. return (
  96. <Typography
  97. key={index}
  98. component="div"
  99. variant="lionerDetailSize"
  100. style={{whiteSpace: 'pre-wrap', wordWrap: 'break-word'}}
  101. >
  102. {lineWithTabs}
  103. </Typography>
  104. )
  105. });
  106. return <div>{detailLines}</div>;
  107. },
  108. },
  109. {
  110. field: 'actions',
  111. type: 'actions',
  112. headerName: !isReminder ? 'Read' : 'Read/ Suppress',
  113. width: 200,
  114. cellClassName: 'actions',
  115. getActions: ({id}) => {
  116. const row = getObjectById(rows,id);
  117. //console.log(row.suppressedBy)
  118. return !isReminder?
  119. isReaded?
  120. [
  121. <ThemeProvider key="theme" theme={LIONER_BUTTON_THEME}>
  122. </ThemeProvider>
  123. ]
  124. :
  125. [
  126. <ThemeProvider key="theme" theme={LIONER_BUTTON_THEME}>
  127. <GridActionsCellItem
  128. key="markRead"
  129. icon={<AssignmentTurnedInOutlinedIcon sx={{fontSize: 25}}/>}
  130. label="Read"
  131. onClick={handleReadClick(id)}
  132. color="create"
  133. />
  134. </ThemeProvider>
  135. ,
  136. ]
  137. :
  138. isReaded?
  139. [
  140. <ThemeProvider key="theme" theme={LIONER_BUTTON_THEME}>
  141. <GridActionsCellItem
  142. key="Suppress"
  143. icon={<NotificationsOffRoundedIcon sx={{fontSize: 25}}/>}
  144. label="Suppress"
  145. className="textPrimary"
  146. onClick={handleSuppressClick(id)}
  147. color="delete"
  148. disabled={row.suppressedBy === null ? !ability.can('SUPPRESS','REMINDER') : true}
  149. />
  150. </ThemeProvider>,
  151. ]
  152. :
  153. [
  154. <ThemeProvider key="theme" theme={LIONER_BUTTON_THEME}>
  155. <GridActionsCellItem
  156. key="markRead"
  157. icon={<AssignmentTurnedInOutlinedIcon sx={{fontSize: 25}}/>}
  158. label="Read"
  159. onClick={handleReadClick(id)}
  160. color="create"
  161. />
  162. </ThemeProvider>
  163. ,
  164. <ThemeProvider key="theme" theme={LIONER_BUTTON_THEME}>
  165. <GridActionsCellItem
  166. key="Suppress"
  167. icon={<NotificationsOffRoundedIcon sx={{fontSize: 25}}/>}
  168. label="Suppress"
  169. className="textPrimary"
  170. onClick={handleSuppressClick(id)}
  171. disabled={!ability.can('SUPPRESS','REMINDER') }
  172. color="delete"
  173. />
  174. </ThemeProvider>,
  175. ]
  176. },
  177. },
  178. ];
  179. return (
  180. <div style={{height: '33vh', width: '100%'}}>
  181. <DataGrid
  182. rows={rows}
  183. columns={columns}
  184. columnHeaderHeight={45}
  185. editMode="row"
  186. rowModesModel={rowModesModel}
  187. disableColumnMenu
  188. getRowHeight={() => 'auto'}
  189. //getRowHeight={getRowHeight}
  190. initialState={{
  191. pagination: {
  192. paginationModel: {page: 0, pageSize: 10},
  193. },
  194. }}
  195. slots={{
  196. noRowsOverlay: () => (
  197. CustomNoRowsOverlay()
  198. )
  199. }}
  200. pageSizeOptions={[10, 15, 20]}
  201. //autoHeight
  202. />
  203. </div>
  204. );
  205. }