25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

270 satır
11 KiB

  1. // material-ui
  2. import {
  3. Grid, Button, Typography,
  4. Dialog, DialogTitle, DialogContent, DialogActions,
  5. } from '@mui/material';
  6. import MainCard from "../../components/MainCard";
  7. import * as React from "react";
  8. import * as yup from 'yup';
  9. import { useEffect, useState } from "react";
  10. import * as HttpUtils from '../../utils/HttpUtils';
  11. import * as UrlUtils from "../../utils/ApiPathConst";
  12. import * as FieldUtils from "../../utils/FieldUtils";
  13. import * as ComboData from "../../utils/ComboData";
  14. import { useNavigate } from "react-router-dom";
  15. import { useFormik } from 'formik';
  16. const LoadingComponent = Loadable(lazy(() => import('../extra-pages/LoadingComponent')));
  17. import Loadable from 'components/Loadable';
  18. import { lazy } from 'react';
  19. import { notifyCreateSuccess } from 'utils/CommonFunction';
  20. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  21. const OrganizationCard_loadFromUser = ({ userData, userId }) => {
  22. const [currentUserData, setCurrentUserData] = useState(userData);
  23. const navigate = useNavigate();
  24. const [onReady, setOnReady] = useState(false);
  25. const [isFailPopUp, setIsFailPopUp] = useState(false);
  26. const [failText, setFailText] = useState("");
  27. useEffect(() => {
  28. //if state data are ready and assign to different field
  29. // console.log(currentApplicationDetailData)
  30. if (Object.keys(currentUserData).length > 0) {
  31. setOnReady(true);
  32. }
  33. }, [currentUserData]);
  34. function displayErrorMsg(errorMsg) {
  35. return <Typography variant="errorMessage1">{errorMsg}</Typography>
  36. }
  37. const formik = useFormik({
  38. enableReinitialize: true,
  39. initialValues: currentUserData,
  40. validationSchema: yup.object().shape({
  41. enCompanyName: yup.string().max(255, displayErrorMsg("請輸入英文名稱")).required(displayErrorMsg('請輸入英文名稱')),
  42. chCompanyName: yup.string().max(255, displayErrorMsg('請輸入中文名稱')).nullable(),
  43. addressLine1: yup.string().max(255).required(displayErrorMsg('請輸入第一行地址')),
  44. addressLine2: yup.string().max(255).nullable(),
  45. addressLine3: yup.string().max(255).nullable(),
  46. fax_countryCode: yup.string().min(3, displayErrorMsg("請輸入國際區號")).nullable(),
  47. tel_countryCode: yup.string().min(3, displayErrorMsg("請輸入國際區號")),
  48. phoneNumber: yup.string().min(8, displayErrorMsg('請輸入有效聯絡電話')).required(displayErrorMsg('請輸入聯絡電話')),
  49. faxNumber: yup.string().min(8).nullable(),
  50. brExpiryDate: yup.string().min(8).required(displayErrorMsg('請輸入商業登記證有效日期')),
  51. brNo: yup.string().required(displayErrorMsg('請輸入商業登記證號碼')).test('checkBrNoFormat', displayErrorMsg(displayErrorMsg(`請輸入有效商業登記證號碼 (e.g. 12341234-123-12-12-1)`)), function (value) {
  52. var brNo_pattern = /[0-9]{8}-[0-9]{3}-(0[1-9]|1[012])-[0-9]{2}-[0-9A-Z]{1}/
  53. if (value !== undefined) {
  54. if (value.match(brNo_pattern)) {
  55. return true
  56. } else {
  57. return false
  58. }
  59. }
  60. }),
  61. }),
  62. onSubmit: values => {
  63. HttpUtils.post({
  64. url: UrlUtils.POST_ORG_SAVE_PATH,
  65. params: {
  66. id: null,
  67. primaryUserId: userId,
  68. enCompanyName: values.enCompanyName,
  69. chCompanyName: values.chCompanyName,
  70. brNo: values.brNo,
  71. brExpiryDate: values.brExpiryDate,
  72. enCompanyNameTemp: values.enCompanyNameTemp,
  73. chCompanyNameTemp: values.chCompanyNameTemp,
  74. brExpiryDateTemp: values.brExpiryDateTemp,
  75. contactPerson: values.contactPerson,
  76. contactTel: {
  77. countryCode: values.tel_countryCode,
  78. phoneNumber: values.phoneNumber
  79. },
  80. faxNo: {
  81. countryCode: values.fax_countryCode,
  82. faxNumber: values.faxNumber
  83. },
  84. addressTemp: {
  85. country: values.country,
  86. district: values.district,
  87. addressLine1: values.addressLine1,
  88. addressLine2: values.addressLine2,
  89. addressLine3: values.addressLine3,
  90. }
  91. },
  92. onSuccess: function (responseData) {
  93. if(responseData.msg){
  94. setFailText(responseData.msg);
  95. setIsFailPopUp(true);
  96. return;
  97. }
  98. navigate('/org/' + responseData.id);
  99. notifyCreateSuccess()
  100. }
  101. });
  102. }
  103. });
  104. useEffect(() => {
  105. if (Object.keys(userData).length > 0) {
  106. setCurrentUserData(userData);
  107. }
  108. }, [userData]);
  109. return (
  110. <MainCard elevation={0}
  111. border={false}
  112. content={false}
  113. >
  114. {/* <div style={{ border: "solid 1px;", color: "red" }}>
  115. TODO: Error Summary
  116. {Object.values(formik.errors).map(error => (
  117. <div>{error}</div>
  118. ))}
  119. </div> */}
  120. <div style={{ padding: 24 }}>
  121. <form onSubmit={formik.handleSubmit}>
  122. {!onReady?
  123. <LoadingComponent />
  124. :
  125. <Grid container spacing={1}>
  126. {/*top*/}
  127. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  128. <Grid item sx={{ ml: 3, mr: 3 }}>
  129. <Button
  130. size="large"
  131. variant="contained"
  132. type="submit"
  133. sx={{
  134. textTransform: 'capitalize',
  135. alignItems: 'end'
  136. }}
  137. >
  138. Create
  139. </Button>
  140. </Grid>
  141. </Grid>
  142. {/*top*/}
  143. <Grid item lg={4}>
  144. {FieldUtils.getTextField({
  145. label: "BR No.:",
  146. valueName: "brNo",
  147. form: formik
  148. })}
  149. </Grid>
  150. <Grid item lg={8}></Grid>
  151. <Grid item lg={4}>
  152. {FieldUtils.getTextField({
  153. label: FieldUtils.notNullFieldLabel("Name (Eng):"),
  154. valueName: "enCompanyName",
  155. form: formik
  156. })}
  157. </Grid>
  158. <Grid item lg={4}>
  159. {FieldUtils.getTextField({
  160. label: "Name (Ch):",
  161. valueName: "chCompanyName",
  162. form: formik
  163. })}
  164. </Grid>
  165. <Grid item lg={4}>
  166. {FieldUtils.getDateField({
  167. label: FieldUtils.notNullFieldLabel("Expiry Date:"),
  168. valueName: "brExpiryDate",
  169. form: formik
  170. })}
  171. </Grid>
  172. <Grid item lg={4}>
  173. {FieldUtils.getTextField({
  174. label: FieldUtils.notNullFieldLabel("Contact Person:"),
  175. valueName: "contactPerson",
  176. form: formik
  177. })}
  178. </Grid>
  179. <Grid item lg={4}>
  180. {FieldUtils.getPhoneField({
  181. label: FieldUtils.notNullFieldLabel("Contact Tel:"),
  182. valueName: {
  183. code: "tel_countryCode",
  184. num: "phoneNumber"
  185. },
  186. form: formik
  187. })}
  188. </Grid>
  189. <Grid item lg={4}>
  190. {FieldUtils.getPhoneField({
  191. label: "Fax No:",
  192. valueName: {
  193. code: "fax_countryCode",
  194. num: "faxNumber"
  195. },
  196. form: formik
  197. })}
  198. </Grid>
  199. <Grid item lg={4}>
  200. {FieldUtils.getComboField({
  201. label: FieldUtils.notNullFieldLabel("Country:"),
  202. valueName: "country",
  203. dataList: ComboData.country,
  204. form: formik
  205. })}
  206. </Grid>
  207. <Grid item lg={4}>
  208. {FieldUtils.getComboField({
  209. label: FieldUtils.notNullFieldLabel("District:"),
  210. valueName: "district",
  211. dataList: ComboData.district,
  212. form: formik
  213. })}
  214. </Grid>
  215. <Grid item lg={4}>
  216. {FieldUtils.getAddressField({
  217. label: FieldUtils.notNullFieldLabel("Address:"),
  218. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  219. form: formik
  220. })}
  221. </Grid>
  222. </Grid>
  223. }
  224. </form>
  225. </div>
  226. <div>
  227. <Dialog open={isFailPopUp} onClose={() => setIsFailPopUp(false)} >
  228. <DialogTitle><Typography variant="h3">Action Fail</Typography></DialogTitle>
  229. <DialogContent style={{ display: 'flex', }}>
  230. <Typography variant="h4" style={{ padding: '16px' }}>{failText}</Typography>
  231. </DialogContent>
  232. <DialogActions>
  233. <Button onClick={() => setIsFailPopUp(false)}><Typography variant="h5">OK</Typography></Button>
  234. </DialogActions>
  235. </Dialog>
  236. </div>
  237. </MainCard>
  238. );
  239. };
  240. export default OrganizationCard_loadFromUser;