-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAddCoursePopup.tsx
151 lines (139 loc) · 4.37 KB
/
AddCoursePopup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { FC, useState } from 'react';
import Form from 'react-bootstrap/Form';
import Button from 'react-bootstrap/Button';
import './AddCoursePopup.scss';
import { useAppDispatch, useAppSelector } from '../../store/hooks';
import { moveCourse, setShowAddCourse } from '../../store/slices/roadmapSlice';
import Modal from 'react-bootstrap/Modal';
import { isMobile } from 'react-device-detect';
interface AddCoursePopupProps {}
const AddCoursePopup: FC<AddCoursePopupProps> = () => {
const dispatch = useAppDispatch();
const planner = useAppSelector((state) => state.roadmap.yearPlans);
const showForm = useAppSelector((state) => state.roadmap.showAddCourse);
const [year, setYear] = useState(-1);
const [quarter, setQuarter] = useState(-1);
const [validated, setValidated] = useState(false);
const closeForm = () => {
// close form
dispatch(setShowAddCourse(false));
};
const submit = (event: React.FormEvent<HTMLFormElement>) => {
// validate form
const form = event.currentTarget;
const valid = form.checkValidity();
event.preventDefault();
event.stopPropagation();
// validated
setValidated(true);
// do not proceed if not valid
if (valid === false) {
return;
}
// add course to roadmap
dispatch(
moveCourse({
from: {
yearIndex: -1,
quarterIndex: -1,
courseIndex: -1,
},
to: {
yearIndex: year,
quarterIndex: quarter,
courseIndex: 0,
},
}),
);
closeForm();
};
function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
const addCourseForm = (
<Form noValidate validated={validated} onSubmit={submit}>
<h2 className="add-course-form-header">Add Course</h2>
<p>Where do you want to add this course?</p>
<Form.Group controlId="year">
<Form.Label>School Year</Form.Label>
<Form.Control
as="select"
name="year"
id="year"
required
onChange={(e) => {
const parsed = parseInt(e.target.value);
console.log(parsed, isNaN(parsed));
if (isNaN(parsed)) {
setYear(-1);
} else {
setYear(parsed);
}
}}
>
<option disabled={true} selected value="">
Year
</option>
{planner.map((plannerYear, i) => {
const value = plannerYear.startYear;
return (
<option key={'add-course-form-year-' + i} value={i}>
{value} - {value + 1}
</option>
);
})}
</Form.Control>
<Form.Control.Feedback type="invalid">Missing year</Form.Control.Feedback>
</Form.Group>
{year != -1 && (
<Form.Group controlId="quarter">
<Form.Label>Quarter</Form.Label>
<Form.Control
as="select"
name="quarter"
id="quarter"
required
onChange={(e) => {
const parsed = parseInt(e.target.value);
if (!isNaN(parsed)) {
setQuarter(parsed);
}
}}
>
<option disabled={true} selected value="">
Quarter
</option>
{planner[year].quarters.map((plannerQuarter, i) => {
const value = capitalizeFirstLetter(plannerQuarter.name);
return (
<option key={'add-course-form-quarter-' + i} value={i}>
{value}
</option>
);
})}
</Form.Control>
<Form.Control.Feedback type="invalid">Missing quarter</Form.Control.Feedback>
</Form.Group>
)}
<div className="d-flex justify-content-end">
<Button
className="py-2 px-4 mr-3"
variant="outline-secondary"
size={isMobile ? 'sm' : undefined}
onClick={closeForm}
>
Cancel
</Button>
<Button className="py-2 px-4" type="submit" variant="secondary" size={isMobile ? 'sm' : undefined}>
Submit
</Button>
</div>
</Form>
);
return (
<Modal show={showForm} animation={false} onHide={closeForm} centered>
<div className="add-course-form">{addCourseForm}</div>
</Modal>
);
};
export default AddCoursePopup;