Skip to content
This repository has been 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 4 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
179 changes: 149 additions & 30 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 './helper';

// 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 @@ -308,6 +312,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 +346,40 @@ function FormEducationInfo({
setActiveStep(2);
};

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

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 +399,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 @@ -400,6 +411,114 @@ function FormEducationInfo({
validators={['required']}
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']}
>
{formData.state === undefined ? (
<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']}
>
{formData.district === undefined ? (
<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
Expand Down
27 changes: 27 additions & 0 deletions src/views/pages/ApplicationsView/helper.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];
}