Skip to content

Commit

Permalink
add nicer error handling, allow exam times to be null
Browse files Browse the repository at this point in the history
  • Loading branch information
soumitradev committed Aug 7, 2023
1 parent a42655c commit 2ecab0f
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 73 deletions.
160 changes: 106 additions & 54 deletions src/components/TimetableEditMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ArrowLeft,
ArrowUpRightFromCircle,
Bird,
HelpCircle,
} from "lucide-react";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
Expand Down Expand Up @@ -117,10 +118,10 @@ const TimetableEditMenu = ({
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
}[];
tabState: string;
setTabState: React.Dispatch<React.SetStateAction<string>>;
Expand All @@ -129,10 +130,10 @@ const TimetableEditMenu = ({
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
}
| {
id: null;
Expand All @@ -147,10 +148,10 @@ const TimetableEditMenu = ({
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
}[];
warning?: undefined;
}
Expand All @@ -163,20 +164,20 @@ const TimetableEditMenu = ({
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
clashing: null | string[];
}[];
addedCourses: {
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
}[];
}) => {
return (
Expand Down Expand Up @@ -403,10 +404,13 @@ const TimetableEditMenu = ({
<div className="w-full flex justify-center font-bold text-lg">
<Tooltip delayDuration={100}>
<TooltipTrigger asChild>
<span className="text-slate-50 z-20">
<div className="absolute border-2 border-slate-50 w-12 mt-[-0.1rem] ml-[-0.75rem] h-8 rounded-full"></div>
OR
</span>
<>
{" "}
<div className="absolute border-2 border-slate-50 w-12 mt-[-0.1rem] h-8 rounded-full"></div>
<span className="text-slate-50 z-20">
OR
</span>
</>
</TooltipTrigger>
<TooltipContent className="bg-slate-900 text-slate-50 border-slate-800">
You have to pick only one of these options
Expand Down Expand Up @@ -503,47 +507,95 @@ const TimetableEditMenu = ({
Midsem
</span>
<span className="pl-4 py-1 text-sm">
{`${new Date(
{`${
course.midsemStartTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})}${new Date(
? new Date(
course.midsemStartTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})
: "N/A"
}${
course.midsemEndTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})}`}
? new Date(
course.midsemEndTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})
: "N/A"
}`}
{course.midsemStartTime === null && (
<Tooltip delayDuration={100}>
<TooltipTrigger asChild>
<div className="inline bg-transparent w-fit rounded-full hover:bg-slate-800/80 text-slate-100 p-1 transition duration-200 ease-in-out ml-2 text-sm font-bold">
<HelpCircle className="inline h-4 w-4" />
</div>
</TooltipTrigger>
<TooltipContent className="w-96 bg-slate-700 text-slate-50 border-slate-600 text-md">
Timetable Division hasn't published the
midsem dates for this course. Either there
is no midsem exam, or they haven't decided
it yet. We recommend checking with your
professor.
</TooltipContent>
</Tooltip>
)}
</span>
</div>
<div className="pb-4">
<span className="pl-4 py-1 text-sm font-bold">
Compre
</span>
<span className="pl-4 py-1 text-sm">
{`${new Date(
{`${
course.compreStartTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})}${new Date(
? new Date(
course.compreStartTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})
: "N/A"
}${
course.compreEndTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})}`}
? new Date(
course.compreEndTime
).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
})
: "N/A"
}`}
{course.compreStartTime === null && (
<Tooltip delayDuration={100}>
<TooltipTrigger asChild>
<div className="inline bg-transparent w-fit rounded-full hover:bg-slate-800/80 text-slate-100 p-1 transition duration-200 ease-in-out ml-2 text-sm font-bold">
<HelpCircle className="inline h-4 w-4" />
</div>
</TooltipTrigger>
<TooltipContent className="w-96 bg-slate-700 text-slate-50 border-slate-600 text-md">
Timetable Division hasn't published the
compre dates for this course. Either there
is no compre exam, or they haven't decided
it yet. We recommend checking with your
professor.
</TooltipContent>
</Tooltip>
)}
</span>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/pages/cms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ const CMSDashboard = () => {
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
}[]
>([]);
const [allowEdit, setAllowEdit] = useState(true);
Expand Down
54 changes: 42 additions & 12 deletions src/pages/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ const Edit = () => {
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
}[]
>([]);
const lastUpdatedDate = useMemo(
Expand Down Expand Up @@ -181,12 +181,34 @@ const Edit = () => {
id: string;
code: string;
name: string;
midsemStartTime: string;
midsemEndTime: string;
compreStartTime: string;
compreEndTime: string;
midsemStartTime: string | null;
midsemEndTime: string | null;
compreStartTime: string | null;
compreEndTime: string | null;
clashing: null | string[];
};
if (e.midsemStartTime === null && e.compreStartTime === null) {
withClash.clashing = null;
return withClash;
} else if (e.midsemStartTime === null && e.compreStartTime !== null) {
const clashes = timetableDetails.examTimes.filter((x) => {
if (x.split("|")[0] === e.code) return false;
return x.includes(
withClash.compreStartTime + "|" + withClash.compreEndTime
);
});
withClash.clashing = clashes.length === 0 ? null : clashes;
return withClash;
} else if (e.midsemStartTime !== null && e.compreStartTime === null) {
const clashes = timetableDetails.examTimes.filter((x) => {
if (x.split("|")[0] === e.code) return false;
return x.includes(
withClash.midsemStartTime + "|" + withClash.midsemStartTime
);
});
withClash.clashing = clashes.length === 0 ? null : clashes;
return withClash;
}
const clashes = timetableDetails.examTimes.filter((x) => {
if (x.split("|")[0] === e.code) return false;
return (
Expand Down Expand Up @@ -377,6 +399,7 @@ const Edit = () => {
alert(`Server error: ${JSON.stringify(json)}`);
}
}, []);
const [userInfoCookie] = useCookies(["userInfo"]);

const fetchTimetableDetails = useCallback(async () => {
const res = await fetch(
Expand All @@ -393,16 +416,24 @@ const Edit = () => {
);
const json = await res.json();
if (res.status === 200) {
setIsLoaded(true);
setTimetableDetails(json);
if (
userInfoCookie["userInfo"] &&
json.authorId === userInfoCookie["userInfo"].id
) {
setIsLoaded(true);
setTimetableDetails(json);
} else {
alert("Error: You are not authorized to edit this timetable");
navigate(`/view/${id}`);
}
} else if (res.status === 404) {
alert(`Error: ${json.message}`);
} else if (res.status === 500) {
alert(`Server error: ${json.message}`);
} else {
alert(`Server error: ${JSON.stringify(json)}`);
}
}, [id]);
}, [id, navigate, userInfoCookie]);

const addCourseSection = useCallback(
async (sectionId: string) => {
Expand Down Expand Up @@ -666,7 +697,6 @@ const Edit = () => {
useEffect(() => {
fetchTimetableDetails();
}, [fetchTimetableDetails, id]);
const [userInfoCookie] = useCookies(["userInfo"]);

const deleteTimetable = async (id: string) => {
const res = await fetch(
Expand Down
7 changes: 4 additions & 3 deletions src/pages/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,20 @@ const View = () => {
}
);
const json = await res.json();
if (res.status === 200) {
if (res.status === 200 && json.draft === false) {
setIsLoaded(true);
setTimetableDetails(json);
} else if (res.status === 404) {
} else if (res.status === 404 || json.draft === true) {
alert(`Error: ${json.message}`);
navigate(`/`);
} else if (res.status === 500) {
alert(`Server error: ${json.message}`);
} else {
alert(`Server error: ${json}`);
}
};
fetchTimetableDetails();
}, [id]);
}, [id, navigate]);

const [userInfoCookie] = useCookies(["userInfo"]);

Expand Down

0 comments on commit 2ecab0f

Please sign in to comment.