選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

OrganizationCard.js 19 KiB

1年前
2年前
1年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
1年前
2年前
1年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
1年前
2年前
2年前
2年前
2年前
1年前
2年前
2年前
2年前
2年前
1年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
1年前
1年前
1年前
2年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // material-ui
  2. import {
  3. Grid, Button, Checkbox, FormControlLabel, Typography,
  4. Dialog, DialogTitle, DialogContent, DialogActions,
  5. } from '@mui/material';
  6. // import { FormControlLabel } from '@material-ui/core';
  7. import MainCard from "../../../components/MainCard";
  8. import * as React from "react";
  9. import { useFormik } from 'formik';
  10. import * as yup from 'yup';
  11. import { useEffect, useState } from "react";
  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. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  17. import Loadable from 'components/Loadable';
  18. import { lazy } from 'react';
  19. import { notifySaveSuccess } from 'utils/CommonFunction';
  20. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  21. const OrganizationCard = ({ userData, loadDataFun, id, setEditModeFun }) => {
  22. const [creditorConfirmPopUp, setCreditorConfirmPopUp] = React.useState(false);
  23. const [nonCreditorConfirmPopUp, setNonCreditorConfirmPopUp] = React.useState(false);
  24. const [currentUserData, setCurrentUserData] = useState({});
  25. const [editMode, setEditMode] = useState(false);
  26. const [createMode, setCreateMode] = useState(false);
  27. const [onReady, setOnReady] = useState(false);
  28. 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. enCompanyName: yup.string().max(255).required(displayErrorMsg('請輸入英文名稱')),
  43. chCompanyName: yup.string().max(255, displayErrorMsg('請輸入中文名稱')).nullable(),
  44. addressLine1: yup.string().max(255).required(displayErrorMsg('請輸入第一行地址')),
  45. addressLine2: yup.string().max(255, displayErrorMsg("length must <= 255")),
  46. addressLine3: yup.string().max(255, displayErrorMsg("length must <= 255")),
  47. fax_countryCode: yup.string().min(3, displayErrorMsg('請輸入國際區號')).nullable(),
  48. tel_countryCode: yup.string().min(3, displayErrorMsg('請輸入國際區號')),
  49. phoneNumber: yup.string().min(8, displayErrorMsg('請輸入有效聯絡電話')).required(displayErrorMsg('請輸入聯絡電話')),
  50. faxNumber: yup.string().min(8, displayErrorMsg('請輸入8位數字')).nullable(),
  51. brExpiryDate: yup.string().min(8).required(displayErrorMsg('請輸入商業登記證有效日期')),
  52. brNo: yup.string().required(displayErrorMsg('請輸入商業登記證號碼')).test('checkBrNoFormat', displayErrorMsg(displayErrorMsg(`請輸入有效商業登記證號碼`)), function (value) {
  53. var brNo_pattern = /[0-9]{8}/
  54. if (value !== undefined) {
  55. if (value.match(brNo_pattern)) {
  56. return true
  57. } else {
  58. return false
  59. }
  60. }
  61. }),
  62. }),
  63. onSubmit: vaule => {
  64. console.log(vaule)
  65. HttpUtils.post({
  66. url: UrlUtils.POST_ORG_SAVE_PATH,
  67. params: {
  68. id: id > 0 ? id : null,
  69. enCompanyName: vaule.enCompanyName,
  70. chCompanyName: vaule.chCompanyName,
  71. brNo: vaule.brNo,
  72. brExpiryDate: vaule.brExpiryDate,
  73. enCompanyNameTemp: vaule.enCompanyNameTemp,
  74. chCompanyNameTemp: vaule.chCompanyNameTemp,
  75. brExpiryDateTemp: vaule.brExpiryDateTemp,
  76. contactPerson: vaule.contactPerson,
  77. contactTel: {
  78. countryCode: vaule.tel_countryCode,
  79. phoneNumber: vaule.phoneNumber
  80. },
  81. faxNo: {
  82. countryCode: vaule.fax_countryCode,
  83. faxNumber: vaule.faxNumber
  84. },
  85. addressTemp: {
  86. country: vaule.country,
  87. district: vaule.district,
  88. addressLine1: vaule.addressLine1,
  89. addressLine2: vaule.addressLine2,
  90. addressLine3: vaule.addressLine3,
  91. },
  92. //creditor: vaule.creditor,
  93. },
  94. onSuccess: function () {
  95. notifySaveSuccess()
  96. loadDataFun();
  97. setEditMode(false);
  98. }
  99. });
  100. }
  101. });
  102. useEffect(()=>{
  103. setEditModeFun(editMode);
  104. },[editMode]);
  105. useEffect(() => {
  106. if (Object.keys(userData).length > 0) {
  107. setCreateMode(id <= 0);
  108. setEditMode(id <= 0);
  109. setCurrentUserData(userData);
  110. }
  111. }, [userData]);
  112. // useEffect(() => {
  113. // if (Object.keys(userData).length > 0) {
  114. // if(userData.creditor === undefined||userData.creditor === null){
  115. // userData.creditor = false
  116. // }
  117. // setCurrentUserData(userData);
  118. // }
  119. // }, []);
  120. const onEditClick = () => {
  121. setEditMode(true);
  122. };
  123. const markAsCreditor = () => {
  124. setCreditorConfirmPopUp(false);
  125. HttpUtils.get({
  126. url: UrlUtils.GET_ORG_MARK_AS_CREDITOR + "/" + id,
  127. onSuccess: () => {
  128. loadDataFun();
  129. }
  130. });
  131. }
  132. const markAsNonCreditor = () => {
  133. setNonCreditorConfirmPopUp(false);
  134. HttpUtils.get({
  135. url: UrlUtils.GET_ORG_MARK_AS_NON_CREDITOR + "/" + id,
  136. onSuccess: () => {
  137. loadDataFun();
  138. }
  139. });
  140. }
  141. return (
  142. <MainCard elevation={0}
  143. border={false}
  144. content={false}
  145. >
  146. <form onSubmit={formik.handleSubmit} style={{ padding: 24 }}>
  147. {/*top*/}
  148. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  149. <Grid container maxWidth justifyContent="flex-start">
  150. {editMode ?
  151. <>
  152. {createMode ?
  153. <>
  154. <Grid item sx={{ ml: 0, mr: 3 }}>
  155. <Button
  156. size="large"
  157. variant="contained"
  158. type="submit"
  159. sx={{
  160. textTransform: 'capitalize',
  161. alignItems: 'end'
  162. }}
  163. >
  164. <Typography variant="h5">Create</Typography>
  165. </Button>
  166. </Grid>
  167. </> :
  168. <>
  169. <Grid item sx={{ ml: 0, mr: 3 }}>
  170. <Button
  171. size="large"
  172. variant="contained"
  173. onClick={loadDataFun}
  174. sx={{
  175. textTransform: 'capitalize',
  176. alignItems: 'end'
  177. }}
  178. >
  179. <Typography variant="h5">Reset & Back</Typography>
  180. </Button>
  181. </Grid>
  182. <Grid item sx={{ ml: 3, mr: 3 }}>
  183. <Button
  184. size="large"
  185. variant="contained"
  186. type="submit"
  187. color="success"
  188. sx={{
  189. textTransform: 'capitalize',
  190. alignItems: 'end'
  191. }}
  192. >
  193. <Typography variant="h5">Save</Typography>
  194. </Button>
  195. </Grid>
  196. </>
  197. }
  198. </>
  199. :
  200. <>
  201. <Grid item sx={{ ml: 0, mr: 3 }}>
  202. <Button
  203. size="large"
  204. variant="contained"
  205. sx={{
  206. textTransform: 'capitalize',
  207. alignItems: 'end'
  208. }}
  209. onClick={onEditClick}
  210. >
  211. <Typography variant="h5">Edit</Typography>
  212. </Button>
  213. </Grid>
  214. <Grid item sx={{ ml: 3, mr: 3 }}>
  215. <Button
  216. size="large"
  217. variant="contained"
  218. color="orange"
  219. onClick={()=>setCreditorConfirmPopUp(true)}
  220. sx={{
  221. textTransform: 'capitalize',
  222. alignItems: 'end'
  223. }}
  224. >
  225. <Typography variant="h5">Mark as Creditor</Typography>
  226. </Button>
  227. </Grid>
  228. <Grid item sx={{ ml: 3, mr: 3 }}>
  229. <Button
  230. size="large"
  231. variant="contained"
  232. color="error"
  233. onClick={()=>setNonCreditorConfirmPopUp(true)}
  234. sx={{
  235. textTransform: 'capitalize',
  236. alignItems: 'end'
  237. }}
  238. >
  239. <Typography variant="h5">Mark as Non-Creditor</Typography>
  240. </Button>
  241. </Grid>
  242. </>
  243. }
  244. </Grid>
  245. </Grid>
  246. {/*top*/}
  247. {!onReady ?
  248. <LoadingComponent />
  249. :
  250. <Grid container spacing={1}>
  251. <Grid item xs={12}>
  252. <Typography variant="h4" sx={{ mb: 2, mr: 3, borderBottom: "1px solid black" }}>
  253. Organisation Details
  254. </Typography>
  255. </Grid>
  256. <Grid item lg={4} >
  257. {FieldUtils.getTextField({
  258. label: FieldUtils.notNullFieldLabel("BR No.:"),
  259. valueName: "brNo",
  260. disabled: (!editMode && !createMode),
  261. form: formik
  262. })}
  263. </Grid>
  264. <Grid item lg={4} >
  265. <FormControlLabel
  266. control={<Checkbox checked={formik.values.creditor} />}
  267. label="is Creditor"
  268. name="creditor"
  269. onChange={() => {
  270. formik.setFieldValue("creditor", !formik.values.creditor);
  271. }}
  272. disabled={true}
  273. //disabled={!editMode && !createMode}
  274. />
  275. </Grid>
  276. <Grid item lg={4} ></Grid>
  277. <Grid item lg={4} >
  278. {FieldUtils.getTextField({
  279. label: FieldUtils.notNullFieldLabel("Name (Eng):"),
  280. valueName: "enCompanyName",
  281. disabled: (!editMode && !createMode),
  282. form: formik
  283. })}
  284. </Grid>
  285. <Grid item lg={4} >
  286. {FieldUtils.getTextField({
  287. label: "Name (Ch):",
  288. valueName: "chCompanyName",
  289. disabled: (!editMode && !createMode),
  290. form: formik
  291. })}
  292. </Grid>
  293. <Grid item lg={4} >
  294. {FieldUtils.getDateField({
  295. label: FieldUtils.notNullFieldLabel("Expiry Date:"),
  296. valueName: "brExpiryDate",
  297. disabled: (!editMode && !createMode),
  298. form: formik
  299. })}
  300. </Grid>
  301. <Grid item lg={4} >
  302. {FieldUtils.getTextField({
  303. label: FieldUtils.notNullFieldLabel("Contact Person:"),
  304. valueName: "contactPerson",
  305. disabled: (!editMode && !createMode),
  306. form: formik
  307. })}
  308. </Grid>
  309. <Grid item lg={4} >
  310. {FieldUtils.getPhoneField({
  311. label: FieldUtils.notNullFieldLabel("Contact Tel:"),
  312. valueName: {
  313. code: "tel_countryCode",
  314. num: "phoneNumber"
  315. },
  316. disabled: (!editMode && !createMode),
  317. form: formik
  318. })}
  319. </Grid>
  320. <Grid item lg={4} >
  321. {FieldUtils.getPhoneField({
  322. label: "Fax No:",
  323. valueName: {
  324. code: "fax_countryCode",
  325. num: "faxNumber"
  326. },
  327. disabled: (!editMode && !createMode),
  328. form: formik
  329. })}
  330. </Grid>
  331. <Grid item lg={4} >
  332. {FieldUtils.getComboField({
  333. label: FieldUtils.notNullFieldLabel("Country:"),
  334. valueName: "country",
  335. disabled: (!editMode && !createMode),
  336. dataList: ComboData.country,
  337. form: formik
  338. })}
  339. </Grid>
  340. <Grid item lg={4} >
  341. {FieldUtils.getComboField({
  342. label: FieldUtils.notNullFieldLabel("District:"),
  343. valueName: "district",
  344. disabled: (!editMode && !createMode),
  345. dataList: ComboData.district,
  346. form: formik
  347. })}
  348. </Grid>
  349. <Grid item lg={4} >
  350. {FieldUtils.getAddressField({
  351. label: FieldUtils.notNullFieldLabel("Address:"),
  352. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  353. disabled: (!editMode && !createMode),
  354. form: formik
  355. })}
  356. </Grid>
  357. <Grid item lg={12} ></Grid>
  358. </Grid>
  359. }
  360. </form>
  361. <div>
  362. <Dialog open={creditorConfirmPopUp} onClose={() => setCreditorConfirmPopUp(false)} >
  363. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  364. <DialogContent style={{ display: 'flex', }}>
  365. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Creditor?</Typography>
  366. </DialogContent>
  367. <DialogActions>
  368. <Button onClick={() => setCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  369. <Button onClick={() => markAsCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  370. </DialogActions>
  371. </Dialog>
  372. </div>
  373. <div>
  374. <Dialog open={nonCreditorConfirmPopUp} onClose={() => setNonCreditorConfirmPopUp(false)} >
  375. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  376. <DialogContent style={{ display: 'flex', }}>
  377. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Non-Creditor?</Typography>
  378. </DialogContent>
  379. <DialogActions>
  380. <Button onClick={() => setNonCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  381. <Button onClick={() => markAsNonCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  382. </DialogActions>
  383. </Dialog>
  384. </div>
  385. </MainCard>
  386. );
  387. };
  388. export default OrganizationCard;