diff --git a/.env b/.env index bcbd53c..878b89b 100644 --- a/.env +++ b/.env @@ -91,4 +91,8 @@ REACT_APP_SHOW_UPLOAD_PROFILE_PICTURE=0 #GOOGLE RECAPTCHA SITEKEY REACT_APP_GOOGLE_RECAPTCHA_SITEKEY=6Ld_qKooAAAAADNL1TPzRLmcBA8vlpvx__39Rj39 -#6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI \ No newline at end of file +#6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI + +#FAMILY MEMBER MINIMUM AGE +REACT_APP_FAMILY_MINIMUM_AGE=4 +REACT_APP_FAMILY_MAXIMUM_AGE=18 \ No newline at end of file diff --git a/.env.development b/.env.development index 2816431..6795a1f 100644 --- a/.env.development +++ b/.env.development @@ -59,4 +59,8 @@ REACT_APP_SHOW_UPLOAD_PROFILE_PICTURE=0 #GOOGLE RECAPTCHA SITEKEY REACT_APP_GOOGLE_RECAPTCHA_SITEKEY=6Ld_qKooAAAAADNL1TPzRLmcBA8vlpvx__39Rj39 -#6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI \ No newline at end of file +#6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI + +#FAMILY MEMBER MINIMUM AGE +REACT_APP_FAMILY_MINIMUM_AGE=4 +REACT_APP_FAMILY_MAXIMUM_AGE=18 \ No newline at end of file diff --git a/.env.production b/.env.production index dbbeb1b..59c8eaf 100644 --- a/.env.production +++ b/.env.production @@ -66,3 +66,7 @@ REACT_APP_SHOW_UPLOAD_PROFILE_PICTURE=0 #GOOGLE RECAPTCHA SITEKEY REACT_APP_GOOGLE_RECAPTCHA_SITEKEY=6Ld_qKooAAAAADNL1TPzRLmcBA8vlpvx__39Rj39 #6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI + +#FAMILY MEMBER MINIMUM AGE +REACT_APP_FAMILY_MINIMUM_AGE=4 +REACT_APP_FAMILY_MAXIMUM_AGE=18 diff --git a/src/components/FamilyAcc/Tabs/FamilyProfile.jsx b/src/components/FamilyAcc/Tabs/FamilyProfile.jsx index db03abd..8284164 100644 --- a/src/components/FamilyAcc/Tabs/FamilyProfile.jsx +++ b/src/components/FamilyAcc/Tabs/FamilyProfile.jsx @@ -4,36 +4,66 @@ import LoadingSpinner from "../../Spinners/LoadingSpinner"; import usersService from "../../../services/UsersService"; import { useNavigate } from "react-router-dom"; +import { Formik, Form } from "formik"; +import * as Yup from "yup"; + + export default function FamilyProfile({familyData, className }) { + + const validationSchema = Yup.object().shape({ + firstname: Yup.string() + .required("Firstname is required"), + lastname: Yup.string() + // .min(3, "Minimum 3 characters") + // .max(25, "Maximum 25 characters") + .required("Lastname is required"), + year: Yup.number() + .required("Year is required") + .min(new Date().getFullYear()-process.env.REACT_APP_FAMILY_MAXIMUM_AGE, `Age must be within ${process.env.REACT_APP_FAMILY_MINIMUM_AGE} to ${process.env.REACT_APP_FAMILY_MAXIMUM_AGE} years`) + .max(new Date().getFullYear()-process.env.REACT_APP_FAMILY_MINIMUM_AGE, `Age must be within ${process.env.REACT_APP_FAMILY_MINIMUM_AGE} to ${process.env.REACT_APP_FAMILY_MAXIMUM_AGE} years`), + month: Yup.number() + .required("Month is required") + .min(1, "Month is required"), + }); + const navigate = useNavigate() const api = new usersService() let [requestStatus, setRequestStatus] = useState({loading:false, status:false, message: ''}) - let [updateDetails, setUpdateDetails] = useState({ + // let [updateDetails, setUpdateDetails] = useState({ + // family_uid: familyData.uid, + // firstname: familyData.firstname, + // lastname: familyData.lastname, + // year: familyData.year, + // month: familyData.month, + // action: 22020 + // }) + + // initial values for formik + let initialValues = { family_uid: familyData.uid, firstname: familyData.firstname, lastname: familyData.lastname, year: familyData.year, month: familyData.month, action: 22020 - }) + }; const handleChange = ({target:{name, value}})=>{ setUpdateDetails(prev => ({...prev, [name]:value})) } - const updateFamily = () => { + const updateFamily = (values, helpers) => { setRequestStatus({loading:true, status:false, message: ''}) - let {firstname, lastname, year, month} = updateDetails - if(!firstname || !lastname || !year || !month) { - setRequestStatus({loading:false, status:false, message: 'Please fill all fields'}) - return setTimeout(()=>{ - setRequestStatus({loading:false, status:false, message: ''}) - }, 5000) - } - - api.getFamilyUpdate(updateDetails).then(res=>{ + // let {firstname, lastname, year, month} = updateDetails + // if(!firstname || !lastname || year == 0 || month == 0) { + // setRequestStatus({loading:false, status:false, message: 'Please fill all fields'}) + // return setTimeout(()=>{ + // setRequestStatus({loading:false, status:false, message: ''}) + // }, 5000) + // } + api.getFamilyUpdate(values).then(res=>{ if(res.data.internal_return < 0){ return setRequestStatus({loading:false, status:false, message: 'Failed, try again!'}) } @@ -56,103 +86,122 @@ export default function FamilyProfile({familyData, className }) { className || "" }`} > -
- - -
- {/* Age dropdown */} -
- -
- -
- {requestStatus.message && ( -
- {requestStatus.message} -
- )} -
- {requestStatus.loading ? - <> -
- - - : - - } -
- + + {(props) => { + return ( +
+ + +
+ {/* Age dropdown */} +
+ + {((props.errors.year && props.touched.year) || (props.errors.month && props.touched.month)) && ( + + {' '}{props.errors.year || props.errors.month} + + )} +
+
+ + + +
+
+ {requestStatus.message && ( +
+ {requestStatus.message} +
+ )} +
+ {requestStatus.loading ? + <> +
+ + + : + + } +
+ + ); + }} +
); } - - - -function YearMonthDropdowns({ - selectedYear, - selectedMonth, - handleChange -}) { // Get the current year + // const currentYear = new Date().getFullYear() - process.env.REACT_APP_FAMILY_MINIMUM_AGE; const currentYear = new Date().getFullYear(); // Generate an array of years from the current year to (currentYear - 19) - const years = Array.from({ length: 17 }, (_, index) => currentYear - index); - - // Array of month names - // const months = [ - // "January", - // "February", - // "March", - // "April", - // "May", - // "June", - // "July", - // "August", - // "September", - // "October", - // "November", - // "December", - // ]; + const years = Array.from({ length: 19 }, (_, index) => currentYear - index); const months = [ {id:'1', name:"January"}, @@ -169,38 +218,4 @@ function YearMonthDropdowns({ {id:'12', name:"December"}, ]; - return ( -
- - - -
- ); -}