Skip to content

Commit

Permalink
country code for edit and add
Browse files Browse the repository at this point in the history
  • Loading branch information
adhiraj23zelthy committed Sep 4, 2024
1 parent 062bc1a commit 83884bc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
1 change: 1 addition & 0 deletions frontend/src/mocks/appUsersManagementHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export const appUsersManagementHandlers = [
previous: null,
records: searchValue ? [] : slicedData,
},
pn_country_code:'+213',
dropdown_options: {
roles: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ import {
toggleRerenderPage,
} from '../../../slice';
import CountryCodeSelector from '../../../../../components/Form/CountryCodeSelector';
import { useState } from 'react';
import { useState , useLayoutEffect} from 'react';
import { countryCodeList } from '../../../../../utils/countryCodes';

const AddNewUserForm = ({ closeModal }) => {
const [countryCode,setCountryCode] = useState({
name: 'India',
dial_code: '+91',
code: 'IN',
})
name: 'India',
dial_code: '+91',
code: 'IN',
})
const [mobileValid,setMobileValid] = useState(true)
let { appId } = useParams();
const dispatch = useDispatch();

const appUserManagementData = useSelector(selectAppUserManagementData);
const triggerApi = useApi();
let pn_country_code = appUserManagementData?.pn_country_code ?? '+91'
useLayoutEffect(()=>{
let countryCodeObj = countryCodeList.find((c)=>c.dial_code===pn_country_code)
setCountryCode(countryCodeObj)
},[])
let initialValues = {
name: '',
email: '',
Expand Down Expand Up @@ -65,6 +71,7 @@ const AddNewUserForm = ({ closeModal }) => {
);

let onSubmit = (values) => {
setMobileValid(true)
let tempValues = {...values,mobile:countryCode?.dial_code+values.mobile}
let dynamicFormData = transformToFormData(tempValues);

Expand Down Expand Up @@ -143,7 +150,7 @@ const AddNewUserForm = ({ closeModal }) => {
placeholder="00000 00000"
/>
</div>
<p className='text-red-600 text-[11px]'>{!mobileValid && 'Not a valid mobile number'}</p>
<p className='text-red-600 text-[11px]'>{!mobileValid && 'Invalid phone number'}</p>
{formik.touched.mobile && formik.errors.mobile ? (
<div data-cy="error_message" className="font-lato text-form-xs text-[#cc3300]">
{formik.errors.mobile}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Formik } from 'formik';
import { get } from 'lodash';
import { useLayoutEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import * as Yup from 'yup';
import InputField from '../../../../../components/Form/InputField';
import MultiSelectField from '../../../../../components/Form/MultiSelectField';
import CountryCodeSelector from '../../../../../components/Form/CountryCodeSelector';
import SubmitButton from '../../../../../components/Form/SubmitButton';
import useApi from '../../../../../hooks/useApi';
import { transformToFormData } from '../../../../../utils/form';
Expand All @@ -13,25 +15,35 @@ import {
selectAppUserManagementFormData,
toggleRerenderPage,
} from '../../../slice';
import { countryCodeList } from '../../../../../utils/countryCodes';

const EditUserDetailsForm = ({ closeModal }) => {
let { appId } = useParams();
const dispatch = useDispatch();
const [mobileValid,setMobileValid] = useState(true)
const [countryCode,setCountryCode] = useState({
name: 'India',
dial_code: '+91',
code: 'IN',
})

const appUserManagementData = useSelector(selectAppUserManagementData);
const appUserManagementFormData = useSelector(
let appUserManagementFormData = useSelector(
selectAppUserManagementFormData
);

const triggerApi = useApi();
let pn_country_code = appUserManagementFormData?.pn_country_code ?? '+91'
useLayoutEffect(()=>{
let countryCodeObj = countryCodeList.find((c)=>c.dial_code===pn_country_code)
setCountryCode(countryCodeObj)
},[])


let initialValues = {
name: appUserManagementFormData?.name ?? '',
email: appUserManagementFormData?.email ?? '',
mobile: appUserManagementFormData?.mobile
? appUserManagementFormData?.mobile.indexOf('+91') > -1
? appUserManagementFormData?.mobile?.slice(3) ?? ''
: appUserManagementFormData?.mobile
: '',
mobile: pn_country_code.length>0?appUserManagementFormData?.mobile.slice(pn_country_code.length):appUserManagementFormData?.mobile,
roles: appUserManagementFormData?.roles?.map((eachApp) => eachApp.id) ?? [],
};

Expand All @@ -49,22 +61,16 @@ const EditUserDetailsForm = ({ closeModal }) => {
is: (email) => {
if (!email) return true;
},
then: Yup.string()
.min(10, 'Must be 10 digits')
.max(10, 'Must be 10 digits')
.required('Required'),
otherwise: Yup.string()
.min(10, 'Must be 10 digits')
.max(10, 'Must be 10 digits'),
then: Yup.string().required('Required')
}),
roles: Yup.array().min(1, 'Minimun one is required').required('Required'),
},
[['name'], ['mobile', 'email'], ['email', 'mobile'], ['roles']]
);

let onSubmit = (values) => {
let tempValues = values;

setMobileValid(true)
let tempValues = {...values,mobile:countryCode?.dial_code+values.mobile}
let dynamicFormData = transformToFormData(tempValues);

const makeApiCall = async () => {
Expand All @@ -78,6 +84,8 @@ const EditUserDetailsForm = ({ closeModal }) => {
if (success && response) {
closeModal();
dispatch(toggleRerenderPage());
}else{
setMobileValid(false)
}
};

Expand Down Expand Up @@ -124,9 +132,11 @@ const EditUserDetailsForm = ({ closeModal }) => {
>
Mobile
</label>
<div className="flex gap-[12px] rounded-[6px] border border-[#DDE2E5] px-[12px] py-[14px]">
<span className="font-lato text-[#6C747D]">+91</span>
<input
<div className="flex gap-[12px] rounded-[6px] border border-[#DDE2E5] px-[12px]">
<span className="font-lato text-[#6C747D]">
<CountryCodeSelector countryCode={countryCode} setCountryCode={setCountryCode} />
</span>
<input
id="mobile"
name="mobile"
type="number"
Expand All @@ -137,6 +147,7 @@ const EditUserDetailsForm = ({ closeModal }) => {
placeholder="00000 00000"
/>
</div>
<p className='text-red-600 text-[11px]'>{!mobileValid && 'Invalid phone number'}</p>
{formik.touched.mobile && formik.errors.mobile ? (
<div className="font-lato text-form-xs text-[#cc3300]">
{formik.errors.mobile}
Expand Down

0 comments on commit 83884bc

Please sign in to comment.