Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress meter #19

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/assets/js/progressBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const innerMeter = document.querySelector('.meter-inner');
const checkboxes = document.querySelectorAll('.watched');
let completed = Number(innerMeter.innerText);

if (checkboxes) {
for (const checkbox of checkboxes) {
checkbox.addEventListener('change', updateProgress);
}
}

function updateWidth() {
innerMeter.style.width = `${completed / checkboxes.length * 100}%`;
innerMeter.innerText = `${completed} / ${checkboxes.length} `;
if (completed === checkboxes.length) {
innerMeter.innerText = "You are a software engineer.";
}
}

function updateProgress(e) {
if (e.target.checked) {
completed += 1;
} else {
completed -= 1;
}
updateWidth();
}

// Set the progress bar width on page load
updateWidth();
4 changes: 2 additions & 2 deletions src/controllers/homework.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const addEditHomework = async (req, res) => {
class: hwClass[i],
due: req.body.due,
description: hwDesc[i],
required: hwRequired[i] === "true" ? true : false,
required: hwRequired[i] === "true",
};
const hwItem = await HomeworkItem.findByIdAndUpdate(hwId[i] || mongoose.Types.ObjectId(), item, {upsert: true, new: true});
items.push(hwItem._id);
Expand Down Expand Up @@ -113,7 +113,7 @@ export const showHomework = async (req, res) => {
};

export const importData = async (req, res) => {
if (!req.isAuthenticated()) return notLoggedIn(req, res);;
if (!req.isAuthenticated()) return notLoggedIn(req, res);
try {
const data = JSON.parse(JSON.parse(req.body.import).CBState);
const submitData = [];
Expand Down
8 changes: 7 additions & 1 deletion src/controllers/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export const dashboard = async (req, res) => {
currentLesson = await Lesson.findById(req.user.currentClass);
currentLesson = await getLessonProgress(req.user.id, currentLesson);
}
res.render("dashboard", { lesson: currentLesson });

const totalCount = await LessonProgress.countDocuments();
Copy link
Author

@7MinutesDead-Git 7MinutesDead-Git Feb 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For totalCount, if traffic to your site ever starts really picking up, it may then be best to do this query only once on server startup, and keep its value cached in memory for subsequent requests for all users as it's unlikely the total lessons will change often (if ever). For now though this works for simplicity's sake.

const completedCount = await LessonProgress.countDocuments({
user: req.user.id,
watched: true,
});
res.render("dashboard", { lesson: currentLesson, completedCount, totalCount });
};

export const account = (req, res) => {
Expand Down
8 changes: 8 additions & 0 deletions src/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@
@apply shadow-[0_2px_10px_0_rgba(0,0,0,0.1)];
}

/*** PROGRESS METER ***/
.meter-inner {
@apply transition-all duration-500 ease-in-out;
}
.meter {
@apply overflow-hidden;
}

/********* temporary ************/
#video {
max-width: 960px;
Expand Down
3 changes: 3 additions & 0 deletions src/views/allLessons.pug
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extends layouts/default.pug
include ./mixins/lessonCard.pug
include ./mixins/progressBar.pug

block variables
- title = "Classes"
Expand All @@ -11,6 +12,7 @@ block content

h1.mb-4 All Classes

+progressBar
#lessons.grid.gap-y-12.gap-x-16(class="sm:gap-y-24 grid-cols-[repeat(auto-fill,_minmax(285px,_1fr))]")
each lesson in lessons
+lessonCard(lesson)
Expand All @@ -21,3 +23,4 @@ append scripts
if loggedIn
script(src="/js/lessonProgress.js")
script(src="/js/lessonDone.js")
script(src="/js/progressBar.js")
6 changes: 6 additions & 0 deletions src/views/mixins/progressBar.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mixin progressBar(completedCount, totalCount)
div.meter.w-full.h-8.rounded.mb-6.bg-twilight-50.border
if totalCount
div.meter-inner.text-center.text-lg.font-medium.h-8.text-twilight-50.bg-pink-800(style=`width: ${completedCount / totalCount * 100}%`) #{completedCount} / #{totalCount}
else
div.meter-inner.text-center.text-lg.font-medium.h-8.text-twilight-50.bg-pink-800 #{completedCount}