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.
 
 

401 rivejä
21 KiB

  1. // material-ui
  2. import {
  3. Grid, Button, Typography,
  4. FormHelperText,
  5. Stack,
  6. IconButton
  7. } from '@mui/material';
  8. import MainCard from "components/MainCard";
  9. import * as React from "react";
  10. import { useEffect, useState } from "react";
  11. import * as yup from 'yup';
  12. import { useFormik } from 'formik';
  13. import * as FieldUtils from "utils/FieldUtils";
  14. import * as HttpUtils from 'utils/HttpUtils';
  15. import * as UrlUtils from "utils/ApiPathConst";
  16. import * as ComboData from "utils/ComboData";
  17. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  18. import Loadable from 'components/Loadable';
  19. import { lazy } from 'react';
  20. import {notifySaveSuccess,} from 'utils/CommonFunction';
  21. import {FormattedMessage, useIntl} from "react-intl";
  22. import {PNSPS_BUTTON_THEME} from "../../../themes/buttonConst";
  23. import {ThemeProvider} from "@emotion/react";
  24. import { EyeInvisibleOutlined, EyeOutlined } from '@ant-design/icons';
  25. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  26. const UserInformationCard_Individual_Pub = ({ formData, loadDataFun }) => {
  27. const intl = useIntl();
  28. const [currentUserData, setCurrentUserData] = useState({});
  29. const [editMode, setEditMode] = useState(false);
  30. const [onReady, setOnReady] = useState(false);
  31. const [errorMsg, setErrorMsg] = useState("");
  32. const [showId, setshowId] = useState(false);
  33. const handleClickShowId = () => {
  34. setshowId(!showId);
  35. };
  36. const handleMouseDownId = (event) => {
  37. event.preventDefault();
  38. };
  39. useEffect(() => {
  40. if (Object.keys(formData).length > 0) {
  41. setCurrentUserData(formData);
  42. }
  43. }, [formData]);
  44. useEffect(() => {
  45. //if state data are ready and assign to different field
  46. // console.log(currentApplicationDetailData)
  47. if (Object.keys(currentUserData).length > 0) {
  48. setOnReady(true);
  49. }
  50. }, [currentUserData]);
  51. function getMaxErrStr(num, fieldname){
  52. return intl.formatMessage({ id: 'noMoreThenNWords' },{num:num, fieldname:fieldname?intl.formatMessage({ id: fieldname})+": ":""});
  53. }
  54. const formik = useFormik({
  55. enableReinitialize: true,
  56. initialValues: currentUserData,
  57. validationSchema: yup.object().shape({
  58. enName: yup.string().max(40, getMaxErrStr(40)).when('chName', {
  59. is: (chName) => chName?false:true,
  60. then: yup.string().required(intl.formatMessage({ id: 'userRequireEnglishName' }))
  61. }).nullable(),
  62. chName: yup.string().max(6, getMaxErrStr(6)).nullable(),
  63. addressLine1: yup.string().max(40).required(intl.formatMessage({id: 'validateAddressLine1'})),
  64. addressLine2: yup.string().max(40).nullable(),
  65. addressLine3: yup.string().max(40).nullable(),
  66. emailAddress: yup.string().email(intl.formatMessage({id: 'validEmailFormat'})).max(255).required(intl.formatMessage({id: 'requireEmail'})),
  67. tel_countryCode: yup.string().min(3, intl.formatMessage({id: 'require3Number'})).required(intl.formatMessage({id: 'requireDialingCode'})),
  68. fax_countryCode: yup.string().min(3, intl.formatMessage({id: 'require3Number'})),
  69. phoneNumber: yup.string().min(8, intl.formatMessage({id: 'require8Number'})).required(intl.formatMessage({id: 'requireContactNumber'})),
  70. faxNumber: yup.string().min(8, intl.formatMessage({id: 'require8Number'})).nullable(),
  71. }),
  72. onSubmit: values => {
  73. // console.log(values)
  74. if (values.country==null){
  75. setErrorMsg(intl.formatMessage({id: 'pleaseFillInCountry'}))
  76. } else {
  77. if (values.country.type == "hongKong" && values.district == null){
  78. setErrorMsg(intl.formatMessage({id: 'pleaseFillInDistrict'}))
  79. } else {
  80. HttpUtils.post({
  81. url: UrlUtils.POST_PUB_IND_USER,
  82. params: {
  83. enName: values.enName,
  84. chName: values.chName,
  85. mobileNumber: {
  86. countryCode: values.tel_countryCode,
  87. phoneNumber: values.phoneNumber
  88. },
  89. faxNo: {
  90. countryCode: values.fax_countryCode,
  91. faxNumber: values.faxNumber
  92. },
  93. address: {
  94. country: values.country.type,
  95. district: values.district?.type,
  96. addressLine1: values.addressLine1,
  97. addressLine2: values.addressLine2,
  98. addressLine3: values.addressLine3,
  99. },
  100. preferLocale: values.preferLocale.type
  101. },
  102. onSuccess: function () {
  103. notifySaveSuccess();
  104. window.location.reload();
  105. }
  106. });
  107. }
  108. }
  109. }
  110. });
  111. const onEditClick = () => {
  112. setEditMode(true);
  113. };
  114. return (
  115. <MainCard elevation={0}
  116. border={false}
  117. content={false}
  118. >
  119. {!onReady ?
  120. <LoadingComponent />
  121. :
  122. <form onSubmit={formik.handleSubmit} style={{ padding: 12 }}>
  123. {/*top button*/}
  124. <Grid item xs={12} sm={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  125. <Grid container maxWidth justifyContent="flex-start">
  126. {editMode ?
  127. <>
  128. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  129. <Grid item sx={{ mr: 3 }}>
  130. <Button
  131. aria-label={intl.formatMessage({id: 'back'})}
  132. variant="contained"
  133. color="cancel"
  134. onClick={loadDataFun}
  135. >
  136. <FormattedMessage id="resetAndBack" />
  137. </Button>
  138. </Grid>
  139. <Grid item sx={{ ml: 3, mr: 3 }}>
  140. <Button
  141. aria-label={intl.formatMessage({id: 'save'})}
  142. variant="contained"
  143. type="submit"
  144. color="success"
  145. >
  146. <FormattedMessage id="save" />
  147. </Button>
  148. </Grid>
  149. </ThemeProvider>
  150. </>
  151. :
  152. <>
  153. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  154. <Grid item sx={{ mr: 3 }}>
  155. <Button
  156. aria-label={intl.formatMessage({id: 'edit'})}
  157. variant="contained"
  158. onClick={onEditClick}
  159. >
  160. <FormattedMessage id="edit" />
  161. </Button>
  162. </Grid>
  163. </ThemeProvider>
  164. </>
  165. }
  166. </Grid>
  167. </Grid>
  168. {/*end top button*/}
  169. <Typography variant="h4" sx={{ mt: 3, mb: 2, borderBottom: "1px solid black" }}>
  170. <FormattedMessage id="userDetail" />
  171. </Typography>
  172. <Grid item xs={12} sm={12} md={12} lg={12}>
  173. <Grid container>
  174. <Grid item xs={12}>
  175. <FormHelperText error id="helper-text-address1-signup">
  176. <Typography variant="errorMessage1">
  177. {errorMsg}
  178. </Typography>
  179. </FormHelperText>
  180. </Grid>
  181. <Grid item xs={12} sm={12} md={12} lg={4} >
  182. {FieldUtils.getTextField({
  183. label: intl.formatMessage({id: 'userLoginName'}) + ":",
  184. valueName: "username",
  185. disabled: true,
  186. form: formik
  187. })}
  188. </Grid>
  189. <Grid item xs={12} sm={12} md={12} lg={4}>
  190. {FieldUtils.getTextField({
  191. label: intl.formatMessage({id: 'userEnglishName'}) + ":",
  192. valueName: "enName",
  193. disabled: true,
  194. form: formik
  195. })}
  196. </Grid>
  197. <Grid item xs={12} sm={12} md={12} lg={4}>
  198. {FieldUtils.getTextField({
  199. label: intl.formatMessage({id: 'userChineseName'}) + ":",
  200. valueName: "chName",
  201. disabled: true,
  202. form: formik
  203. })}
  204. </Grid>
  205. <Grid item xs={12} sm={12} md={12} lg={4}>
  206. {FieldUtils.getComboField({
  207. label: intl.formatMessage({id: 'idType'}) + ":",
  208. valueName: "idDocType",
  209. disabled: true,
  210. dataList: ComboData.idDocType,
  211. getOptionLabel: (option) => option? intl.formatMessage({ id: option }) : "",
  212. form: formik
  213. })}
  214. </Grid>
  215. <Grid xs={12} sm={12} md={12} lg={4}>
  216. <Grid container alignItems={"center"} spacing={1} sx={{mb:2}} >
  217. <Grid item xs={12} sm={12} md={3} lg={3} sx={{ display: 'flex', alignItems: 'center' }}>
  218. <Typography variant="pnspsFormParagraphBold">
  219. <FormattedMessage id="idNo" />:
  220. </Typography>
  221. </Grid>
  222. <Grid item xs={12} sm={12} md={9} lg={6}>
  223. <Grid container>
  224. {formik.values.idDocType === "HKID" ?
  225. // <>
  226. // <Grid item xs={6} sm={6} md={6} lg={7.5} sx={{mr:1}}>
  227. // {FieldUtils.initField({
  228. // valueName: "identification",
  229. // disabled: true,
  230. // form: formik,
  231. // placeholder: intl.formatMessage({id: 'idDocNumber'}),
  232. // inputProps: {
  233. // maxLength: 7,
  234. // onKeyDown: (e) => {
  235. // if (e.key === 'Enter') {
  236. // e.preventDefault();
  237. // }
  238. // },
  239. // }
  240. // })}
  241. // </Grid>
  242. // <Grid item xs={2} sm={2} md={2} lg={2} style={{minWidth:40}}>
  243. // {FieldUtils.initField({
  244. // valueName: "checkDigit",
  245. // disabled: true,
  246. // form: formik,
  247. // })}
  248. // </Grid>
  249. // </>
  250. <Stack direction="row">
  251. <Typography variant="h5" mt={1}>
  252. {formik.values.identification.slice(0, 4)}
  253. </Typography>
  254. <Typography variant="h5"mt={1}>
  255. {showId ?formik.values.identification.slice(4):"****"}{showId ? '(' + formik.values.checkDigit + ')' :null}
  256. </Typography>
  257. <IconButton
  258. aria-label="toggle id visibility"
  259. onClick={handleClickShowId}
  260. onMouseDown={handleMouseDownId}
  261. edge="end"
  262. size="large"
  263. >
  264. {showId ? <EyeOutlined /> : <EyeInvisibleOutlined />}
  265. </IconButton>
  266. </Stack>
  267. :
  268. // <Grid item xs={10} sm={4} md={4} lg={10}>
  269. // {FieldUtils.initField({
  270. // valueName: "identification",
  271. // disabled: true,
  272. // form: formik
  273. // })}
  274. // </Grid>
  275. <Stack direction="row">
  276. <Typography variant="h5" mt={1}>
  277. {formik.values.identification.slice(0, 4)}
  278. </Typography>
  279. <Typography variant="h5"mt={1}>
  280. {showId ?formik.values.identification.slice(4):"****"}
  281. </Typography>
  282. <IconButton
  283. aria-label="toggle id visibility"
  284. onClick={handleClickShowId}
  285. onMouseDown={handleMouseDownId}
  286. edge="end"
  287. size="large"
  288. >
  289. {showId ? <EyeOutlined /> : <EyeInvisibleOutlined />}
  290. </IconButton>
  291. </Stack>
  292. }
  293. </Grid>
  294. </Grid>
  295. </Grid>
  296. </Grid>
  297. <Grid item xs={12} sm={12} md={12} lg={4}>
  298. {FieldUtils.getPhoneField({
  299. label: intl.formatMessage({id: 'userContactNumber'}) + ":",
  300. valueName: {
  301. code: "tel_countryCode",
  302. num: "phoneNumber"
  303. },
  304. disabled: (!editMode),
  305. form: formik
  306. })}
  307. </Grid>
  308. <Grid item xs={12} sm={12} md={12} lg={4}>
  309. {FieldUtils.getComboField({
  310. label: intl.formatMessage({id: 'country'}) + ":",
  311. valueName: "country",
  312. getOptionLabel: (option) => option.type? intl.formatMessage({ id: option.type }) : "",
  313. dataList: ComboData.country,
  314. disabled: (!editMode),
  315. form: formik
  316. })}
  317. </Grid>
  318. <Grid item xs={12} sm={12} md={12} lg={4}>
  319. {FieldUtils.getTextField({
  320. label: intl.formatMessage({id: 'userContactEmail'}) + ":",
  321. valueName: "emailAddress",
  322. disabled: true,
  323. form: formik
  324. })}
  325. </Grid>
  326. <Grid item xs={12} sm={12} md={12} lg={4}>
  327. {FieldUtils.getPhoneField({
  328. label: intl.formatMessage({id: 'userFaxNumber'}) + ":",
  329. valueName: {
  330. code: "fax_countryCode",
  331. num: "faxNumber"
  332. },
  333. disabled: (!editMode),
  334. form: formik
  335. })}
  336. </Grid>
  337. <Grid item xs={12} sm={12} md={12} lg={4}>
  338. {FieldUtils.getComboField({
  339. label: intl.formatMessage({id: 'district'}) + ":",
  340. valueName: "district",
  341. dataList: ComboData.district,
  342. getOptionLabel: (option) => option.type? intl.formatMessage({ id: option.type }) : "",
  343. disabled: (!editMode),
  344. form: formik
  345. })}
  346. </Grid>
  347. <Grid item xs={12} sm={12} md={12} lg={4}>
  348. {FieldUtils.getComboField({
  349. label: intl.formatMessage({id: 'language'}) + ":",
  350. valueName: "preferLocale",
  351. dataList: ComboData.Locale,
  352. getOptionLabel: (option) => option.label? option.label: "",
  353. disabled: (!editMode),
  354. form: formik
  355. })}
  356. </Grid>
  357. <Grid item xs={12} sm={12} md={12} lg={12}>
  358. {FieldUtils.getAddressField({
  359. label: intl.formatMessage({id: 'userAddress'}) + ":",
  360. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  361. disabled: (!editMode),
  362. form: formik
  363. })}
  364. </Grid>
  365. </Grid>
  366. </Grid>
  367. </form>
  368. }
  369. </MainCard>
  370. );
  371. };
  372. export default UserInformationCard_Individual_Pub;