Skip to content

Commit

Permalink
Merge pull request #56 from yashrajpahwa/main
Browse files Browse the repository at this point in the history
Added functionality to save course code with name locally
  • Loading branch information
utkarsh-1905 authored Aug 1, 2024
2 parents c2007c5 + 626c803 commit 9f6daca
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
defer dataFile.Close()
table, _ := template.ParseFiles("./templates/table.html")
home, _ := template.ParseFiles("./templates/home.html")
courseNameCode, _ := template.ParseFiles("./templates/course-name-code.html")
errorPage, _ := template.ParseFiles("./templates/error.html")

type HomeData struct {
Expand Down Expand Up @@ -96,6 +97,11 @@ func main() {
table.Execute(w, data)
})

// handler to serve add course page
http.HandleFunc("/course", func(w http.ResponseWriter, r *http.Request) {
courseNameCode.Execute(w, h)
})

fs := http.FileServer(http.Dir("assets/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))

Expand Down
78 changes: 78 additions & 0 deletions templates/course-name-code.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Add course name</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"
></script>
</head>
<body class="bg-dark">
<div
class="d-flex flex-column justify-content-center align-items-center vh-100"
>
<form id="courseForm">
<div class="form-group">
<label for="courseCode text-white"
><p class="text-white">Course Code:</p></label
>
<input
type="text"
class="form-control"
id="courseCode"
name="courseCode"
required
/>
</div>
<div class="form-group mt-2">
<label for="courseName text-white"
><p class="text-white">Course Name:</p></label
>
<input
type="text"
class="form-control"
id="courseName"
name="courseName"
required
/>
</div>
<button type="submit" class="btn btn-primary w-100 mt-2">Save</button>
</form>
<a href="/" class="text-center text-white mt-4 btn btn-outline-secondary"
>Go to home</a
>
</div>

<script>
document
.getElementById("courseForm")
.addEventListener("submit", function (event) {
event.preventDefault();
var courseCode = document.getElementById("courseCode").value;
var courseName = document.getElementById("courseName").value;
var course = {
code: courseCode.trim(),
name: courseName.trim(),
};
let courseLocal = localStorage.getItem("course");
courseArray = [];
if (courseLocal) {
courseArray = JSON.parse(courseLocal);
}
courseArray.push(course);
localStorage.setItem("course", JSON.stringify(courseArray));
alert("Course saved successfully!");
document.getElementById("courseForm").reset();
});
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ <h3 style="text-align: center; margin: 2rem 0" class="text-success">
Save Class
</button>
</div>
<a class="btn secondary text-light text-decoration-underline" href="/course">
Add course name
</a>
<!-- remove the alert later on -->
<div class="alert alert-success m-3" role="alert">Time Table for 2024 (ODD SEM) is updated now!!</div>
<div class="saved-urls container mb-4">
Expand Down
42 changes: 42 additions & 0 deletions templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,48 @@
<img src="https://contrib.rocks/image?repo=utkarsh-1905/time-table" />
</a>
</p>

<!-- dom manipulation logic start -->
<script>
document.addEventListener("DOMContentLoaded", function () {
var courseLocal = localStorage.getItem("course");
codeArray = [];
nameArray = [];
if (courseLocal) {
var courseArray = JSON.parse(courseLocal);
var codeArray = courseArray.map(function (course) {
return course.code;
});
var nameArray = courseArray.map(function (course) {
return course.name;
});
}

var tablemainElements = document.getElementsByClassName("tablemain");
var textArray = [];
for (var i = 0; i < tablemainElements.length; i++) {
var text = tablemainElements[i].textContent.trim();
var words = text.split(" ");
if (codeArray.some((code) => words.includes(code))) {
var indexes = [];
codeArray.forEach((code) => {
var index = words.findIndex((word) => word === code);
if (index !== -1) {
indexes.push(index);
}
});
indexes.forEach((index) => {
ci = codeArray.findIndex((code) => code === words[index]);
words[index] = words[index] + " (" + nameArray[ci] + ")";
});

tablemainElements[i].textContent = words.join(" ");
}
textArray.push(words);
}
});
</script>
<!-- dom manipulation logic end -->
</footer>
</body>
</html>

0 comments on commit 9f6daca

Please sign in to comment.