// material-ui import { Grid, Button, Typography, FormHelperText, Stack, IconButton } from '@mui/material'; import MainCard from "components/MainCard"; import { useEffect, useState, lazy } from "react"; import * as yup from 'yup'; import { useFormik } from 'formik'; import * as FieldUtils from "utils/FieldUtils"; import * as HttpUtils from 'utils/HttpUtils'; import * as UrlUtils from "utils/ApiPathConst"; import * as ComboData from "utils/ComboData"; const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent'))); import Loadable from 'components/Loadable'; import { notifyActiveSuccess, notifyLockSuccess, notifySaveSuccess, notifyVerifySuccess } from 'utils/CommonFunction'; import { useIntl } from "react-intl"; import { PNSPS_BUTTON_THEME } from "themes/buttonConst"; import { ThemeProvider } from "@emotion/react"; import { EyeInvisibleOutlined, EyeOutlined } from '@ant-design/icons'; import { isGrantedAny } from "auth/utils"; // ==============================|| DASHBOARD - DEFAULT ||============================== // const UserInformationCard_Individual = ({ formData, loadDataFun }) => { const intl = useIntl(); const [currentUserData, setCurrentUserData] = useState(formData); const [editMode, setEditMode] = useState(false); const [locked, setLocked] = useState(false); const [onReady, setOnReady] = useState(false); const [errorMsg, setErrorMsg] = useState(""); const [showId, setshowId] = useState(false); const [selectedIdDocInputType, setSelectedIdDocInputType] = useState(""); const handleClickShowId = () => { setshowId(!showId); }; const handleMouseDownId = (event) => { event.preventDefault(); }; useEffect(() => { //if state data are ready and assign to different field // console.log(currentApplicationDetailData) if (Object.keys(currentUserData).length > 0) { setSelectedIdDocInputType(currentUserData.idDocType) setOnReady(true); } }, [currentUserData]); function getMaxErrStr(num, fieldname) { return intl.formatMessage({ id: 'noMoreThenNWords' }, { num: num, fieldname: fieldname ? intl.formatMessage({ id: fieldname }) + ": " : "" }); } function getRequiredErrStr(fieldname) { return displayErrorMsg(intl.formatMessage({ id: 'require' }, { fieldname: fieldname ? intl.formatMessage({ id: fieldname }) : "" })); } function displayErrorMsg(errorMsg) { return {errorMsg} } const formik = useFormik({ enableReinitialize: true, initialValues: currentUserData, validationSchema: yup.object().shape({ enName: yup.string().max(40, getMaxErrStr(40)).when('chName', { is: (chName) => chName?false:true, then: yup.string().required(intl.formatMessage({ id: 'userRequireEnglishName' })) }).nullable(), chName: yup.string().max(6, getMaxErrStr(6)).nullable(), addressLine1: yup.string().max(40, getMaxErrStr(40)).required(intl.formatMessage({ id: 'validateAddressLine1' })), addressLine2: yup.string().max(40, getMaxErrStr(40)).nullable(), addressLine3: yup.string().max(40, getMaxErrStr(40)).nullable(), emailAddress: yup.string().email(intl.formatMessage({ id: 'validEmailFormat' })).max(255).required(intl.formatMessage({ id: 'requireEmail' })), idDocType: yup.string().max(255, getMaxErrStr(255)).required(intl.formatMessage({ id: 'requireIdDocType' })), identification: yup.string().required(getRequiredErrStr('number')) .matches(/^[aA-zZ0-9\s]+$/, { message: displayErrorMsg(`${selectedIdDocInputType}${intl.formatMessage({ id: 'noSpecialCharacter' })}`) }) .matches(/^\S*$/, { message: displayErrorMsg(`${selectedIdDocInputType}${intl.formatMessage({ id: 'noSpace' })}`) }) .test('checkIDCardFormat', displayErrorMsg(`${intl.formatMessage({ id: 'requiredValid' })}${selectedIdDocInputType}${intl.formatMessage({ id: 'number' })}`), function (value) { var pattern_HKIDv1 = /^[A-Z]{1}[0-9]{6}$/; var pattern_HKIDv2 = /^[A-Z]{2}[0-9]{6}$/; var pattern_passport = /^[A-Z]{1}[0-9]{8}$/; var pattern_CHID = /^[0-9]{6}(20|19)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9X]{1}/; var pattern_otherCert = /^[A-Z]{1}[0-9]{5,}/; if (value !== undefined) { switch (selectedIdDocInputType) { case "HKID": if (value.match(pattern_HKIDv1)) { return true } else if (value.match(pattern_HKIDv2)) { return true } else { return false } case "passport": if (value.match(pattern_passport)) { return true } else { return false } case "CNID": if (value.match(pattern_CHID)) { const subStr_year = value.substring(6, 10) const subStr_month = value.substring(10, 12) const subStr_date = value.substring(12, 14) const today = new Date() const inputDate = new Date(`${subStr_year}-${subStr_month}-${subStr_date}`) if (inputDate > today || inputDate === "Invalid Date" || inputDate.getFullYear().toString() !== subStr_year || (inputDate.getMonth() + 1).toString().padStart(2, "0") !== subStr_month || inputDate.getDate().toString().padStart(2, "0") !== subStr_date) { return false } else { return true } } else { return false } case "otherCert": if (value.match(pattern_otherCert)) { return true } else { return false } default: break; } } }), checkDigit: yup.string().max(1, getMaxErrStr(1)).nullable() .matches(/^[A-Z0-9\s]+$/, { message: displayErrorMsg(`${selectedIdDocInputType}${intl.formatMessage({ id: 'noSpecialCharacter' })}`) }) .test('checkIDCardFormat', displayErrorMsg(`${intl.formatMessage({ id: 'requiredNumberInQuote' })}`), function (value) { console.log(selectedIdDocInputType) if (value != undefined || value != null) { switch (selectedIdDocInputType) { case "HKID": if (value.length == 1) { return true } else { return false } case "passport": return true case "CNID": return true case "otherCert": return true default: break; } } else { if (selectedIdDocInputType != "HKID"){ return true } else { return false } } }), tel_countryCode: yup.string().min(3, intl.formatMessage({ id: 'require3Number' })).required(intl.formatMessage({ id: 'requireDialingCode' })), fax_countryCode: yup.string().min(3, intl.formatMessage({ id: 'require3Number' })), phoneNumber: yup.string().min(8, intl.formatMessage({ id: 'require8Number' })).required(intl.formatMessage({ id: 'requireContactNumber' })), faxNumber: yup.string().min(8, intl.formatMessage({ id: 'require8Number' })).nullable(), }), onSubmit: values => { if (values.country == null) { setErrorMsg(intl.formatMessage({ id: 'pleaseFillInCountry' })) } else { if (values.country.type == "hongKong" && values.district == null) { setErrorMsg(intl.formatMessage({ id: 'pleaseFillInDistrict' })) } else { HttpUtils.post({ url: UrlUtils.POST_IND_USER + "/" + formData.id, params: { prefix: values.prefix, enName: values.enName, chName: values.chName, idDocType: values.idDocType, mobileNumber: { countryCode: values.tel_countryCode, phoneNumber: values.phoneNumber }, identification: values.identification, checkDigit: values.checkDigit, faxNo: { countryCode: values.fax_countryCode, faxNumber: values.faxNumber }, emailAddress: values.emailAddress, address: { country: values.country.type, district: values.district?.type, addressLine1: values.addressLine1, addressLine2: values.addressLine2, addressLine3: values.addressLine3, }, preferLocale: values.preferLocale.type }, onSuccess: function () { notifySaveSuccess(); loadDataFun(); } }); } } } }); useEffect(() => { if (Object.keys(formData).length > 0) { setCurrentUserData(formData); } }, [formData]); useEffect(() => { setLocked(currentUserData.locked); }, [currentUserData]); const onEditClick = () => { setEditMode(true); }; const onVerifiedClick = () => { HttpUtils.get({ url: UrlUtils.GET_IND_USER_VERIFY + "/" + formData.id, onSuccess: function () { notifyVerifySuccess() loadDataFun(); } }); }; const doLock = () => { HttpUtils.get({ url: UrlUtils.GET_USER_LOCK + "/" + formData.id, onSuccess: function () { notifyLockSuccess() loadDataFun(); } }); }; const doUnlock = () => { HttpUtils.get({ url: UrlUtils.GET_USER_UNLOCK + "/" + formData.id, onSuccess: function () { notifyActiveSuccess() loadDataFun(); } }); }; return ( {!onReady ? :
{/*top button*/} { isGrantedAny("MAINTAIN_USER") ? {editMode ? <> : <> } : <> } {/*end top button*/} Individual User Details {errorMsg} {FieldUtils.getTextField({ label: "Username:", valueName: "username", disabled: true, form: formik })} {FieldUtils.getTextField({ label: "English Name:", valueName: "enName", disabled: (!editMode), form: formik })} {FieldUtils.getTextField({ label: "Created Date:", valueName: "createDate", disabled: true, form: formik })} {FieldUtils.getTextField({ label: "Prefix:", valueName: "prefix", disabled: (!editMode), form: formik })} {FieldUtils.getTextField({ label: "Chinese Name:", valueName: "chName", disabled: (!editMode), form: formik })} {FieldUtils.getTextField({ label: "Last Updated:", valueName: "modifieDate", disabled: true, form: formik })} {FieldUtils.getComboField({ label: "ID Type:", valueName: "idDocType", disabled: (!editMode), dataList: ComboData.idDocType, filterOptions: (options) => options, getOptionLabel: (item) => item ? typeof item === 'string' ? item : (item["type"] ? item["type"] + "-" + item["label"] : "") : "", onInputChange: (event, newValue, setInputValue) => { if (newValue == null) { setInputValue(""); } let _val = newValue.split("-"); if (_val[0]) { setInputValue(_val[0]); } }, onChange: (event, newValue) => { if (newValue == null) { formik.setFieldValue("idDocType", ""); return; } formik.setFieldValue("idDocType", newValue.type); setSelectedIdDocInputType(newValue.type); if (newValue.type !== "HKID") { formik.setFieldValue("checkDigit", "") } }, form: formik })} {FieldUtils.getPhoneField({ label: "Contact Tel:", valueName: { code: "tel_countryCode", num: "phoneNumber" }, disabled: (!editMode), form: formik })} Verified: { !isGrantedAny("MAINTAIN_USER") || currentUserData.verifiedBy || editMode ? {FieldUtils.initField({ valueName: "verifiedStatus", disabled: true, form: formik, })} : <> {FieldUtils.initField({ valueName: "verifiedStatus", disabled: true, form: formik, })} } ID No.: {formik.values.idDocType === "HKID" ? editMode ? <> {FieldUtils.initField({ valueName: "identification", disabled: (!editMode), form: formik, placeholder: intl.formatMessage({ id: 'idDocNumber' }), inputProps: { maxLength: 7, onKeyDown: (e) => { if (e.key === 'Enter') { e.preventDefault(); } }, } })} {FieldUtils.initField({ valueName: "checkDigit", disabled: (!editMode), form: formik, })} : {formik.values.identification?.slice(0, 4)} {showId ? formik.values.identification?.slice(4) : "****"}{showId ? formik.values.checkDigit?'(' +formik.values.checkDigit+ ')': "()" : ""} {showId ? : } : editMode ? <> {FieldUtils.initField({ valueName: "identification", disabled: (!editMode), form: formik })} : {formik.values.identification?.slice(0, 4)} {showId ? formik.values.identification?.slice(4) : "****"} {showId ? : } } {FieldUtils.getPhoneField({ label: "Fax No.:", valueName: { code: "fax_countryCode", num: "faxNumber" }, disabled: (!editMode), form: formik })} {FieldUtils.getTextField({ label: "Last Login:", valueName: "lastLoginDate", disabled: true, form: formik })} {FieldUtils.getComboField({ label: "Country:", valueName: "country", getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "", dataList: ComboData.country, disabled: (!editMode), form: formik })} {FieldUtils.getTextField({ label: "Email:", valueName: "emailAddress", disabled: (!editMode), form: formik })} Status: { !isGrantedAny("MAINTAIN_USER") || editMode ? {FieldUtils.initField({ valueName: "status", disabled: true, form: formik, })} : <> {FieldUtils.initField({ valueName: "status", disabled: true, form: formik, })} { locked ? : } } {FieldUtils.getComboField({ label: "District:", valueName: "district", dataList: ComboData.district, getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "", disabled: (!editMode), form: formik })} {FieldUtils.getComboField({ label: intl.formatMessage({ id: 'language' }) + ":", valueName: "preferLocale", dataList: ComboData.Locale, getOptionLabel: (option) => option.label ? option.label : "", disabled: (!editMode), form: formik })} {FieldUtils.getAddressField({ label: "Address:", valueName: ["addressLine1", "addressLine2", "addressLine3"], disabled: (!editMode), form: formik })}
}
); }; export default UserInformationCard_Individual;