1
0
Fork 0
This repository has been archived on 2024-09-27. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
bearlypassing/frontend/src/components/dashboard/QuickEmailCard.tsx
2024-09-27 11:17:53 +01:00

79 lines
No EOL
3.4 KiB
TypeScript

import Card from "react-bootstrap/Card";
import Button from "react-bootstrap/Button";
import DashboardCard from "./DashboardCard";
import {useContext, useMemo, useState} from "react";
import {Teacher, Term} from "../../interfaces";
import Form from "react-bootstrap/Form";
import {isHiddenTeacher, sectionsTaughtThisTerm} from "../../lib/teachers";
import {globalContext} from "../../contexts";
import {getTeachersForDay} from "../../lib/teachers";
type QuickEmailButtonProps = { title: string, teachers: Teacher[], body: string }
const QuickEmailButton = ({title, teachers, body}: QuickEmailButtonProps) =>
<Button disabled={teachers.length === 0}
onClick={() => openEmailTemplate(teachers.map(t => t.email), body)}>{title}</Button>;
const openEmailTemplate = (to: string[], body: string) => {
const deduplicatedEmails = Array.from(new Set(to));
// Open the mailto URL and focus the window
const win = window.open(`mailto:${deduplicatedEmails.join(",")}?body=${encodeURI(body)}`, "_blank");
if (win !== null) {
win.focus();
}
};
type TeacherObject = { semester: Teacher[], today: Teacher[], tomorrow: Teacher[] }
export default function QuickEmailCard({currentTerm}: { currentTerm: Term | null }) {
const {psData} = useContext(globalContext);
const [hideTeachers, setHideTeachers] = useState(true);
const teachers = useMemo(() => {
const teachers: TeacherObject = {
semester: [],
today: [],
tomorrow: []
};
const currentDate = new Date();
if (currentTerm !== null) {
teachers.semester = psData.teachers.filter(t => sectionsTaughtThisTerm(t, currentTerm));
teachers.today = getTeachersForDay(currentDate, psData.schedule);
teachers.tomorrow = getTeachersForDay(new Date(new Date().setDate(currentDate.getDate() + 1)), psData.schedule);
}
return teachers;
}, [psData.teachers, currentTerm, psData.schedule]);
const displayTeachers: TeacherObject = useMemo(() =>
(!hideTeachers || currentTerm === null)
? teachers
: {
semester: teachers.semester.filter(t => !isHiddenTeacher(t, currentTerm)),
today: teachers.today.filter(t => !isHiddenTeacher(t, currentTerm)),
tomorrow: teachers.tomorrow.filter(t => !isHiddenTeacher(t, currentTerm))
}
, [teachers, hideTeachers, currentTerm]);
const emailBodyTemplate = `Dear teachers,\n\n\n\nThanks,\n${psData.student.firstName}`;
return (
<DashboardCard>
<Card.Title>Quick Email Compose to:</Card.Title>
<Form.Check type="checkbox" label="Hide teachers without graded classes" checked={hideTeachers}
onChange={e => setHideTeachers(e.currentTarget.checked)}/>
<div className="d-grid gap-2">
<QuickEmailButton title="All current teachers" teachers={displayTeachers.semester}
body={emailBodyTemplate}/>
<QuickEmailButton title="All teachers with classes today" teachers={displayTeachers.today}
body={emailBodyTemplate}/>
<QuickEmailButton title="All teachers with classes tomorrow" teachers={displayTeachers.tomorrow}
body={emailBodyTemplate}/>
</div>
</DashboardCard>
)
};