Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

607 righe
30 KiB

  1. // material-ui
  2. import {
  3. Grid, Typography, Button,
  4. Dialog, DialogTitle, DialogContent, DialogActions,
  5. } from '@mui/material';
  6. import MainCard from "../../../components/MainCard";
  7. import * as React from "react";
  8. import * as FieldUtils from "../../../utils/FieldUtils";
  9. import * as HttpUtils from '../../../utils/HttpUtils';
  10. import * as UrlUtils from "../../../utils/ApiPathConst";
  11. import * as ComboData from "../../../utils/ComboData";
  12. import { useFormik } from 'formik';
  13. import * as yup from 'yup';
  14. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  15. import Loadable from 'components/Loadable';
  16. import { lazy } from 'react';
  17. import { notifyActiveSuccess, notifyLockSuccess, notifySaveSuccess, notifyVerifySuccess } from 'utils/CommonFunction';
  18. import {useIntl} from "react-intl";
  19. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  20. const UserInformationCard_Organization = ({ userData, loadDataFun, orgData }) => {
  21. const [currentUserData, setCurrentUserData] = React.useState(userData);
  22. const [isWarningPopUp, setIsWarningPopUp] = React.useState(false);
  23. const [warningText, setWarningText] = React.useState("");
  24. const [isConfirmPopUp, setIsConfirmPopUp] = React.useState(false);
  25. const [confirmText, setConfirmText] = React.useState("");
  26. const [confirmAction, setConfirmAction] = React.useState();
  27. const [editMode, setEditMode] = React.useState(false);
  28. const [onReady, setOnReady] = React.useState(false);
  29. const intl = useIntl();
  30. React.useEffect(() => {
  31. //if state data are ready and assign to different field
  32. // console.log(currentApplicationDetailData)
  33. if (Object.keys(currentUserData).length > 0) {
  34. setOnReady(true);
  35. }
  36. }, [currentUserData]);
  37. function displayErrorMsg(errorMsg) {
  38. return <Typography variant="errorMessage1">{errorMsg}</Typography>
  39. }
  40. const formik = useFormik({
  41. enableReinitialize: true,
  42. initialValues: currentUserData,
  43. validationSchema: yup.object().shape({
  44. contactPerson: yup.string().max(255).required(displayErrorMsg(intl.formatMessage({id: 'userRequireName'}))),
  45. enCompanyName: yup.string().max(255).required(displayErrorMsg(intl.formatMessage({id: 'userRequireEnglishName'}))),
  46. chCompanyName: yup.string().max(255).nullable(),
  47. addressLine1: yup.string().max(255).required(displayErrorMsg(intl.formatMessage({id: 'validateAddressLine1'}))),
  48. addressLine2: yup.string().max(255).nullable(),
  49. addressLine3: yup.string().max(255).nullable(),
  50. emailBus: yup.string().max(255).required(displayErrorMsg(intl.formatMessage({id: 'requireEmail'}))),
  51. tel_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({id: 'require3Number'}))).required(displayErrorMsg(intl.formatMessage({id: 'requireDialingCode'}))),
  52. fax_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({id: 'require3Number'}))).nullable(),
  53. phoneNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({id: 'require8Number'}))).required(displayErrorMsg(intl.formatMessage({id: 'requireContactNumber'}))),
  54. faxNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({id: 'require8Number'}))).nullable(),
  55. brExpiryDate: yup.string().min(8, displayErrorMsg(intl.formatMessage({id: 'pleaseFillInBusinessRegCertValidityDate'}))),
  56. brNo: yup.string().max(8).required(displayErrorMsg(intl.formatMessage({id: 'pleaseFillInBusinessRegCertNumber'})))
  57. .test('checkBrNoFormat', displayErrorMsg(`${intl.formatMessage({id: 'pleaseFillInValidBusinessRegCertNumber'})} (e.g. 12341234)`), function (value) {
  58. var brNo_pattern = /[0-9]{8}/
  59. if (value !== undefined) {
  60. if (value.match(brNo_pattern)) {
  61. return true
  62. } else {
  63. return false
  64. }
  65. }
  66. }),
  67. }),
  68. onSubmit: (values) => {
  69. HttpUtils.post({
  70. url: UrlUtils.POST_ORG_USER + "/" + userData.id,
  71. params: {
  72. contactTel: {
  73. countryCode: values.tel_countryCode,
  74. phoneNumber: values.phoneNumber
  75. },
  76. // faxNo: {
  77. // countryCode: values.fax_countryCode,
  78. // faxNumber: values.faxNumber
  79. // },
  80. // addressBus: {
  81. // country: values.country,
  82. // district: values.district,
  83. // addressLine1: values.addressLine1,
  84. // addressLine2: values.addressLine2,
  85. // addressLine3: values.addressLine3,
  86. // },
  87. identification: values.identification,
  88. emailBus: values.emailBus,
  89. contactPerson: values.contactPerson,
  90. // enCompanyName: values.enCompanyName,
  91. // chCompanyName: values.chCompanyName,
  92. orgId: values.orgId,
  93. // brNo: values.brNo,
  94. // brExpiryDate: values.brExpiryDate,
  95. },
  96. onSuccess: function () {
  97. notifySaveSuccess()
  98. loadDataFun();
  99. }
  100. });
  101. }
  102. });
  103. React.useEffect(() => {
  104. if (Object.keys(userData).length > 0) {
  105. setCurrentUserData(userData);
  106. }
  107. }, [userData]);
  108. const onEditClick = () => {
  109. setEditMode(true);
  110. };
  111. const createOrgClick = () => {
  112. window.open("/org/fromUser/" + userData.id, "_blank", "noreferrer");
  113. window.addEventListener("focus", onFocus)
  114. };
  115. const onFocus = () => {
  116. loadDataFun();
  117. window.removeEventListener("focus", onFocus)
  118. }
  119. const onVerifiedClick = () => {
  120. if (formik?.values?.orgId) {
  121. HttpUtils.get({
  122. url: UrlUtils.GET_IND_USER_VERIFY + "/" + userData.id,
  123. onSuccess: function () {
  124. notifyVerifySuccess()
  125. loadDataFun();
  126. }
  127. });
  128. } else {
  129. setWarningText("Please select Organisation before active this account.")
  130. setIsWarningPopUp(true);
  131. }
  132. };
  133. const doLock = () => {
  134. setConfirmText("Confirm to Lock this Account?");
  135. setConfirmAction({
  136. function: function () {
  137. HttpUtils.get({
  138. url: UrlUtils.GET_USER_LOCK + "/" + userData.id,
  139. onSuccess: function () {
  140. notifyLockSuccess()
  141. loadDataFun();
  142. }
  143. });
  144. }
  145. });
  146. setIsConfirmPopUp(true);
  147. };
  148. const doUnlock = () => {
  149. setConfirmText("Confirm to Un-Lock this Account?");
  150. setConfirmAction({
  151. function: function () {
  152. HttpUtils.get({
  153. url: UrlUtils.GET_USER_UNLOCK + "/" + userData.id,
  154. onSuccess: function () {
  155. notifyActiveSuccess()
  156. loadDataFun();
  157. }
  158. });
  159. }
  160. });
  161. setIsConfirmPopUp(true);
  162. };
  163. return (
  164. <MainCard elevation={0}
  165. border={false}
  166. content={false}
  167. >
  168. {!onReady ?
  169. <LoadingComponent />
  170. :
  171. <form onSubmit={formik.handleSubmit}>
  172. {/*top button*/}
  173. <Grid item s={12} md={12} lg={12} sx={{ mb: 3, mt: 2 }} alignItems={"start"} justifyContent="center">
  174. <Grid container maxWidth justifyContent="flex-start">
  175. {editMode ?
  176. <>
  177. <Grid item sx={{ ml: 3, mr: 3 }}>
  178. <Button
  179. size="large"
  180. variant="contained"
  181. onClick={loadDataFun}
  182. sx={{
  183. textTransform: 'capitalize',
  184. alignItems: 'end'
  185. }}
  186. >
  187. <Typography variant="h5">Reset & Back</Typography>
  188. </Button>
  189. </Grid>
  190. <Grid item sx={{ ml: 3, mr: 3 }}>
  191. <Button
  192. size="large"
  193. variant="contained"
  194. type="submit"
  195. color="success"
  196. sx={{
  197. textTransform: 'capitalize',
  198. alignItems: 'end'
  199. }}
  200. >
  201. <Typography variant="h5">Save</Typography>
  202. </Button>
  203. </Grid>
  204. </>
  205. :
  206. <>
  207. <Grid item sx={{ ml: 3, mr: 3 }}>
  208. <Button
  209. size="large"
  210. variant="contained"
  211. sx={{
  212. textTransform: 'capitalize',
  213. alignItems: 'end'
  214. }}
  215. onClick={onEditClick}
  216. >
  217. <Typography variant="h5">Edit</Typography>
  218. </Button>
  219. </Grid>
  220. </>
  221. }
  222. </Grid>
  223. </Grid>
  224. {/*end top button*/}
  225. <div style={{ paddingLeft: 24, paddingRight: 24 }}>
  226. <Typography variant="h4" sx={{ mt: 3, mb: 2, mr: 3, borderBottom: "1px solid black" }}>
  227. Organisation User Details
  228. </Typography>
  229. <Grid container spacing={1}>
  230. <Grid item lg={4}>
  231. {FieldUtils.getTextField({
  232. label: "Username:",
  233. valueName: "username",
  234. disabled: true,
  235. form: formik
  236. })}
  237. </Grid>
  238. <Grid item lg={4}>
  239. {FieldUtils.getTextField({
  240. label: "Name:",
  241. valueName: "contactPerson",
  242. disabled: (!editMode),
  243. form: formik
  244. })}
  245. </Grid>
  246. <Grid item lg={4}>
  247. {FieldUtils.getTextField({
  248. label: "Created Date:",
  249. valueName: "createDate",
  250. disabled: true,
  251. form: formik
  252. })}
  253. </Grid>
  254. <Grid item lg={4}>
  255. {FieldUtils.getTextField({
  256. label: "Email:",
  257. valueName: "emailBus",
  258. disabled: (!editMode),
  259. form: formik
  260. })}
  261. </Grid>
  262. <Grid item lg={4}>
  263. {FieldUtils.getPhoneField({
  264. label: "Contact Tel:",
  265. valueName: {
  266. code: "tel_countryCode",
  267. num: "phoneNumber"
  268. },
  269. disabled: (!editMode),
  270. form: formik
  271. })}
  272. </Grid>
  273. <Grid item lg={4}>
  274. {FieldUtils.getTextField({
  275. label: "Last Updated:",
  276. valueName: "modifieDate",
  277. disabled: true,
  278. form: formik
  279. })}
  280. </Grid>
  281. <Grid item lg={4}>
  282. {FieldUtils.getComboField({
  283. label: "Organisation:",
  284. valueName: "orgId",
  285. disabled: (!editMode),
  286. dataList: orgData,
  287. filterOptions: (options, state) => {
  288. if (!state || !state.inputValue) return options;
  289. let searchStr = state.inputValue.toLowerCase().toLowerCase().trim();
  290. const displayOptions = options.filter((option) => {
  291. let brNo = option.brNo.toLowerCase().trim();
  292. let enCompanyName = option.enCompanyName ? option.enCompanyName.toLowerCase().trim() : "";
  293. let chCompanyName = option.chCompanyName ? option.chCompanyName.toLowerCase().trim() : "";
  294. return brNo.includes(searchStr) || enCompanyName.includes(searchStr) || chCompanyName.includes(searchStr);
  295. },
  296. );
  297. return displayOptions;
  298. },
  299. getOptionLabel: (item) => item ? typeof item === 'number' ? item + "" : (item["brNo"] ?
  300. <div>BR No.: {item["brNo"]}<div>Org. Name(Eng): {item["enCompanyName"] === null ? "N/A" : item["enCompanyName"]}</div><div>Org. Name(CH): {item["chCompanyName"] === null ? "N/A" : item["chCompanyName"]}</div></div> : "")
  301. : "",
  302. isOptionEqualToValue: (option, newValue, setValue, setInputValue) => {
  303. if (option.id == newValue || option.id == newValue.id) {
  304. setValue(option);
  305. setInputValue(option["brNo"]);
  306. return true;
  307. }
  308. return option == newValue || option.id == newValue.id;
  309. },
  310. onInputChange: (event, newValue, setInputValue) => {
  311. if (newValue !== "[object Object]") {
  312. setInputValue(newValue);
  313. }
  314. },
  315. onChange: (event, newValue) => {
  316. if (newValue == null) {
  317. formik.setFieldValue("orgId", "");
  318. return;
  319. }
  320. formik.setFieldValue("orgId", newValue.id);
  321. },
  322. form: formik
  323. })}
  324. </Grid>
  325. <Grid item lg={4}>
  326. <Grid container alignItems={"center"}>
  327. <Grid item xs={12} md={3} lg={3} sx={{ display: 'flex', alignItems: 'center' }}>
  328. <Typography variant="h5">Verified:</Typography>
  329. </Grid>
  330. {
  331. currentUserData.verifiedBy || editMode ?
  332. <Grid item xs={12} md={6} lg={6}>
  333. {FieldUtils.initField({
  334. valueName: "verifiedStatus",
  335. disabled: true,
  336. form: formik,
  337. })}
  338. </Grid>
  339. :
  340. <>
  341. <Grid item xs={10} md={4} lg={4}>
  342. {FieldUtils.initField({
  343. valueName: "verifiedStatus",
  344. disabled: true,
  345. form: formik,
  346. })}
  347. </Grid>
  348. <Grid item xs={1} md={1} lg={1}>
  349. <Button
  350. size="large"
  351. variant="contained"
  352. sx={{
  353. textTransform: 'capitalize',
  354. alignItems: 'end'
  355. }}
  356. onClick={() => { onVerifiedClick() }}
  357. >
  358. <Typography variant="h5">Verify</Typography>
  359. </Button>
  360. </Grid>
  361. </>
  362. }
  363. </Grid>
  364. </Grid>
  365. <Grid item lg={4}>
  366. {FieldUtils.getTextField({
  367. label: "Last Login:",
  368. valueName: "lastLoginDate",
  369. disabled: true,
  370. form: formik
  371. })}
  372. </Grid>
  373. <Grid item lg={8}></Grid>
  374. <Grid item lg={4}>
  375. <Grid container alignItems={"center"}>
  376. <Grid item xs={12} md={3} lg={3} sx={{ display: 'flex', alignItems: 'center' }}>
  377. <Typography variant="h5">Status:</Typography>
  378. </Grid>
  379. {
  380. editMode ?
  381. <Grid item xs={12} md={6} lg={6}>
  382. {FieldUtils.initField({
  383. valueName: "status",
  384. disabled: true,
  385. form: formik,
  386. })}
  387. </Grid>
  388. :
  389. <>
  390. <Grid item xs={10} md={4} lg={4}>
  391. {FieldUtils.initField({
  392. valueName: "status",
  393. disabled: true,
  394. form: formik,
  395. })}
  396. </Grid>
  397. {formik.values.locked ?
  398. <Grid item xs={1} md={1} lg={1} sx={{ ml: 1 }}>
  399. <Button
  400. size="large"
  401. variant="contained"
  402. color="success"
  403. sx={{
  404. textTransform: 'capitalize',
  405. alignItems: 'end'
  406. }}
  407. onClick={doUnlock}
  408. >
  409. <Typography variant="h5">Active</Typography>
  410. </Button>
  411. </Grid>
  412. :
  413. <Grid item xs={1} md={1} lg={1} sx={{ ml: 1 }}>
  414. <Button
  415. size="large"
  416. variant="contained"
  417. color="error"
  418. sx={{
  419. textTransform: 'capitalize',
  420. alignItems: 'end',
  421. }}
  422. onClick={doLock}
  423. >
  424. <Typography variant="h5">Lock</Typography>
  425. </Button>
  426. </Grid>
  427. }
  428. </>
  429. }
  430. </Grid>
  431. </Grid>
  432. </Grid>
  433. </div>
  434. <br />
  435. <div style={{ paddingLeft: 24, paddingRight: 24 }}>
  436. <Grid container spacing={1} >
  437. <Grid item lg={12}>
  438. <Grid container alignItems={"center"}>
  439. {currentUserData.orgId == null ?
  440. <Grid item lg={2} >
  441. <Button variant="contained"
  442. onClick={createOrgClick}
  443. >
  444. <Typography variant="h5">Create Organisation</Typography>
  445. </Button>
  446. </Grid>
  447. : null
  448. }
  449. </Grid>
  450. </Grid>
  451. <Grid item lg={12} >
  452. <Typography variant="h4" sx={{ mt: 3, mb: 2, mr: 3, borderBottom: "1px solid black" }}>
  453. Organisation
  454. </Typography>
  455. </Grid>
  456. <Grid item lg={4}>
  457. {FieldUtils.getTextField({
  458. label: "Org.Name (English):",
  459. valueName: "enCompanyName",
  460. disabled: true,
  461. form: formik
  462. })}
  463. </Grid>
  464. <Grid item lg={4}>
  465. {FieldUtils.getTextField({
  466. label: "Org.Name (Chinese):",
  467. valueName: "chCompanyName",
  468. disabled: true,
  469. form: formik
  470. })}
  471. </Grid>
  472. <Grid item lg={4}>
  473. {FieldUtils.getTextField({
  474. label: "BR No.:",
  475. valueName: "brNo",
  476. disabled: true,
  477. form: formik
  478. })}
  479. </Grid>
  480. <Grid item lg={4}>
  481. {FieldUtils.getComboField({
  482. label: "Country:",
  483. valueName: "country",
  484. dataList: ComboData.country,
  485. getOptionLabel: (option) => option.type? intl.formatMessage({ id: option.type }) : "",
  486. disabled: true,
  487. form: formik
  488. })}
  489. </Grid>
  490. <Grid item lg={4}>
  491. {FieldUtils.getPhoneField({
  492. label: "Fax No.:",
  493. valueName: {
  494. code: "fax_countryCode",
  495. num: "faxNumber"
  496. },
  497. disabled: true,
  498. form: formik
  499. })}
  500. </Grid>
  501. <Grid item lg={4}>
  502. {FieldUtils.getDateField({
  503. label: "BR Expiry Date.:",
  504. valueName: "brExpiryDate",
  505. disabled: true,
  506. form: formik
  507. })}
  508. </Grid>
  509. <Grid item lg={4}>
  510. {FieldUtils.getAddressField({
  511. label: "Address:",
  512. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  513. disabled: true,
  514. form: formik
  515. })}
  516. </Grid>
  517. <Grid item lg={4}>
  518. {FieldUtils.getComboField({
  519. label: "District:",
  520. valueName: "district",
  521. dataList: ComboData.district,
  522. getOptionLabel: (option) => option.type? intl.formatMessage({ id: option.type }) : "",
  523. disabled: true,
  524. form: formik
  525. })}
  526. </Grid>
  527. </Grid>
  528. </div>
  529. <br />
  530. </form>
  531. }
  532. <div>
  533. <Dialog open={isWarningPopUp} onClose={() => setIsWarningPopUp(false)} >
  534. <DialogTitle><Typography variant="h3">Warning</Typography></DialogTitle>
  535. <DialogContent style={{ display: 'flex', }}>
  536. <Typography variant="h4" style={{ padding: '16px' }}>{warningText}</Typography>
  537. </DialogContent>
  538. <DialogActions>
  539. <Button onClick={() => setIsWarningPopUp(false)}><Typography variant="h5">Close</Typography></Button>
  540. </DialogActions>
  541. </Dialog>
  542. </div>
  543. <div>
  544. <Dialog open={isConfirmPopUp} onClose={() => setIsConfirmPopUp(false)} >
  545. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  546. <DialogContent style={{ display: 'flex', }}>
  547. <Typography variant="h4" style={{ padding: '16px' }}>{confirmText}</Typography>
  548. </DialogContent>
  549. <DialogActions>
  550. <Button onClick={() => { setIsConfirmPopUp(false) }}><Typography variant="h5">Close</Typography></Button>
  551. <Button onClick={() => { confirmAction?.function(); }}><Typography variant="h5">Confirm</Typography></Button>
  552. </DialogActions>
  553. </Dialog>
  554. </div>
  555. </MainCard>
  556. );
  557. };
  558. export default UserInformationCard_Organization;