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.
 
 

379 lines
17 KiB

  1. // material-ui
  2. import {
  3. Grid, Button,
  4. // Checkbox, FormControlLabel,
  5. Typography,
  6. Dialog, DialogTitle, DialogContent, DialogActions,
  7. FormHelperText
  8. } from '@mui/material';
  9. // import { FormControlLabel } from '@material-ui/core';
  10. import MainCard from "components/MainCard";
  11. import * as React from "react";
  12. import { useFormik } from 'formik';
  13. import * as yup from 'yup';
  14. import { useEffect, useState } from "react";
  15. import * as HttpUtils from 'utils/HttpUtils';
  16. import * as UrlUtils from "utils/ApiPathConst";
  17. import * as FieldUtils from "utils/FieldUtils";
  18. import * as ComboData from "utils/ComboData";
  19. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  20. import Loadable from 'components/Loadable';
  21. import { lazy } from 'react';
  22. import { notifySaveSuccess } from 'utils/CommonFunction';
  23. import { FormattedMessage, useIntl } from "react-intl";
  24. import { PNSPS_BUTTON_THEME } from "themes/buttonConst";
  25. import { ThemeProvider } from "@emotion/react";
  26. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  27. const OrganizationPubCard = ({ userData, loadDataFun, id, setEditModeFun }) => {
  28. const intl = useIntl();
  29. const [creditorConfirmPopUp, setCreditorConfirmPopUp] = React.useState(false);
  30. const [nonCreditorConfirmPopUp, setNonCreditorConfirmPopUp] = React.useState(false);
  31. const [currentUserData, setCurrentUserData] = useState({});
  32. const [editMode, setEditMode] = useState(false);
  33. const [createMode, setCreateMode] = useState(false);
  34. const [onReady, setOnReady] = useState(false);
  35. const [errorMsg, setErrorMsg] = useState("");
  36. useEffect(() => {
  37. //if state data are ready and assign to different field
  38. // console.log(currentApplicationDetailData)
  39. if (Object.keys(currentUserData).length > 0) {
  40. setOnReady(true);
  41. }
  42. }, [currentUserData]);
  43. function displayErrorMsg(errorMsg) {
  44. return <Typography variant="errorMessage1">{errorMsg}</Typography>
  45. }
  46. const formik = useFormik({
  47. enableReinitialize: true,
  48. initialValues: currentUserData,
  49. validationSchema: yup.object().shape({
  50. addressLine1: yup.string().max(40).required(displayErrorMsg(intl.formatMessage({ id: 'validateAddressLine1' }))),
  51. addressLine2: yup.string().max(40, displayErrorMsg(intl.formatMessage({ id: 'noMoreThen40Words' }))),
  52. addressLine3: yup.string().max(40, displayErrorMsg(intl.formatMessage({ id: 'noMoreThen40Words' }))),
  53. tel_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({ id: 'requireDialingCode' }))),
  54. phoneNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({ id: 'requiredValidNumber' }))).required(displayErrorMsg(intl.formatMessage({ id: 'requireContactNumber' }))),
  55. faxNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({ id: 'require8Number' }))).nullable(),
  56. }),
  57. onSubmit: values => {
  58. if (values.country == null) {
  59. setErrorMsg(intl.formatMessage({ id: 'pleaseFillInCountry' }))
  60. } else {
  61. if (values.country.type == "hongKong" && values.district == null) {
  62. setErrorMsg(intl.formatMessage({ id: 'pleaseFillInDistrict' }))
  63. } else {
  64. HttpUtils.post({
  65. url: UrlUtils.POST_PUB_ORG_SAVE_PATH,
  66. params: {
  67. contactPerson: values.contactPerson,
  68. contactTel: {
  69. countryCode: values.tel_countryCode,
  70. phoneNumber: values.phoneNumber
  71. },
  72. faxNo: {
  73. countryCode: values.fax_countryCode,
  74. faxNumber: values.faxNumber
  75. },
  76. addressTemp: {
  77. country: values.country.type,
  78. district: values.district?.type,
  79. addressLine1: values.addressLine1,
  80. addressLine2: values.addressLine2,
  81. addressLine3: values.addressLine3,
  82. },
  83. //creditor: values.creditor,
  84. },
  85. onSuccess: function () {
  86. notifySaveSuccess()
  87. loadDataFun();
  88. setEditMode(false);
  89. }
  90. });
  91. }
  92. }
  93. }
  94. });
  95. useEffect(() => {
  96. setEditModeFun(editMode);
  97. }, [editMode]);
  98. useEffect(() => {
  99. if (Object.keys(userData).length > 0) {
  100. setCreateMode(id <= 0);
  101. setEditMode(id <= 0);
  102. setCurrentUserData(userData);
  103. }
  104. }, [userData]);
  105. // useEffect(() => {
  106. // if (Object.keys(userData).length > 0) {
  107. // if(userData.creditor === undefined||userData.creditor === null){
  108. // userData.creditor = false
  109. // }
  110. // setCurrentUserData(userData);
  111. // }
  112. // }, []);
  113. const onEditClick = () => {
  114. setEditMode(true);
  115. };
  116. return (
  117. <MainCard elevation={0}
  118. border={false}
  119. content={false}
  120. >
  121. <form onSubmit={formik.handleSubmit} style={{ padding: 24 }}>
  122. {/*top*/}
  123. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  124. <Grid container maxWidth justifyContent="flex-start">
  125. {editMode ?
  126. <>
  127. {createMode ?
  128. <>
  129. <Grid item sx={{ ml: 0, mr: 3 }}>
  130. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  131. <Button
  132. variant="contained"
  133. type="submit"
  134. color="success"
  135. >
  136. <FormattedMessage id="create" />
  137. </Button>
  138. </ThemeProvider>
  139. </Grid>
  140. </> :
  141. <>
  142. <Grid item sx={{ ml: 0, mr: 3 }}>
  143. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  144. <Button
  145. variant="contained"
  146. color="cancel"
  147. onClick={loadDataFun}
  148. >
  149. <FormattedMessage id="resetAndBack" />
  150. </Button>
  151. </ThemeProvider>
  152. </Grid>
  153. <Grid item sx={{ ml: 3, mr: 3 }}>
  154. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  155. <Button
  156. variant="contained"
  157. type="submit"
  158. color="success"
  159. >
  160. <FormattedMessage id="save" />
  161. </Button>
  162. </ThemeProvider>
  163. </Grid>
  164. </>
  165. }
  166. </>
  167. :
  168. <>
  169. <Grid item sx={{ ml: 0, mr: 3 }}>
  170. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  171. <Button
  172. variant="contained"
  173. onClick={onEditClick}
  174. color="success"
  175. >
  176. < FormattedMessage id="edit" />
  177. </Button>
  178. </ThemeProvider>
  179. </Grid>
  180. </>
  181. }
  182. </Grid>
  183. </Grid>
  184. {/*top*/}
  185. {!onReady ?
  186. <LoadingComponent />
  187. :
  188. <Grid container spacing={1}>
  189. {/* <Grid item xs={12}>
  190. <Typography variant="h4" sx={{ mb: 2, mr: 3, borderBottom: "1px solid black" }}>
  191. <FormattedMessage id="organizationDetails" />
  192. </Typography>
  193. </Grid> */}
  194. <Grid item xs={12}>
  195. <FormHelperText error id="helper-text-address1-signup">
  196. <Typography variant="errorMessage1">
  197. {errorMsg}
  198. </Typography>
  199. </FormHelperText>
  200. </Grid>
  201. <Grid item xs={12} lg={4} >
  202. {FieldUtils.getTextField({
  203. label: intl.formatMessage({ id: 'brNo' }) + ":",
  204. valueName: "brNo",
  205. disabled: true,
  206. form: formik
  207. })}
  208. </Grid>
  209. <Grid item xs={12} lg={4} >
  210. {FieldUtils.getTextField({
  211. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'expiryDate' }) + ":"),
  212. valueName: "brExpiryDate",
  213. disabled: true,
  214. form: formik
  215. })}
  216. </Grid>
  217. <Grid item xs={12} lg={4} ></Grid>
  218. <Grid item xs={12} lg={4} >
  219. {FieldUtils.getTextField({
  220. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'nameEng' }) + ":"),
  221. valueName: "enCompanyName",
  222. disabled: true,
  223. form: formik
  224. })}
  225. </Grid>
  226. <Grid item xs={12} lg={4} >
  227. {FieldUtils.getTextField({
  228. label: intl.formatMessage({ id: 'nameChi' }) + ":",
  229. valueName: "chCompanyName",
  230. disabled: true,
  231. form: formik
  232. })}
  233. </Grid>
  234. <Grid item xs={12} lg={4} >
  235. </Grid>
  236. <Grid item xs={12} lg={4} >
  237. {FieldUtils.getTextField({
  238. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'contactPerson' }) + ":"),
  239. valueName: "contactPerson",
  240. disabled: (!editMode && !createMode),
  241. form: formik
  242. })}
  243. </Grid>
  244. <Grid item xs={12} lg={4} >
  245. {FieldUtils.getPhoneField({
  246. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'userContactNumber' }) + ":"),
  247. valueName: {
  248. code: "tel_countryCode",
  249. num: "phoneNumber"
  250. },
  251. disabled: (!editMode && !createMode),
  252. form: formik
  253. })}
  254. </Grid>
  255. <Grid item xs={12} lg={4} >
  256. {FieldUtils.getPhoneField({
  257. label: intl.formatMessage({ id: 'contactFaxNumber' }) + ":",
  258. valueName: {
  259. code: "fax_countryCode",
  260. num: "faxNumber"
  261. },
  262. disabled: (!editMode && !createMode),
  263. form: formik
  264. })}
  265. </Grid>
  266. <Grid item xs={12} lg={12} >
  267. {FieldUtils.getAddressField({
  268. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'formAddress' }) + ":"),
  269. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  270. disabled: (!editMode && !createMode),
  271. form: formik
  272. })}
  273. </Grid>
  274. <Grid item xs={12} lg={12} >
  275. {FieldUtils.getProfileComboField({
  276. // label: FieldUtils.notNullFieldLabel(""),
  277. label: "",
  278. valueName: "district",
  279. disabled: (!editMode && !createMode),
  280. dataList: ComboData.district,
  281. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  282. form: formik
  283. })}
  284. </Grid>
  285. <Grid item xs={12} lg={12} >
  286. {FieldUtils.getProfileComboField({
  287. // label: FieldUtils.notNullFieldLabel(""),
  288. label: "",
  289. valueName: "country",
  290. disabled: true,
  291. dataList: ComboData.country,
  292. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  293. form: formik
  294. })}
  295. </Grid>
  296. <Grid item lg={12} ></Grid>
  297. </Grid>
  298. }
  299. </form>
  300. <div>
  301. <Dialog
  302. open={creditorConfirmPopUp}
  303. onClose={() => setCreditorConfirmPopUp(false)}
  304. PaperProps={{
  305. sx: {
  306. minWidth: '40vw',
  307. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  308. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  309. }
  310. }}
  311. >
  312. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  313. <DialogContent style={{ display: 'flex', }}>
  314. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Credit Client?</Typography>
  315. </DialogContent>
  316. <DialogActions>
  317. <Button onClick={() => setCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  318. <Button onClick={() => markAsCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  319. </DialogActions>
  320. </Dialog>
  321. </div>
  322. <div>
  323. <Dialog
  324. open={nonCreditorConfirmPopUp}
  325. onClose={() => setNonCreditorConfirmPopUp(false)}
  326. PaperProps={{
  327. sx: {
  328. minWidth: '40vw',
  329. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  330. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  331. }
  332. }}
  333. >
  334. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  335. <DialogContent style={{ display: 'flex', }}>
  336. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Non-Credit Client?</Typography>
  337. </DialogContent>
  338. <DialogActions>
  339. <Button onClick={() => setNonCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  340. <Button onClick={() => markAsNonCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  341. </DialogActions>
  342. </Dialog>
  343. </div>
  344. </MainCard>
  345. );
  346. };
  347. export default OrganizationPubCard;