Skip to content

Commit

Permalink
Merge pull request #16 from Krishnansh5/main
Browse files Browse the repository at this point in the history
made final fixes except the template
  • Loading branch information
Krishnansh5 authored Apr 3, 2023
2 parents 267d744 + eff80d2 commit 8e43533
Show file tree
Hide file tree
Showing 16 changed files with 580 additions and 313 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ yarn-debug.log*
yarn-error.log*
yarn.lock
.next/
.clash-hai-bhai-key.json
clash-hai-bhai-key.json

backend/env/
9 changes: 5 additions & 4 deletions components/TopNavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function TopNavBar({
variant="h6"
noWrap
component="a"
href="/"
href={(process.env.NODE_ENV === 'production') ? ("clash-hai-bhai/"):("/")}
sx={{
mr: 2,
display: { xs: 'none', md: 'flex' },
Expand All @@ -32,14 +32,15 @@ export default function TopNavBar({
CLASH HAI BHAI!
</Typography>
<Stack justifyContent="space-between" direction="row">
<Stack spacing={2} direction={{ xs: 'column', sm: 'row' }}>
<Stack direction={{ xs: 'column', sm: 'row' }}>
{pages.map((page) => (
<Typography
noWrap
component="a"
href={page[0]}
href={(process.env.NODE_ENV === 'production') ? ("clash-hai-bhai/"+page[0]):(page[0])}
sx={{
mr: 2,
mt: 1,
mr: 3,
fontFamily: 'monospace',
fontWeight: 200,
letterSpacing: '.3rem',
Expand Down
9 changes: 9 additions & 0 deletions components/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { course_data } from "@js/course_data.js"
import { template_data } from '@js/template.js'

export const allDepts = Object.keys(course_data);

export const allTemplates = template_data;
export const allBranches: string[] = Object.keys(allTemplates);

export const semesters = ["1","2","3","4","5","6","7","8"];
6 changes: 6 additions & 0 deletions components/globalContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from "react";
import { globalContextType } from "./types";

export const GlobalContext = React.createContext<globalContextType | null>(null);

export default GlobalContext;
250 changes: 250 additions & 0 deletions components/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
import { SelectChangeEvent } from "@mui/material";
import { Dispatch, SetStateAction } from "react";
import { courses, semTemplate } from "./types";
import { allTemplates } from "./constants";
import { get_all_dept_eligible_courses, get_course_details, get_eligible_courses } from "@js/get_course_utils";
import { getCredits, removeLastChars } from "./utils";

const setTemplateWithoutLastChars = (template: string[], setTemplate: Dispatch<SetStateAction<string[]>>) => {
setTemplate(template.map((course) => {
return removeLastChars(course);
}))
}

const getTemplateRows = (template: string[]):courses[] => {
return template.reduce((filtered_courses: courses[], course: string) => {
const course_details = get_course_details(course);
if (course_details !== undefined) {
filtered_courses.push(course_details as courses);
}
return filtered_courses;
}, [])
}
// handler function called on change of branch dropdown
export const handleChangeBranch = (
{
event,
allSemTemplates,
sem,
deptToChooseCourseFrom,
setAllSemTemplates,
setTemplate,
setTemplateRows,
setBranch,
setTotalCredits,
setAllAvailableCourses
} : {
event: SelectChangeEvent,
allSemTemplates: semTemplate,
sem: string,
deptToChooseCourseFrom: string,
setAllSemTemplates: Dispatch<SetStateAction<semTemplate>>,
setTemplate: Dispatch<SetStateAction<string[]>>,
setTemplateRows: Dispatch<SetStateAction<courses[]>>,
setBranch: Dispatch<SetStateAction<string>>,
setTotalCredits: Dispatch<SetStateAction<number>>,
setAllAvailableCourses: Dispatch<SetStateAction<courses[]>>
}) => {
let allSemTemplatesCopy: semTemplate = allSemTemplates;
setBranch(event.target.value as string);

//iterate through all templates and find the branch template
for (const key in allTemplates) {
if (key === event.target.value) {
setAllSemTemplates(allTemplates[key as keyof typeof allTemplates]);
allSemTemplatesCopy = allTemplates[key as keyof typeof allTemplates];
}
}

//if sem is not empty, set template and template rows
if (sem !== "") {
for (const key in allSemTemplatesCopy) {
if (key === sem) {
const newTemplate = allSemTemplatesCopy[key as keyof typeof allSemTemplatesCopy];
setTemplateWithoutLastChars(newTemplate,setTemplate);
const tempObject: courses[] = getTemplateRows(newTemplate);
setTemplateRows(tempObject);
let credits = 0;
tempObject.forEach((course) => {
credits += getCredits(course.credits);
})
setTotalCredits(credits);
if (deptToChooseCourseFrom === "") {
const elegible_courses = get_all_dept_eligible_courses(newTemplate);
setAllAvailableCourses(elegible_courses);
}
else if (deptToChooseCourseFrom !== "") {
const elegible_courses = get_eligible_courses(newTemplate, deptToChooseCourseFrom);
setAllAvailableCourses(elegible_courses);
}
}
}
}
};

export const handleChangeSem = (
{
event,
allSemTemplates,
deptToChooseCourseFrom,
setSem,
setTemplate,
setTemplateRows,
setTotalCredits,
setAllAvailableCourses
}:{
event: SelectChangeEvent,
allSemTemplates: semTemplate,
deptToChooseCourseFrom: string,
setSem: Dispatch<SetStateAction<string>>,
setTemplate: Dispatch<SetStateAction<string[]>>,
setTemplateRows: Dispatch<SetStateAction<courses[]>>,
setTotalCredits: Dispatch<SetStateAction<number>>,
setAllAvailableCourses: Dispatch<SetStateAction<courses[]>>
}) => {
setSem(event.target.value as string);
for (const key in allSemTemplates) {
if (key === event.target.value) {
const newTemplate = allSemTemplates[key as keyof typeof allSemTemplates];
setTemplateWithoutLastChars(newTemplate,setTemplate);
const tempObject: courses[] = getTemplateRows(allSemTemplates[key as keyof typeof allSemTemplates]);
setTemplateRows(tempObject);
let credits = 0;
tempObject.forEach((course) => {
credits += getCredits(course.credits);
})
setTotalCredits(credits);
if (deptToChooseCourseFrom === "") {
const elegible_courses = get_all_dept_eligible_courses(newTemplate);
setAllAvailableCourses(elegible_courses);
}
if (deptToChooseCourseFrom !== "") {
const elegible_courses = get_eligible_courses(newTemplate, deptToChooseCourseFrom);
setAllAvailableCourses(elegible_courses);
}
}
}
};

export const handleChangeDepttoChooseFrom = (
{
event,
template,
setDeptToChooseCourseFrom,
setAllAvailableCourses,
}:{
event: SelectChangeEvent,
template: string[],
setDeptToChooseCourseFrom: Dispatch<SetStateAction<string>>,
setAllAvailableCourses: Dispatch<SetStateAction<courses[]>>
}) => {
setDeptToChooseCourseFrom(event.target.value as string);
if (event.target.value === "") {
const elegible_courses = get_all_dept_eligible_courses(template);
setAllAvailableCourses(elegible_courses);
}
else {
const elegible_courses = get_eligible_courses(template, event.target.value);
setAllAvailableCourses(elegible_courses);
}
}

export const handleCourseDrop = (
{
dropCourse,
template,
deptToChooseCourseFrom,
setAllAvailableCourses,
setTemplate,
setTemplateRows,
setTotalCredits,
}:{
dropCourse: string,
template: string[],
deptToChooseCourseFrom: string,
setAllAvailableCourses: Dispatch<SetStateAction<courses[]>>,
setTemplate: Dispatch<SetStateAction<string[]>>,
setTemplateRows: Dispatch<SetStateAction<courses[]>>,
setTotalCredits: Dispatch<SetStateAction<number>>
}) => {
setAllAvailableCourses([]);
const templateFiltered = template.filter((value) => (removeLastChars(value) !== removeLastChars(dropCourse)));
setTemplateWithoutLastChars(templateFiltered,setTemplate);
const tempObject: courses[] = getTemplateRows(templateFiltered);
setTemplateRows(tempObject);
let credits = 0;
tempObject.forEach((course) => {
credits += getCredits(course.credits);
})
setTotalCredits(credits);
if (deptToChooseCourseFrom === "") {
const elegible_courses = get_all_dept_eligible_courses(templateFiltered);
setAllAvailableCourses(elegible_courses);
}
else {
const elegible_courses = get_eligible_courses(templateFiltered, deptToChooseCourseFrom);
setAllAvailableCourses(elegible_courses);
}
}

export const handleCourseAdd = (
{
addCourse,
template,
deptToChooseCourseFrom,
setTemplate,
setTemplateRows,
setTotalCredits,
setAllAvailableCourses,
}:{
template: string[],
addCourse: string,
deptToChooseCourseFrom: string,
setTemplate: Dispatch<SetStateAction<string[]>>,
setTemplateRows: Dispatch<SetStateAction<courses[]>>,
setTotalCredits: Dispatch<SetStateAction<number>>,
setAllAvailableCourses: Dispatch<SetStateAction<courses[]>>,
}) => {
const newTemplate: string[] = template.concat([addCourse]);
const tempObject: courses[] = getTemplateRows(newTemplate);
setTemplateWithoutLastChars(newTemplate,setTemplate);
setTemplateRows(tempObject);
let credits = 0;
tempObject.forEach((course) => {
credits += getCredits(course.credits);
})
setTotalCredits(credits);
if (deptToChooseCourseFrom === "") {
const elegible_courses = get_all_dept_eligible_courses(newTemplate);
setAllAvailableCourses(elegible_courses);
}
else {
const elegible_courses = get_eligible_courses(newTemplate, deptToChooseCourseFrom);
setAllAvailableCourses(elegible_courses);
}
}

export const handleCustomTemplate = (
{
setBranch,
setSem,
setTemplate,
setTemplateRows,
setTotalCredits,
setAllAvailableCourses,
}:{
setBranch: Dispatch<React.SetStateAction<string>>,
setSem: Dispatch<React.SetStateAction<string>>,
setTemplate: Dispatch<SetStateAction<string[]>>,
setTemplateRows: Dispatch<SetStateAction<courses[]>>,
setTotalCredits: Dispatch<SetStateAction<number>>,
setAllAvailableCourses: Dispatch<SetStateAction<courses[]>>
}) => {
setTemplate([]);
setTemplateRows([] as courses[]);
setTotalCredits(0);
setBranch("")
setSem("")
const elegible_courses = get_all_dept_eligible_courses([]);
setAllAvailableCourses(elegible_courses);
}
79 changes: 79 additions & 0 deletions components/options.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Button, InputLabel, MenuItem, Select, Stack, Typography } from "@mui/material"
import { semesters, allDepts, allBranches } from "@components/constants"
import { handleChangeBranch, handleChangeSem, handleChangeDepttoChooseFrom, handleCustomTemplate } from "./handlers"
import React from "react";
import GlobalContext from "./globalContext";
import { courses, globalContextType } from "./types";

export default function Options() {
const { branch,sem,deptToChooseCourseFrom,template,templateRows,allAvailableCourses,totalCredits,allSemTemplates,setBranch,setSem,setDeptToChooseCourseFrom,setTemplate,setTemplateRows,setAllAvailableCourses,setTotalCredits,setAllSemTemplates } = React.useContext(GlobalContext) as globalContextType;
return (
<Stack
direction={{ xs: 'column', sm: 'row' }}
spacing={{ xs: 1, sm: 2, md: 4 }}
justifyContent = "space-between"
padding={4}
>
<Stack spacing={1}>
<Stack direction={{xs: 'column', sm: 'row'}} spacing={2}>
<Stack>
<InputLabel id="branch-select">Select Branch</InputLabel>
<Select
labelId = "branch-select"
id="branch-select"
value={branch}
label="Select Branch"
onChange={(event) => {
if(event.target.value.length==0)return
return handleChangeBranch({ event, allSemTemplates, sem, deptToChooseCourseFrom, setAllSemTemplates, setTemplate, setTemplateRows, setBranch, setTotalCredits, setAllAvailableCourses });
}}
>
{
(allBranches.map((branch,index) => {
return (<MenuItem key={index} value={branch}>{branch}</MenuItem>)
}))
}
<MenuItem value="" onClick={() => {handleCustomTemplate({setBranch,setSem,setTemplate,setTemplateRows,setTotalCredits,setAllAvailableCourses});}}>others</MenuItem>
</Select>
</Stack>
<Stack>
<InputLabel id="sem-select">Select Semester</InputLabel>
<Select
labelId = "sem-select"
id="sem-select"
value={sem}
label="Select Semester"
onChange={(event) => handleChangeSem({event, allSemTemplates, deptToChooseCourseFrom, setTemplate, setTemplateRows, setSem, setTotalCredits, setAllAvailableCourses})}
>
{
(branch!=="" && (semesters.map((sem,index) => {
return (<MenuItem key={index} value={sem}>{sem}</MenuItem>)
})))
}
</Select>
</Stack>
</Stack>
<Typography><b>Total Credits = {totalCredits}</b></Typography>
</Stack>
<Stack direction={{xs: 'column', sm: 'row'}} spacing={2} alignItems="center">
<Stack>
<InputLabel id="dept-select ">Dept to choose courses from</InputLabel>
<Select
labelId = "dept-select"
id="dept-select"
value={deptToChooseCourseFrom}
label="Select Branch"
onChange={(event) => handleChangeDepttoChooseFrom({event, template, setDeptToChooseCourseFrom, setAllAvailableCourses})}
>
<MenuItem key={0} value="">All Departments</MenuItem>
{
(allDepts.map((dept,index) => {
return (<MenuItem key={index} value={dept}>{dept}</MenuItem>)
}))
}
</Select>
</Stack>
</Stack>
</Stack>
)
}
Loading

0 comments on commit 8e43533

Please sign in to comment.