-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathQuarter.tsx
180 lines (166 loc) · 5.65 KB
/
Quarter.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { FC, useContext, useRef, useState } from 'react';
import { Draggable } from 'react-beautiful-dnd';
import { Button, OverlayTrigger, Popover } from 'react-bootstrap';
import { Plus, ThreeDots } from 'react-bootstrap-icons';
import { quarterDisplayNames } from '../../helpers/planner';
import { useIsMobile } from '../../helpers/util';
import { useAppDispatch, useAppSelector } from '../../store/hooks';
import { clearQuarter, deleteCourse, deleteQuarter, setShowSearch } from '../../store/slices/roadmapSlice';
import ThemeContext from '../../style/theme-context';
import { PlannerQuarterData } from '../../types/types';
import './Quarter.scss';
import { StrictModeDroppable } from './StrictModeDroppable';
import Course from './Course';
interface QuarterProps {
year: number;
yearIndex: number;
quarterIndex: number;
data: PlannerQuarterData;
}
const Quarter: FC<QuarterProps> = ({ year, yearIndex, quarterIndex, data }) => {
const dispatch = useAppDispatch();
const quarterTitle = quarterDisplayNames[data.name];
const invalidCourses = useAppSelector((state) => state.roadmap.invalidCourses);
const quarterContainerRef = useRef<HTMLDivElement>(null);
const isMobile = useIsMobile();
const [showQuarterMenu, setShowQuarterMenu] = useState(false);
const { darkMode } = useContext(ThemeContext);
const buttonVariant = darkMode ? 'dark' : 'light';
const handleQuarterMenuClick = () => {
setShowQuarterMenu(!showQuarterMenu);
};
const calculateQuarterStats = () => {
let unitCount = 0;
let courseCount = 0;
data.courses.forEach((course) => {
unitCount += course.minUnits;
courseCount += 1;
});
return [unitCount, courseCount];
};
const unitCount = calculateQuarterStats()[0];
const renderCourses = () => {
return data.courses.map((course, index) => {
return (
<Draggable
key={`quarter-course-${index}`}
draggableId={`${yearIndex}-${quarterIndex}-${course.id}-${index}`}
index={index}
>
{(provided) => {
let requiredCourses: string[] = null!;
// if this is an invalid course, set the required courses
invalidCourses.forEach((ic) => {
const loc = ic.location;
if (loc.courseIndex == index && loc.quarterIndex == quarterIndex && loc.yearIndex == yearIndex) {
requiredCourses = ic.required;
}
});
const onDelete = () => {
dispatch(
deleteCourse({
yearIndex,
quarterIndex,
courseIndex: index,
}),
);
};
return (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
style={{
margin: '0rem 2rem 1rem 2rem',
cursor: 'grab',
...provided.draggableProps.style,
}}
>
<Course key={course.id} {...course} requiredCourses={requiredCourses} onDelete={onDelete} />
</div>
);
}}
</Draggable>
);
});
};
const popover = (
<Popover id={`quarter-menu-${yearIndex}-${quarterIndex}`} className="quarter-menu-popover">
<Popover.Content>
<div>
<Button
variant={buttonVariant}
className="quarter-menu-btn red-menu-btn"
onClick={() => {
dispatch(clearQuarter({ yearIndex: yearIndex, quarterIndex: quarterIndex }));
setShowQuarterMenu(false);
}}
>
Clear
</Button>
<Button
variant={buttonVariant}
className="quarter-menu-btn red-menu-btn"
onClick={() => {
dispatch(deleteQuarter({ yearIndex: yearIndex, quarterIndex: quarterIndex }));
setShowQuarterMenu(false);
}}
>
Delete
</Button>
</div>
</Popover.Content>
</Popover>
);
return (
<div className="quarter" ref={quarterContainerRef}>
<span className="quarter-header">
<h2 className="quarter-title">
{quarterTitle} {year}
</h2>
<OverlayTrigger
trigger="click"
overlay={popover}
rootClose
onToggle={setShowQuarterMenu}
show={showQuarterMenu}
container={quarterContainerRef}
>
{({ ref, ...triggerHandler }) => (
<button ref={ref} {...triggerHandler} onClick={handleQuarterMenuClick} className="quarter-edit-btn">
<ThreeDots />
</button>
)}
</OverlayTrigger>
</span>
<div className="quarter-units">
{unitCount} {unitCount === 1 ? 'unit' : 'units'}
</div>
<StrictModeDroppable droppableId={yearIndex + '-' + quarterIndex} type="COURSE">
{(provided) => {
return (
<div ref={provided.innerRef} {...provided.droppableProps} style={{ paddingBottom: '1rem' }}>
{renderCourses()}
{provided.placeholder}
</div>
);
}}
</StrictModeDroppable>
{isMobile && (
<>
<Button
variant={buttonVariant}
className="quarter quarter-header"
onClick={() => {
dispatch(setShowSearch({ show: true, year: yearIndex, quarter: quarterIndex }));
}}
>
<Plus className="plus-icon" />
<div className="quarter-add-course">Add Course</div>
</Button>
</>
)}
</div>
);
};
export default Quarter;