Skip to content

Commit 65df09a

Browse files
Used date-fns to autofill IEP end date and correctly format IEP dates in student_id (#225)
* used date-fns to format dates in student_id so that IEP date values are as per user selection both in the front end and in the database * updated student IEP date to format correctly and autofill end date in-file, without exporting an IepDateInput component * cleaned up commented-out code * updated function for autofilling end date to handleAutofillIepEndDate and add comment explaining why that is the date
1 parent eb1f3e4 commit 65df09a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/pages/students/[student_id].tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ import $Form from "../../styles/Form.module.css";
1616
import $Modal from "../../styles/Modal.module.css";
1717
import $StudentPage from "../../styles/StudentPage.module.css";
1818
import $Image from "../../styles/Image.module.css";
19+
import { parseISO, addYears, subDays, format } from "date-fns";
1920

2021
const ViewStudentPage = () => {
2122
const [createIepModal, setCreateIepModal] = useState(false);
2223
const [archivePrompt, setArchivePrompt] = useState(false);
24+
const [startDate, setStartDate] = useState("");
25+
const [endDate, setEndDate] = useState("");
2326

2427
const utils = trpc.useContext();
2528
const router = useRouter();
@@ -48,6 +51,16 @@ const ViewStudentPage = () => {
4851
onSuccess: () => utils.student.getActiveStudentIep.invalidate(),
4952
});
5053

54+
const handleAutofillIepEndDate = (date: string) => {
55+
//new IEP generally starts on same date annually, so end date autofills to the day before one year from start date
56+
setStartDate(date);
57+
const parsedDate: Date = parseISO(date);
58+
const datePlusOneYear: Date = addYears(parsedDate, 1);
59+
const finalDate: Date = subDays(datePlusOneYear, 1);
60+
const formattedEndDate: string = format(finalDate, "yyyy-MM-dd");
61+
setEndDate(formattedEndDate);
62+
};
63+
5164
const handleIepSubmit = (event: React.FormEvent<HTMLFormElement>) => {
5265
event.preventDefault();
5366
const data = new FormData(event.currentTarget);
@@ -57,8 +70,8 @@ const ViewStudentPage = () => {
5770
}
5871
iepMutation.mutate({
5972
student_id: student.student_id,
60-
start_date: new Date(data.get("start_date") as string),
61-
end_date: new Date(data.get("end_date") as string),
73+
start_date: new Date(parseISO(data.get("start_date") as string)),
74+
end_date: new Date(parseISO(data.get("end_date") as string)),
6275
});
6376

6477
setCreateIepModal(false);
@@ -195,6 +208,10 @@ const ViewStudentPage = () => {
195208
type="date"
196209
name="start_date"
197210
placeholder="IEP start date"
211+
value={startDate}
212+
onChange={(e) => {
213+
handleAutofillIepEndDate(e.target.value);
214+
}}
198215
required
199216
/>
200217
</Box>
@@ -204,6 +221,7 @@ const ViewStudentPage = () => {
204221
type="date"
205222
name="end_date"
206223
placeholder="IEP end date"
224+
value={endDate}
207225
required
208226
/>
209227
</Box>

0 commit comments

Comments
 (0)