Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

317 Zeilen
15 KiB

  1. // material-ui
  2. import {
  3. Grid, Button, Typography,
  4. Dialog, DialogTitle, DialogContent, DialogActions,
  5. FormHelperText, TextField
  6. } from '@mui/material';
  7. import MainCard from "components/MainCard";
  8. import * as React from "react";
  9. import * as yup from 'yup';
  10. import { useEffect, useState } from "react";
  11. import * as DateUtils from 'utils/DateUtils';
  12. import * as HttpUtils from 'utils/HttpUtils';
  13. import * as UrlUtils from "utils/ApiPathConst";
  14. import * as FieldUtils from "utils/FieldUtils";
  15. import * as ComboData from "utils/ComboData";
  16. import { useNavigate } from "react-router-dom";
  17. import { useFormik } from 'formik';
  18. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  19. import Loadable from 'components/Loadable';
  20. import { lazy } from 'react';
  21. import { notifyCreateSuccess } from 'utils/CommonFunction';
  22. import { useIntl } from "react-intl";
  23. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  24. const OrganizationCard_loadFromUser = ({ userData, userId }) => {
  25. const intl = useIntl();
  26. const [currentUserData, setCurrentUserData] = useState(userData);
  27. const navigate = useNavigate();
  28. const [onReady, setOnReady] = useState(false);
  29. const [isFailPopUp, setIsFailPopUp] = useState(false);
  30. const [failText, setFailText] = useState("");
  31. const [errorMsg, setErrorMsg] = useState("");
  32. useEffect(() => {
  33. //if state data are ready and assign to different field
  34. // console.log(currentApplicationDetailData)
  35. if (Object.keys(currentUserData).length > 0) {
  36. setOnReady(true);
  37. }
  38. }, [currentUserData]);
  39. function displayErrorMsg(errorMsg) {
  40. return <Typography variant="errorMessage1">{errorMsg}</Typography>
  41. }
  42. const formik = useFormik({
  43. enableReinitialize: true,
  44. initialValues: currentUserData,
  45. validationSchema: yup.object().shape({
  46. enCompanyName: yup.string().max(255, displayErrorMsg(intl.formatMessage({ id: 'userRequireEnglishName' }))).required(displayErrorMsg(intl.formatMessage({ id: 'userRequireEnglishName' }))),
  47. chCompanyName: yup.string().max(255, displayErrorMsg(intl.formatMessage({ id: 'userRequireChineseName' }))).nullable(),
  48. addressLine1: yup.string().max(40).required(displayErrorMsg(intl.formatMessage({ id: 'validateAddressLine1' }))),
  49. addressLine2: yup.string().max(40).nullable(),
  50. addressLine3: yup.string().max(40).nullable(),
  51. fax_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({ id: 'requireDialingCode' }))).nullable(),
  52. tel_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({ id: 'requireDialingCode' }))),
  53. phoneNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({ id: 'requiredValidNumber' }))).required(displayErrorMsg(intl.formatMessage({ id: 'requireContactNumber' }))),
  54. faxNumber: yup.string().min(8).nullable(),
  55. brExpiryDate: yup.string().min(8).required(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. if (values.country == null) {
  70. setErrorMsg(intl.formatMessage({ id: 'pleaseFillInCountry' }))
  71. } else {
  72. if (values.country.type == "hongKong" && values.district == null) {
  73. setErrorMsg(intl.formatMessage({ id: 'pleaseFillInDistrict' }))
  74. } else {
  75. HttpUtils.post({
  76. url: UrlUtils.POST_ORG_SAVE_PATH,
  77. params: {
  78. id: null,
  79. primaryUserId: userId,
  80. enCompanyName: values.enCompanyName,
  81. chCompanyName: values.chCompanyName,
  82. brNo: values.brNo,
  83. brExpiryDate: values.brExpiryDate,
  84. enCompanyNameTemp: values.enCompanyNameTemp,
  85. chCompanyNameTemp: values.chCompanyNameTemp,
  86. brExpiryDateTemp: values.brExpiryDateTemp,
  87. contactPerson: values.contactPerson,
  88. contactTel: {
  89. countryCode: values.tel_countryCode,
  90. phoneNumber: values.phoneNumber
  91. },
  92. faxNo: {
  93. countryCode: values.fax_countryCode,
  94. faxNumber: values.faxNumber
  95. },
  96. addressTemp: {
  97. country: values.country.type,
  98. district: values.district?.type,
  99. addressLine1: values.addressLine1,
  100. addressLine2: values.addressLine2,
  101. addressLine3: values.addressLine3,
  102. }
  103. },
  104. onSuccess: function (responseData) {
  105. if (responseData.msg) {
  106. setFailText(responseData.msg);
  107. setIsFailPopUp(true);
  108. return;
  109. }
  110. navigate('/org/' + responseData.id);
  111. notifyCreateSuccess()
  112. }
  113. });
  114. }
  115. }
  116. }
  117. });
  118. useEffect(() => {
  119. if (Object.keys(userData).length > 0) {
  120. setCurrentUserData(userData);
  121. }
  122. }, [userData]);
  123. return (
  124. <MainCard elevation={0}
  125. border={false}
  126. content={false}
  127. >
  128. {/* <div style={{ border: "solid 1px;", color: "red" }}>
  129. TODO: Error Summary
  130. {Object.values(formik.errors).map(error => (
  131. <div>{error}</div>
  132. ))}
  133. </div> */}
  134. <div style={{ padding: 24 }}>
  135. <form onSubmit={formik.handleSubmit}>
  136. {!onReady ?
  137. <LoadingComponent />
  138. :
  139. <Grid container spacing={1}>
  140. {/*top*/}
  141. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  142. <Grid item sx={{ mr: 3 }}>
  143. <Button
  144. size="large"
  145. variant="contained"
  146. type="submit"
  147. sx={{
  148. textTransform: 'capitalize',
  149. alignItems: 'end'
  150. }}
  151. >
  152. Create
  153. </Button>
  154. </Grid>
  155. </Grid>
  156. {/*top*/}
  157. <Grid item xs={12}>
  158. <FormHelperText error id="helper-text-address1-signup">
  159. <Typography variant="errorMessage1">
  160. {errorMsg}
  161. </Typography>
  162. </FormHelperText>
  163. </Grid>
  164. <Grid item lg={4}>
  165. {FieldUtils.getTextField({
  166. label: "BR No.:",
  167. valueName: "brNo",
  168. form: formik
  169. })}
  170. </Grid>
  171. <Grid item lg={8}></Grid>
  172. <Grid item lg={4}>
  173. {FieldUtils.getTextField({
  174. label: FieldUtils.notNullFieldLabel("Name (Eng):"),
  175. valueName: "enCompanyName",
  176. form: formik
  177. })}
  178. </Grid>
  179. <Grid item lg={4}>
  180. {FieldUtils.getTextField({
  181. label: "Name (Ch):",
  182. valueName: "chCompanyName",
  183. form: formik
  184. })}
  185. </Grid>
  186. <Grid item lg={4} >
  187. <Grid container alignItems={"center"}>
  188. <Grid item xs={12} md={3} lg={3} sx={{ display: 'flex', alignItems: 'center' }}>
  189. <Typography variant="pnspsFormParagraphBold">{FieldUtils.notNullFieldLabel("Expiry Date:")}</Typography>
  190. </Grid>
  191. <Grid item xs={12} md={6} lg={6}>
  192. <TextField
  193. fullWidth
  194. id="brExpiryDate"
  195. name="brExpiryDate"
  196. type="date"
  197. inputProps={{ min: DateUtils.dateValue(new Date()) }}
  198. error={Boolean(formik.errors["brExpiryDate"])}
  199. helperText={formik.errors["brExpiryDate"] ? formik.errors["brExpiryDate"] : ''}
  200. onChange={formik.handleChange}
  201. value={formik.values["brExpiryDate"]}
  202. sx={{
  203. width: '100%'
  204. }}
  205. />
  206. </Grid>
  207. </Grid>
  208. </Grid>
  209. <Grid item lg={4}>
  210. {FieldUtils.getTextField({
  211. label: FieldUtils.notNullFieldLabel("Contact Person:"),
  212. valueName: "contactPerson",
  213. form: formik
  214. })}
  215. </Grid>
  216. <Grid item lg={4}>
  217. {FieldUtils.getPhoneField({
  218. label: FieldUtils.notNullFieldLabel("Contact Tel:"),
  219. valueName: {
  220. code: "tel_countryCode",
  221. num: "phoneNumber"
  222. },
  223. form: formik
  224. })}
  225. </Grid>
  226. <Grid item lg={4}>
  227. {FieldUtils.getPhoneField({
  228. label: "Fax No:",
  229. valueName: {
  230. code: "fax_countryCode",
  231. num: "faxNumber"
  232. },
  233. form: formik
  234. })}
  235. </Grid>
  236. <Grid item lg={4}>
  237. {FieldUtils.getComboField({
  238. label: FieldUtils.notNullFieldLabel("Country:"),
  239. valueName: "country",
  240. dataList: ComboData.country,
  241. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  242. form: formik
  243. })}
  244. </Grid>
  245. <Grid item lg={4}>
  246. {FieldUtils.getComboField({
  247. label: FieldUtils.notNullFieldLabel("District:"),
  248. valueName: "district",
  249. dataList: ComboData.district,
  250. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  251. form: formik
  252. })}
  253. </Grid>
  254. <Grid item lg={4}>
  255. {FieldUtils.getAddressField({
  256. label: FieldUtils.notNullFieldLabel("Address:"),
  257. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  258. form: formik
  259. })}
  260. </Grid>
  261. </Grid>
  262. }
  263. </form>
  264. </div>
  265. <div>
  266. <Dialog
  267. open={isFailPopUp}
  268. onClose={() => setIsFailPopUp(false)}
  269. PaperProps={{
  270. sx: {
  271. minWidth: '40vw',
  272. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  273. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  274. }
  275. }}
  276. >
  277. <DialogTitle><Typography variant="h3">Action Fail</Typography></DialogTitle>
  278. <DialogContent style={{ display: 'flex', }}>
  279. <Typography variant="h4" style={{ padding: '16px' }}>{failText}</Typography>
  280. </DialogContent>
  281. <DialogActions>
  282. <Button onClick={() => setIsFailPopUp(false)}><Typography variant="h5">OK</Typography></Button>
  283. </DialogActions>
  284. </Dialog>
  285. </div>
  286. </MainCard>
  287. );
  288. };
  289. export default OrganizationCard_loadFromUser;