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.
 
 

518 line
23 KiB

  1. // material-ui
  2. import {
  3. Grid, Button
  4. } from '@mui/material';
  5. import MainCard from "../../components/MainCard";
  6. import * as React from "react";
  7. import { useEffect, useState } from "react";
  8. import * as yup from 'yup';
  9. import { useFormik } from 'formik';
  10. import * as FieldUtils from "../../utils/FieldUtils";
  11. import * as HttpUtils from '../../utils/HttpUtils';
  12. import * as UrlUtils from "../../utils/ApiPathConst";
  13. import * as ComboData from "../../utils/ComboData";
  14. const LoadingComponent = Loadable(lazy(() => import('../extra-pages/LoadingComponent')));
  15. import Loadable from 'components/Loadable';
  16. import { lazy } from 'react';
  17. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  18. const UserInformationCard_Individual = ({ formData, loadDataFun }) => {
  19. const [currentUserData, setCurrentUserData] = useState(formData);
  20. const [editMode, setEditMode] = useState(false);
  21. const [locked, setLocked] = useState(false);
  22. const [onReady, setOnReady] = useState(false);
  23. useEffect(() => {
  24. //if state data are ready and assign to different field
  25. // console.log(currentApplicationDetailData)
  26. if (Object.keys(currentUserData).length > 0) {
  27. setOnReady(true);
  28. }
  29. }, [currentUserData]);
  30. const formik = useFormik({
  31. enableReinitialize: true,
  32. initialValues: currentUserData,
  33. validationSchema: yup.object().shape({
  34. enName: yup.string().max(255).required('請輸入英文姓名'),
  35. chName: yup.string().max(255).required('請輸入中文姓名'),
  36. addressLine1: yup.string().max(255).required('請輸入第一行地址'),
  37. addressLine2: yup.string().max(255).nullable(),
  38. addressLine3: yup.string().max(255).nullable(),
  39. emailAddress: yup.string().email('請輸入電郵格式').max(255).required('請輸入電郵'),
  40. identification: yup.string().min(7, "請輸入證件號碼").required('請輸入證件號碼'),
  41. checkDigit: yup.string().max(1).required('請輸入括號內的數字或字母').nullable(),
  42. idDocType: yup.string().max(255).required('請輸入證件類別'),
  43. tel_countryCode: yup.string().min(3, '請輸入3位數字').required('請輸入國際區號'),
  44. fax_countryCode: yup.string().min(3, '請輸入3位數字'),
  45. phoneNumber: yup.string().min(8, '請輸入8位數字').required('請輸入聯絡電話'),
  46. faxNumber: yup.string().min(8, '請輸入8位數字').nullable(),
  47. }),
  48. onSubmit: values => {
  49. console.log(values);
  50. HttpUtils.post({
  51. url: UrlUtils.POST_IND_USER + "/" + formData.id,
  52. params: {
  53. prefix: values.prefix,
  54. enName: values.enName,
  55. chName: values.chName,
  56. idDocType: values.idDocType,
  57. mobileNumber: {
  58. countryCode: values.tel_countryCode,
  59. phoneNumber: values.phoneNumber
  60. },
  61. identification: values.identification,
  62. checkDigit: values.checkDigit,
  63. faxNo: {
  64. countryCode: values.fax_countryCode,
  65. faxNumber: values.faxNumber
  66. },
  67. emailAddress: values.emailAddress,
  68. address: {
  69. country: values.country,
  70. district: values.district,
  71. addressLine1: values.addressLine1,
  72. addressLine2: values.addressLine2,
  73. addressLine3: values.addressLine3,
  74. },
  75. },
  76. onSuccess: function () {
  77. loadDataFun();
  78. }
  79. });
  80. }
  81. });
  82. useEffect(() => {
  83. if (Object.keys(formData).length > 0) {
  84. setCurrentUserData(formData);
  85. }
  86. }, [formData]);
  87. useEffect(() => {
  88. setLocked(currentUserData.locked);
  89. }, [currentUserData]);
  90. const onEditClick = () => {
  91. setEditMode(true);
  92. };
  93. const onVerifiedClick = () => {
  94. HttpUtils.get({
  95. url: UrlUtils.GET_IND_USER_VERIFY + "/" + formData.id,
  96. onSuccess: function () {
  97. loadDataFun();
  98. }
  99. });
  100. };
  101. const doLock = () => {
  102. HttpUtils.get({
  103. url: UrlUtils.GET_USER_LOCK + "/" + formData.id,
  104. onSuccess: function () {
  105. loadDataFun();
  106. }
  107. });
  108. };
  109. const doUnlock = () => {
  110. HttpUtils.get({
  111. url: UrlUtils.GET_USER_UNLOCK + "/" + formData.id,
  112. onSuccess: function () {
  113. loadDataFun();
  114. }
  115. });
  116. };
  117. return (
  118. <MainCard elevation={0}
  119. border={false}
  120. content={false}
  121. >
  122. {!onReady?
  123. <LoadingComponent />
  124. :
  125. <form onSubmit={formik.handleSubmit} style={{ padding: 24 }}>
  126. {/*top button*/}
  127. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  128. <Grid container maxWidth justifyContent="flex-start">
  129. {editMode ?
  130. <>
  131. <Grid item sx={{ ml: 3, mr: 3 }}>
  132. <Button
  133. size="large"
  134. variant="contained"
  135. onClick={loadDataFun}
  136. sx={{
  137. textTransform: 'capitalize',
  138. alignItems: 'end'
  139. }}
  140. >
  141. Reset & Back
  142. </Button>
  143. </Grid>
  144. <Grid item sx={{ ml: 3, mr: 3 }}>
  145. <Button
  146. size="large"
  147. variant="contained"
  148. type="submit"
  149. color="success"
  150. sx={{
  151. textTransform: 'capitalize',
  152. alignItems: 'end'
  153. }}
  154. >
  155. Save
  156. </Button>
  157. </Grid>
  158. </>
  159. :
  160. <>
  161. <Grid item sx={{ ml: 3, mr: 3 }}>
  162. <Button
  163. size="large"
  164. variant="contained"
  165. sx={{
  166. textTransform: 'capitalize',
  167. alignItems: 'end'
  168. }}
  169. onClick={onEditClick}
  170. >
  171. Edit
  172. </Button>
  173. </Grid>
  174. </>
  175. }
  176. </Grid>
  177. </Grid>
  178. {/*end top button*/}
  179. <Grid container spacing={1}>
  180. <Grid item lg={4}>
  181. {FieldUtils.getTextField({
  182. label: "Username:",
  183. valueName: "username",
  184. disabled: true,
  185. form: formik
  186. })}
  187. </Grid>
  188. <Grid item lg={4}>
  189. {FieldUtils.getTextField({
  190. label: "English Name:",
  191. valueName: "enName",
  192. disabled: (!editMode),
  193. form: formik
  194. })}
  195. </Grid>
  196. <Grid item lg={4}>
  197. {FieldUtils.getTextField({
  198. label: "Created Date:",
  199. valueName: "createDate",
  200. disabled: true,
  201. form: formik
  202. })}
  203. </Grid>
  204. <Grid item lg={4}>
  205. {FieldUtils.getTextField({
  206. label: "Prefix:",
  207. valueName: "prefix",
  208. disabled: (!editMode),
  209. form: formik
  210. })}
  211. </Grid>
  212. <Grid item lg={4}>
  213. {FieldUtils.getTextField({
  214. label: "Chinese Name:",
  215. valueName: "chName",
  216. disabled: (!editMode),
  217. form: formik
  218. })}
  219. </Grid>
  220. <Grid item lg={4}>
  221. {FieldUtils.getTextField({
  222. label: "Last Updated:",
  223. valueName: "modifieDate",
  224. disabled: true,
  225. form: formik
  226. })}
  227. </Grid>
  228. <Grid item lg={4}>
  229. {FieldUtils.getComboField({
  230. label: "ID Type:",
  231. valueName: "idDocType",
  232. disabled: (!editMode),
  233. dataList: ComboData.idDocType,
  234. filterOptions: (options) => options,
  235. getOptionLabel: (item) => item ? typeof item === 'string' ? item : (item["type"] ? item["type"] + "-" + item["label"] : "") : "",
  236. onInputChange: (event, newValue, setInputValue) => {
  237. if (newValue == null) {
  238. setInputValue("");
  239. }
  240. let _val = newValue.split("-");
  241. if (_val[0]) {
  242. setInputValue(_val[0]);
  243. }
  244. },
  245. onChange: (event, newValue) => {
  246. if (newValue == null) {
  247. formik.setFieldValue("idDocType", "");
  248. return;
  249. }
  250. formik.setFieldValue("idDocType", newValue.type);
  251. },
  252. form: formik
  253. })}
  254. </Grid>
  255. <Grid item lg={4}>
  256. {FieldUtils.getPhoneField({
  257. label: "Contact Tel:",
  258. valueName: {
  259. code: "tel_countryCode",
  260. num: "phoneNumber"
  261. },
  262. disabled: (!editMode),
  263. form: formik
  264. })}
  265. </Grid>
  266. <Grid item lg={4}>
  267. <Grid container alignItems={"center"}>
  268. <Grid item xs={12} md={3} lg={3} sx={{ display: 'flex', alignItems: 'center' }}>
  269. Verified:
  270. </Grid>
  271. {
  272. currentUserData.verifiedBy || editMode ?
  273. <Grid item xs={12} md={6} lg={6}>
  274. {FieldUtils.initField({
  275. valueName: "verifiedStatus",
  276. disabled: true,
  277. form: formik,
  278. })}
  279. </Grid>
  280. :
  281. <>
  282. <Grid item xs={10} md={4} lg={4}>
  283. {FieldUtils.initField({
  284. valueName: "verifiedStatus",
  285. disabled: true,
  286. form: formik,
  287. })}
  288. </Grid>
  289. <Grid item xs={1}>
  290. <Button
  291. size="large"
  292. variant="contained"
  293. sx={{
  294. textTransform: 'capitalize',
  295. alignItems: 'end',
  296. }}
  297. onClick={onVerifiedClick}
  298. >
  299. Verify
  300. </Button>
  301. </Grid>
  302. </>
  303. }
  304. </Grid>
  305. </Grid>
  306. <Grid item lg={4}>
  307. <Grid container alignItems={"center"}>
  308. <Grid item xs={12} md={3} lg={3} sx={{ display: 'flex', alignItems: 'center' }}>
  309. ID No.:
  310. </Grid>
  311. <Grid item xs={12} md={6} lg={6}>
  312. <Grid container>
  313. {formik.values.idDocType == "HKID" ?
  314. <>
  315. <Grid item lg={8}>
  316. {FieldUtils.initField({
  317. valueName: "identification",
  318. disabled: (!editMode),
  319. form: formik,
  320. placeholder: "證件號碼",
  321. inputProps: {
  322. maxLength: 7,
  323. onKeyDown: (e) => {
  324. if (e.key === 'Enter') {
  325. e.preventDefault();
  326. }
  327. },
  328. }
  329. })}
  330. </Grid>
  331. <Grid item lg={4}>
  332. {FieldUtils.initField({
  333. valueName: "checkDigit",
  334. disabled: (!editMode),
  335. form: formik
  336. })}
  337. </Grid>
  338. </> :
  339. <Grid item lg={12}>
  340. {FieldUtils.initField({
  341. valueName: "identification",
  342. disabled: (!editMode),
  343. form: formik
  344. })}
  345. </Grid>
  346. }
  347. </Grid>
  348. </Grid>
  349. </Grid>
  350. </Grid>
  351. <Grid item lg={4}>
  352. {FieldUtils.getPhoneField({
  353. label: "Fax No.:",
  354. valueName: {
  355. code: "fax_countryCode",
  356. num: "faxNumber"
  357. },
  358. disabled: (!editMode),
  359. form: formik
  360. })}
  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={4}>
  371. {FieldUtils.getComboField({
  372. label: "Country:",
  373. valueName: "country",
  374. dataList: ComboData.country,
  375. disabled: (!editMode),
  376. form: formik
  377. })}
  378. </Grid>
  379. <Grid item lg={4}>
  380. {FieldUtils.getTextField({
  381. label: "Email:",
  382. valueName: "emailAddress",
  383. disabled: (!editMode),
  384. form: formik
  385. })}
  386. </Grid>
  387. <Grid item lg={4}>
  388. <Grid container alignItems={"center"}>
  389. <Grid item xs={12} md={3} lg={3} sx={{ display: 'flex', alignItems: 'center' }}>
  390. Status:
  391. </Grid>
  392. {
  393. editMode ?
  394. <Grid item xs={12} md={6} lg={6}>
  395. {FieldUtils.initField({
  396. valueName: "status",
  397. disabled: true,
  398. form: formik,
  399. })}
  400. </Grid>
  401. :
  402. <>
  403. <Grid item lg={4}>
  404. {FieldUtils.initField({
  405. valueName: "status",
  406. disabled: true,
  407. form: formik,
  408. })}
  409. </Grid>
  410. {locked ?
  411. <Grid item lg={1}>
  412. <Button
  413. size="large"
  414. variant="contained"
  415. color="success"
  416. sx={{
  417. textTransform: 'capitalize',
  418. alignItems: 'end'
  419. }}
  420. onClick={doUnlock}
  421. >
  422. Active
  423. </Button>
  424. </Grid>
  425. :
  426. <Grid item lg={1}>
  427. <Button
  428. size="large"
  429. variant="contained"
  430. color="error"
  431. sx={{
  432. textTransform: 'capitalize',
  433. alignItems: 'end'
  434. }}
  435. onClick={doLock}
  436. >
  437. Lock
  438. </Button>
  439. </Grid>
  440. }
  441. </>
  442. }
  443. </Grid>
  444. </Grid>
  445. <Grid item lg={4}>
  446. {FieldUtils.getAddressField({
  447. label: "Address:",
  448. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  449. disabled: (!editMode),
  450. form: formik
  451. })}
  452. </Grid>
  453. <Grid item lg={4}>
  454. {FieldUtils.getComboField({
  455. label: "District:",
  456. valueName: "district",
  457. dataList: ComboData.district,
  458. disabled: (!editMode),
  459. form: formik
  460. })}
  461. </Grid>
  462. </Grid>
  463. </form>
  464. }
  465. </MainCard>
  466. );
  467. };
  468. export default UserInformationCard_Individual;