Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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