Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Added Selectors for State Districts and Colleges #78

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 156 additions & 31 deletions src/views/pages/ApplicationsView/ApplicationSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
SelectValidator
} from 'react-material-ui-form-validator';

import { getStates, getDistrict, getColleges } from './educationFormUtil';

// import axios from 'axios';

const useStyles = makeStyles(theme => ({
root: {
width: '100%'
Expand Down Expand Up @@ -93,7 +97,7 @@ export function ApplicationSteps({ applicationId, setCourseTitle }) {
const isMountedRef = useIsMountedRef();
const baseUrl =
'https://us-central1-codeforcauseorg.cloudfunctions.net/widgets/applications/request';
const [activeStep, setActiveStep] = React.useState(-1);
const [activeStep, setActiveStep] = React.useState(1);
Copy link
Contributor Author

@Abhishek-kumar09 Abhishek-kumar09 Sep 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporary, just for easy review

1st thing to change after/before merging PR

const [formData, setFormData] = React.useState({
personal: {},
education: {},
Expand Down Expand Up @@ -177,7 +181,10 @@ export function ApplicationSteps({ applicationId, setCourseTitle }) {
)}
{activeStep === steps.length + 1 && (
<Paper square elevation={0} className={classes.resetContainer}>
<Typography>Application have been submitted successfully. We will email/call you for further process.</Typography>
<Typography>
Application have been submitted successfully. We will email/call you
for further process.
</Typography>
</Paper>
)}
</div>
Expand Down Expand Up @@ -308,6 +315,9 @@ function FormEducationInfo({
}) {
const classes = useStyles();
const [formData, updateFormData] = useState(data.education);
const [districtToCollegePair, updateDistrictToCollegePair] = useState({});
const [districts, updateDistricts] = useState([]);
const [collegeList, updateCollege] = useState([]);

const handleChange = event => {
updateFormData({
Expand Down Expand Up @@ -339,9 +349,42 @@ function FormEducationInfo({
setActiveStep(2);
};

const handleStateFieldChange = event => {
updateFormData({
...formData,
[event.target.name]: event.target.value
});
updateDistrictToCollegePair(getDistrict(event.target.value));
updateDistricts([]);
updateCollege([]);
};

const handleDistrictFieldChange = event => {
updateFormData({
...formData,
[event.target.name]: event.target.value
});
updateCollege(getColleges(districtToCollegePair, event.target.value));
};

const handleDistrictFocus = () => {
if (formData.state !== undefined) {
updateDistricts(Object.keys(districtToCollegePair));
}
};

function notIndia() {
return (
formData.country === undefined ||
formData.country.trim().toLowerCase() !== 'india'
);
}

const years = Array(25)
.fill(2000)
.map((x, y) => x + y);
.fill(2024)
.map((x, y) => x - y);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most often, Courses will be booked by recent graduates, so reversed the array from
2000-2024 to 2024-2000


const states = getStates();

return (
<ValidatorForm onSubmit={handleSubmit}>
Expand All @@ -361,33 +404,6 @@ function FormEducationInfo({
return <MenuItem value={year}>{year}</MenuItem>;
})}
</SelectValidator>

<TextValidator
key="college"
className={classes.textField}
label="College"
variant="outlined"
value={formData.college}
fullWidth
name="college"
onChange={handleChange}
validators={['required']}
errorMessages={['College is a required field']}
/>

<TextValidator
key="state"
className={classes.textField}
label="State"
variant="outlined"
value={formData.state}
fullWidth
name="state"
onChange={handleChange}
validators={['required']}
errorMessages={['State is a required field']}
/>

<TextValidator
key="country"
className={classes.textField}
Expand All @@ -401,6 +417,115 @@ function FormEducationInfo({
errorMessages={['Country is a required field']}
/>

{notIndia() ? (
<TextValidator
key="state"
className={classes.textField}
autoComplete="true"
label="State"
variant="outlined"
value={formData.state}
fullWidth
name="state"
onChange={handleChange}
validators={['required']}
errorMessages={['State is a required field']}
/>
) : (
<SelectValidator
key="state"
className={classes.textField}
autoComplete="true"
label="State"
variant="outlined"
value={formData.state}
fullWidth
name="state"
onChange={handleStateFieldChange}
validators={['required']}
errorMessages={['State is a required field']}
>
{states.sort().map(state => {
return <MenuItem value={state}>{state}</MenuItem>;
})}
</SelectValidator>
)}

{notIndia() ? (
<TextValidator
key="district"
className={classes.textField}
autoComplete="true"
label="District"
variant="outlined"
value={formData.district}
fullWidth
name="district"
onChange={handleChange}
validators={['required']}
errorMessages={['State is a required field']}
/>
) : (
<SelectValidator
key="district"
className={classes.textField}
autoComplete="true"
label="District"
variant="outlined"
value={formData.district}
fullWidth
onFocus={handleDistrictFocus}
name="district"
onChange={handleDistrictFieldChange}
validators={['required']}
errorMessages={['State is a required field']}
>
{districts.length === 0 ? (
<MenuItem value="undefined">{'Select State'}</MenuItem>
) : (
districts.sort().map(dis => {
return <MenuItem value={dis}>{dis}</MenuItem>;
})
)}
</SelectValidator>
)}

{notIndia() ? (
<TextValidator
key="college"
className={classes.textField}
label="College"
variant="outlined"
value={formData.college}
onChange={handleChange}
fullWidth
name="college"
validators={['required']}
errorMessages={['College is a required field']}
/>
) : (
<SelectValidator
key="college"
className={classes.textField}
label="College"
variant="outlined"
value={formData.college}
fullWidth
onChange={handleChange}
name="college"
validators={['required']}
errorMessages={['College is a required field']}
>
{collegeList.length === 0 ? (
<MenuItem value="undefined">{'Select District'}</MenuItem>
) : (
collegeList.sort().map(college => {
return <MenuItem value={college}>{college}</MenuItem>;
})
)}
</SelectValidator>
)}

<Button variant="outlined" onClick={handlePrev} color="secondary">
Prev
</Button>
Expand Down
27 changes: 27 additions & 0 deletions src/views/pages/ApplicationsView/educationFormUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Data from '../../../data/colleges.json';

export function getStates() {
return Object.keys(Data);
}

export function getDistrict(state) {
const disToC = Object.keys(Data)
.filter(st => st === state)
.reduce((obj, key) => {
obj[key] = Data[key];
return obj;
}, {});

return Object.values(disToC)[0];
}

export function getColleges(districtToCollegePair, district) {
const colleges = Object.keys(districtToCollegePair)
.filter(dis => dis === district)
.reduce((obj, key) => {
obj[key] = districtToCollegePair[key];
return obj;
}, {});

return Object.values(colleges)[0];
}