Skip to content

Commit

Permalink
Saved ExamSchedule to local storage, move some function to utils.ts.
Browse files Browse the repository at this point in the history
  • Loading branch information
smitug01 committed Sep 14, 2023
1 parent 5075203 commit 390358b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
25 changes: 16 additions & 9 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import { useState, useEffect, FC } from "react";
import EditDialog from "@/components/editDialog";
import { Exam, Attendance, EditingData } from "@/lib/interfaces";
import {
saveExamScheduleToLocalStorage,
loadExamScheduleFromLocalStorage,
calculateRemainingTime
} from "@/lib/utils";

const Home: FC = () => {
const [isDialogOpen, setIsDialogOpen] = useState<boolean>(false);
Expand Down Expand Up @@ -68,6 +73,17 @@ const Home: FC = () => {
setIsDialogOpen(false);
};

useEffect(() => {
const savedSchedule = loadExamScheduleFromLocalStorage();
if (savedSchedule.length > 0) {
setExamSchedule(savedSchedule);
}
}, []);

useEffect(() => {
saveExamScheduleToLocalStorage(examSchedule);
}, [examSchedule]);

useEffect(() => {
const intervalId = setInterval(() => {
setCurrentTime(new Date());
Expand All @@ -92,15 +108,6 @@ const Home: FC = () => {
setCurrentExam(current ?? null);
}, [currentTime, examSchedule]);

const calculateRemainingTime = (endTime: string): number => {
const now = new Date();
const end = new Date(now.toDateString() + " " + endTime);
const remainingMinutes = Math.floor(
(end.getTime() - now.getTime()) / 60000,
);
return remainingMinutes >= 0 ? remainingMinutes : 0;
};

return (
<>
<div className="absolute top-0 left-0 p-2 text-xs text-gray-400 font-bold">
Expand Down
17 changes: 17 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Exam } from "@lib/interfaces";

export const saveExamScheduleToLocalStorage = (schedule: Exam[]) => {
localStorage.setItem('examSchedule', JSON.stringify(schedule));
};

export const loadExamScheduleFromLocalStorage = () => {
const schedule = localStorage.getItem('examSchedule');
return schedule ? JSON.parse(schedule) : [];
};

export const calculateRemainingTime = (endTime: string) => {
const now = new Date();
const end = new Date(now.toDateString() + " " + endTime);
const remainingMinutes = Math.floor((end.getTime() - now.getTime()) / 60000);
return remainingMinutes >= 0 ? remainingMinutes : 0;
};

0 comments on commit 390358b

Please sign in to comment.