Skip to content

Commit

Permalink
Fixed assignment "already submitted" label
Browse files Browse the repository at this point in the history
- Fixed assignment "already submitted" label
- Moved assignment and page api requests to separate functions
- Added misc comments
- Updated manifest description
  • Loading branch information
da-stoi committed Nov 21, 2023
1 parent e12bd01 commit 6fc827e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 52 deletions.
4 changes: 2 additions & 2 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Canvas Strikethrough",
"description": "Mark assignments without deliverables as complete.",
"description": "Mark Canvas calendar events without deliverables as complete.",
"author": "Daniel Stoiber",
"version": "1.3",
"version": "1.4",
"manifest_version": 3,
"permissions": [
"storage"
Expand Down
73 changes: 23 additions & 50 deletions src/displayCheckButton.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getAssignment, getPage } from "./getTodos";
import { markCompleteOnclick, markIncompleteOnclick } from "./markTodos";
import { isTodoChecked } from "./storage";
import { Todo } from "./types";
Expand All @@ -21,6 +22,7 @@ function checkButtonHtml(courseId: number, todo: Todo, isChecked: boolean, isSub
return checkButton;
}

// Add general disclaimer
function assignmentDisclaimer(): HTMLSpanElement {
const disclaimer: HTMLSpanElement = document.createElement('span');
disclaimer.innerHTML = 'Note: Marking an assignment as complete is only for your own reference. It does not affect your submission/grade, nor does it notify your instructor.';
Expand All @@ -29,6 +31,7 @@ function assignmentDisclaimer(): HTMLSpanElement {
return disclaimer;
}

// Add disclaimer if assignment is already submitted
function gradedDisclaimer(): HTMLSpanElement {
const disclaimer: HTMLSpanElement = document.createElement('span');
disclaimer.innerHTML = 'Note: This assignment has already been submitted.';
Expand All @@ -40,27 +43,22 @@ function gradedDisclaimer(): HTMLSpanElement {

async function handleAssignment(courseId: number, assignmentId: number) {

// Get right side of page
const rightSide = document.querySelector('#right-side');
const buttonDiv = document.createElement('div');

if (!buttonDiv) {
return;
}

// Get assignment name
const assignmentReq = await fetch(`../../../api/v1/courses/${courseId}/assignments/${assignmentId}`);
const assignmentData = await assignmentReq.json();
// Get assignment
const assignmentData = await getAssignment(courseId, assignmentId);

const assignmentName = assignmentData.name;
const isSubmitted = assignmentData.has_submitted_submissions;
if (!assignmentData) {
return;
}

// Create todo object
const todo: Todo = {
type: 'assignment',
courseId,
id: assignmentId,
name: assignmentName
};
const { todo, isSubmitted } = assignmentData;

// Get check status
const isChecked = isTodoChecked(courseId, assignmentId);
Expand Down Expand Up @@ -97,22 +95,16 @@ async function handlePage(courseId: number, pageUrl: string) {
}

// Get page name
const pageReq = await fetch(`../../../api/v1/courses/${courseId}/pages/${pageUrl}`);
const pageData = await pageReq.json();

const pageName = pageData.title;
const pageId = pageData.page_id;

// Create todo object
const todo: Todo = {
type: 'page',
courseId,
id: pageId,
name: pageName
const pageData = await getPage(courseId, pageUrl);

if (!pageData) {
return;
}

const { todo } = pageData;

// Get check status
const isChecked = isTodoChecked(courseId, pageId);
const isChecked = isTodoChecked(courseId, todo.id);

// Create check button
const checkButton = checkButtonHtml(courseId, todo, isChecked, false);
Expand All @@ -129,37 +121,18 @@ async function handleEvent(courseId: number, todoId: number | string, eventType:

if (typeof todoId === 'string') {

const pageReq = await fetch(`../../api/v1/courses/${courseId}/pages/${todoId}`);
const pageData = await pageReq.json();
const pageData = await getPage(courseId, todoId);

const pageName = pageData.title;
const pageId = pageData.page_id;

// Create todo object
todo = {
type: 'page',
courseId,
id: pageId,
name: pageName
}
todo = pageData.todo;

// Get check status
isChecked = isTodoChecked(courseId, pageId);
isChecked = isTodoChecked(courseId, todo.id);
} else if (typeof todoId === 'number') {

const assignmentReq = await fetch(`../../api/v1/courses/${courseId}/assignments/${todoId}`);
const assignmentData = await assignmentReq.json();
const assignmentData = await getAssignment(courseId, todoId);

const assignmentName = assignmentData.name;
isSubmitted = assignmentData.has_submitted_submissions;

// Create todo object
todo = {
type: 'assignment',
courseId,
id: todoId,
name: assignmentName
};
isSubmitted = assignmentData.isSubmitted
todo = assignmentData.todo;

// Get check status
isChecked = isTodoChecked(courseId, todoId);
Expand Down
52 changes: 52 additions & 0 deletions src/getTodos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Todo } from "./types";

export async function getAssignment(courseId: number, assignmentId: number) {

// Get assignment data
const assignmentReq = await fetch(`/api/v1/courses/${courseId}/assignments/${assignmentId}`);
// Get submission data
const submissionReq = await fetch(`/api/v1/courses/${courseId}/students/submissions?assignment_ids[]=${assignmentId}`);

// Parse json
const assignmentData = await assignmentReq.json();
const submissionData = await submissionReq.json();

// Check if assignment is submitted
const isSubmitted = submissionData[0].workflow_state !== 'unsubmitted';

// Create todo object
const assignment: Todo = {
type: 'assignment',
courseId,
id: assignmentId,
name: assignmentData.name
};

return {
todo: assignment,
isSubmitted
};
}

export async function getPage(courseId: number, pageId: number | string) {

// Check if pageId is a number or a string
const pageIdType = typeof pageId === 'number' ? 'pageId' : 'pageUrl';

// Get page data
const pageReq = await fetch(`/api/v1/courses/${courseId}/pages/${pageId}`);
const pageData = await pageReq.json();

// Create todo object
const page: Todo = {
type: 'page',
courseId,
id: pageIdType === 'pageId' ? pageId : pageData.id,
name: pageData.title
};

return {
todo: page,
isSubmitted: false
}
}

0 comments on commit 6fc827e

Please sign in to comment.