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

Commit 5f2814f

Browse files
Added AutoSuggestion for District state college
1 parent fa8a106 commit 5f2814f

File tree

2 files changed

+173
-27
lines changed

2 files changed

+173
-27
lines changed

src/views/pages/ApplicationsView/ApplicationSteps.js

Lines changed: 146 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import {
1717
SelectValidator
1818
} from 'react-material-ui-form-validator';
1919

20+
import { getStates, getDistrict, getColleges } from './helper';
21+
22+
// import axios from 'axios';
23+
2024
const useStyles = makeStyles(theme => ({
2125
root: {
2226
width: '100%'
@@ -308,6 +312,9 @@ function FormEducationInfo({
308312
}) {
309313
const classes = useStyles();
310314
const [formData, updateFormData] = useState(data.education);
315+
const [districtToCollegePair, updateDistrictToCollegePair] = useState({});
316+
const [districts, updateDistricts] = useState([]);
317+
const [collegeList, updateCollege] = useState([]);
311318

312319
const handleChange = event => {
313320
updateFormData({
@@ -339,9 +346,40 @@ function FormEducationInfo({
339346
setActiveStep(2);
340347
};
341348

349+
const handleStateFieldChange = event => {
350+
updateFormData({
351+
...formData,
352+
[event.target.name]: event.target.value
353+
});
354+
updateDistrictToCollegePair(getDistrict(event.target.value));
355+
};
356+
357+
const handleDistrictFieldChange = event => {
358+
updateFormData({
359+
...formData,
360+
[event.target.name]: event.target.value
361+
});
362+
updateCollege(getColleges(districtToCollegePair, event.target.value));
363+
};
364+
365+
const handleDistrictFocus = () => {
366+
if (formData.state != undefined) {
367+
updateDistricts(Object.keys(districtToCollegePair));
368+
}
369+
};
370+
371+
function notIndia() {
372+
return (
373+
formData.country === undefined ||
374+
formData.country.trim().toLowerCase() !== 'india'
375+
);
376+
}
377+
342378
const years = Array(25)
343-
.fill(2000)
344-
.map((x, y) => x + y);
379+
.fill(2024)
380+
.map((x, y) => x - y);
381+
382+
const states = getStates();
345383

346384
return (
347385
<ValidatorForm onSubmit={handleSubmit}>
@@ -361,7 +399,6 @@ function FormEducationInfo({
361399
return <MenuItem value={year}>{year}</MenuItem>;
362400
})}
363401
</SelectValidator>
364-
365402
<TextValidator
366403
key="college"
367404
className={classes.textField}
@@ -374,32 +411,114 @@ function FormEducationInfo({
374411
validators={['required']}
375412
errorMessages={['College is a required field']}
376413
/>
414+
{notIndia() ? (
415+
<TextValidator
416+
key="state"
417+
className={classes.textField}
418+
autoComplete="true"
419+
label="State"
420+
variant="outlined"
421+
value={formData.state}
422+
fullWidth
423+
name="state"
424+
onChange={handleChange}
425+
validators={['required']}
426+
errorMessages={['State is a required field']}
427+
/>
428+
) : (
429+
<SelectValidator
430+
key="state"
431+
className={classes.textField}
432+
autoComplete="true"
433+
label="State"
434+
variant="outlined"
435+
value={formData.state}
436+
fullWidth
437+
name="state"
438+
onChange={handleStateFieldChange}
439+
validators={['required']}
440+
errorMessages={['State is a required field']}
441+
>
442+
{states.sort().map(state => {
443+
return <MenuItem value={state}>{state}</MenuItem>;
444+
})}
445+
</SelectValidator>
446+
)}
377447

378-
<TextValidator
379-
key="state"
380-
className={classes.textField}
381-
label="State"
382-
variant="outlined"
383-
value={formData.state}
384-
fullWidth
385-
name="state"
386-
onChange={handleChange}
387-
validators={['required']}
388-
errorMessages={['State is a required field']}
389-
/>
448+
{notIndia() ? (
449+
<TextValidator
450+
key="district"
451+
className={classes.textField}
452+
autoComplete="true"
453+
label="District"
454+
variant="outlined"
455+
value={formData.district}
456+
fullWidth
457+
name="district"
458+
onChange={handleChange}
459+
validators={['required']}
460+
errorMessages={['State is a required field']}
461+
/>
462+
) : (
463+
<SelectValidator
464+
key="district"
465+
className={classes.textField}
466+
autoComplete="true"
467+
label="District"
468+
variant="outlined"
469+
value={formData.district}
470+
fullWidth
471+
onFocus={handleDistrictFocus}
472+
name="district"
473+
onChange={handleDistrictFieldChange}
474+
validators={['required']}
475+
errorMessages={['State is a required field']}
476+
>
477+
{formData.state === undefined ? (
478+
<MenuItem value="undefined">{'Select State'}</MenuItem>
479+
) : (
480+
districts.sort().map(dis => {
481+
return <MenuItem value={dis}>{dis}</MenuItem>;
482+
})
483+
)}
484+
</SelectValidator>
485+
)}
390486

391-
<TextValidator
392-
key="country"
393-
className={classes.textField}
394-
label="Country"
395-
variant="outlined"
396-
value={formData.country}
397-
fullWidth
398-
name="country"
399-
onChange={handleChange}
400-
validators={['required']}
401-
errorMessages={['Country is a required field']}
402-
/>
487+
{notIndia() ? (
488+
<TextValidator
489+
key="college"
490+
className={classes.textField}
491+
label="College"
492+
variant="outlined"
493+
value={formData.college}
494+
onChange={handleChange}
495+
fullWidth
496+
name="college"
497+
validators={['required']}
498+
errorMessages={['College is a required field']}
499+
/>
500+
) : (
501+
<SelectValidator
502+
key="college"
503+
className={classes.textField}
504+
label="College"
505+
variant="outlined"
506+
value={formData.college}
507+
fullWidth
508+
onChange={handleChange}
509+
name="college"
510+
validators={['required']}
511+
errorMessages={['College is a required field']}
512+
>
513+
{formData.district === undefined ? (
514+
<MenuItem value="undefined">{'Select District'}</MenuItem>
515+
) : (
516+
collegeList.sort().map(college => {
517+
return <MenuItem value={college}>{college}</MenuItem>;
518+
})
519+
)}
520+
</SelectValidator>
521+
)}
403522

404523
<Button variant="outlined" onClick={handlePrev} color="secondary">
405524
Prev
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Data from '../../../data/colleges.json';
2+
3+
export function getStates() {
4+
return Object.keys(Data);
5+
}
6+
7+
export function getDistrict(state) {
8+
const disToC = Object.keys(Data)
9+
.filter(st => st == state)
10+
.reduce((obj, key) => {
11+
obj[key] = Data[key];
12+
return obj;
13+
}, {});
14+
15+
return Object.values(disToC)[0];
16+
}
17+
18+
export function getColleges(districtToCollegePair, district) {
19+
const colleges = Object.keys(districtToCollegePair)
20+
.filter(dis => dis == district)
21+
.reduce((obj, key) => {
22+
obj[key] = districtToCollegePair[key];
23+
return obj;
24+
}, {});
25+
26+
return Object.values(colleges)[0];
27+
}

0 commit comments

Comments
 (0)