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.

280 lines
10 KiB

  1. // material-ui
  2. import {
  3. Grid,
  4. Typography,
  5. Stack,
  6. Button,
  7. Dialog, DialogTitle, DialogContent, DialogActions,
  8. } from '@mui/material';
  9. import * as UrlUtils from "utils/ApiPathConst";
  10. import * as React from "react";
  11. import * as HttpUtils from "utils/HttpUtils";
  12. import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'
  13. import Loadable from 'components/Loadable';
  14. const LoadingComponent = Loadable(React.lazy(() => import('pages/extra-pages/LoadingComponent')));
  15. import MainCard from 'components/MainCard';
  16. const ExportForm = Loadable(React.lazy(() => import('./ExportForm')));
  17. const SearchForm = Loadable(React.lazy(() => import('./SearchForm')));
  18. const GazetteIssueTable = Loadable(React.lazy(() => import('./DataGrid')))
  19. const BackgroundHead = {
  20. backgroundImage: `url(${titleBackgroundImg})`,
  21. width: '100%',
  22. height: '100%',
  23. backgroundSize: 'contain',
  24. backgroundRepeat: 'no-repeat',
  25. backgroundColor: '#0C489E',
  26. backgroundPosition: 'right'
  27. }
  28. import {PNSPS_LONG_BUTTON_THEME} from "themes/buttonConst";
  29. import {ThemeProvider} from "@emotion/react";
  30. import { dateStr_Year } from "utils/DateUtils";
  31. import { notifySaveSuccess } from 'utils/CommonFunction';
  32. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  33. const Index = () => {
  34. const [record, setRecord] = React.useState([]);
  35. const [comboData, setComboData] = React.useState([]);
  36. const [holidayComboData, setHolidayComboData] = React.useState([]);
  37. const [onReady, setOnReady] = React.useState(false);
  38. const [onSearchReady, setOnSearchReady] = React.useState(false);
  39. const [onExportReady, setOnExportReady] = React.useState(false);
  40. // const navigate = useNavigate()
  41. const [searchCriteria, setSearchCriteria] = React.useState({
  42. year: dateStr_Year(new Date()),
  43. // dateFrom: DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)),
  44. });
  45. const [exportCriteria, setExportCriteria] = React.useState({
  46. // year: dateStr_Year(new Date()),
  47. // dateFrom: DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)),
  48. });
  49. const [attachments, setAttachments] = React.useState([]);
  50. const [waitImport, setWaitImport] = React.useState(false);
  51. const [waitDownload, setWaitDownload] = React.useState(false);
  52. const [isWarningPopUp, setIsWarningPopUp] = React.useState(false);
  53. const [warningText, setWarningText] = React.useState("");
  54. React.useEffect(() => {
  55. // console.log(searchCriteria)
  56. setOnSearchReady(false)
  57. loadForm();
  58. }, [searchCriteria]);
  59. function loadForm() {
  60. HttpUtils.get({
  61. url: UrlUtils.GET_ISSUE,
  62. params: searchCriteria,
  63. onSuccess: (responseData) => {
  64. // console.log(comboData)
  65. setRecord(responseData);
  66. if (comboData.length == 0) {
  67. loadCombo();
  68. }else{
  69. setOnSearchReady(true)
  70. }
  71. }
  72. });
  73. }
  74. function loadCombo() {
  75. HttpUtils.get({
  76. url: UrlUtils.GET_ISSUE_YEAR_COMBO,
  77. onSuccess: (responseData) => {
  78. let combo = responseData;
  79. setComboData(combo);
  80. setOnSearchReady(true)
  81. loadHolidayCombo(true)
  82. // setOnReady(true);
  83. }
  84. });
  85. }
  86. function loadHolidayCombo() {
  87. HttpUtils.get({
  88. url: UrlUtils.GET_HOLIDAY_COMBO,
  89. onSuccess: (responseData) => {
  90. let combo = responseData
  91. setHolidayComboData(combo)
  92. setOnExportReady(true)
  93. setOnReady(true)
  94. }
  95. });
  96. }
  97. function applySearch(input) {
  98. setSearchCriteria(input);
  99. }
  100. function applyExport(input) {
  101. setExportCriteria(input);
  102. }
  103. React.useEffect(() => {
  104. if (Object.keys(exportCriteria).length > 0) {
  105. // console.log(exportCriteria)
  106. doExport();
  107. }
  108. }, [exportCriteria]);
  109. React.useEffect(() => {
  110. if (attachments.length > 0) {
  111. importHoliday();
  112. }
  113. }, [attachments]);
  114. const readFile = (event) => {
  115. let file = event.target.files[0];
  116. if (file) {
  117. if (!file.name.toLowerCase().substr(file.name.length - 5).includes(".xlsx")) {
  118. setWarningText("Please upload a valid file (File format: .xlsx).");
  119. setIsWarningPopUp(true);
  120. document.getElementById("uploadFileBtn").value = "";
  121. return;
  122. }
  123. file['id'] = attachments.length;
  124. setAttachments([
  125. ...attachments,
  126. file
  127. ]);
  128. document.getElementById("uploadFileBtn").value = "";
  129. }
  130. }
  131. const doExport=()=>{
  132. setWaitDownload(true)
  133. HttpUtils.fileDownload({
  134. url: UrlUtils.GET_ISSUE_LIST,
  135. params: exportCriteria,
  136. onResponse: () => {
  137. setTimeout(()=> setWaitDownload(false), 500)
  138. }
  139. });
  140. }
  141. const importHoliday = () => {
  142. setWaitImport(true);
  143. if (!attachments || attachments.length <= 0) {
  144. setWarningText("Please upload file.");
  145. setSaving(false);
  146. return;
  147. }
  148. HttpUtils.postWithFiles({
  149. url: UrlUtils.POST_ISSUE_FILE,
  150. files: attachments,
  151. onSuccess: () => {
  152. notifySaveSuccess()
  153. setWaitImport(false);
  154. setAttachments([]);
  155. loadForm();
  156. }
  157. });
  158. }
  159. return (
  160. !onReady ?
  161. <Grid container sx={{ minHeight: '87vh', mb: 3 }} direction="column" justifyContent="center" alignItems="center">
  162. <Grid item>
  163. <LoadingComponent />
  164. </Grid>
  165. </Grid>
  166. :
  167. (
  168. <Grid container sx={{minHeight: '87vh', backgroundColor: 'backgroundColor.default'}} direction="column" justifyContent="flex-start" alignItems="center" >
  169. <Grid item xs={12} width="100%">
  170. <div style={BackgroundHead} width="100%">
  171. <Stack direction="row" height='70px'>
  172. <Typography ml={15} color='#FFF' variant="h4" sx={{ pt: 2 }}>Gazette Issue</Typography>
  173. </Stack>
  174. </div>
  175. </Grid>
  176. {!onExportReady?
  177. <LoadingComponent />:
  178. <Grid item xs={12} md={12} lg={12} width="100%">
  179. <ExportForm
  180. applyExport={applyExport}
  181. comboData={holidayComboData}
  182. waitDownload={waitDownload}
  183. />
  184. </Grid>
  185. }
  186. <Grid item xs={12} md={12} lg={6} width="100%">
  187. <Stack direction="row" justifyContent="flex-start" alignItems="center" spacing={2} sx={{ml:2,mt:1}} >
  188. <ThemeProvider theme={PNSPS_LONG_BUTTON_THEME}>
  189. <input
  190. id="uploadFileBtn"
  191. name="file"
  192. type="file"
  193. accept=".xlsx"
  194. style={{ display: 'none' }}
  195. disabled={waitImport}
  196. onChange={(event) => {
  197. readFile(event)
  198. }}
  199. />
  200. <label htmlFor="uploadFileBtn">
  201. <Button
  202. component="span"
  203. variant="contained"
  204. size="large"
  205. disabled={waitImport}
  206. >
  207. <Typography variant="h5">Upload Files</Typography>
  208. </Button>
  209. </label>
  210. </ThemeProvider>
  211. </Stack>
  212. </Grid>
  213. {/*row 1*/}
  214. <Grid item xs={12} md={12} lg={12} width="100%">
  215. <SearchForm
  216. applySearch={applySearch}
  217. comboData={comboData}
  218. />
  219. </Grid>
  220. {/*row 2*/}
  221. {!onSearchReady?
  222. <LoadingComponent/>:
  223. <Grid item xs={12} md={12} lg={12} width="100%">
  224. <MainCard elevation={0}
  225. border={false}
  226. content={false}
  227. >
  228. <GazetteIssueTable
  229. recordList={record}
  230. />
  231. </MainCard>
  232. </Grid>
  233. }
  234. <div>
  235. <Dialog
  236. open={isWarningPopUp}
  237. onClose={() => setIsWarningPopUp(false)}
  238. PaperProps={{
  239. sx: {
  240. minWidth: '40vw',
  241. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  242. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  243. }
  244. }}
  245. >
  246. <DialogTitle><Typography variant="h3">Warning</Typography></DialogTitle>
  247. <DialogContent style={{ display: 'flex', }}>
  248. <Typography variant="h4" style={{ padding: '16px' }}>{warningText}</Typography>
  249. </DialogContent>
  250. <DialogActions>
  251. <Button onClick={() => setIsWarningPopUp(false)}><Typography variant="h5">OK</Typography></Button>
  252. </DialogActions>
  253. </Dialog>
  254. </div>
  255. </Grid >
  256. )
  257. );
  258. };
  259. export default Index;