Skip to content

Commit bf0cc3c

Browse files
committed
Alter the assignment count so that it only shows the number of recent assignments to do
1 parent f179d47 commit bf0cc3c

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed

src/app/components/navigation/NavigationBar.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {useEffect, useState} from "react";
22
import {Link} from "react-router-dom";
33
import {useDispatch, useSelector} from "react-redux";
4-
import {AppState} from "../../state/reducers";
4+
import {AppState, assignments} from "../../state/reducers";
55
import {
66
Badge,
77
Collapse,
@@ -15,12 +15,20 @@ import {
1515
} from "reactstrap";
1616
import {isAdmin, isStaff} from "../../services/user";
1717
import {loadMyAssignments} from "../../state/actions";
18+
import {filterAssignmentsByStatus} from "../../services/assignments";
1819

1920
export const NavigationBar = () => {
2021
const dispatch = useDispatch();
2122
const [menuOpen, setMenuOpen] = useState(false);
2223
const user = useSelector((state: AppState) => (state && state.user) || null);
23-
const assignmentCount = useSelector((state: AppState) => (state && state.assignments && state.assignments.length) || 0);
24+
const assignmentCount = useSelector((state: AppState) => {
25+
if (state && state.assignments) {
26+
const {inProgressRecent} = filterAssignmentsByStatus(state.assignments);
27+
return inProgressRecent.length;
28+
} else {
29+
return 0;
30+
}
31+
});
2432

2533
useEffect(() => {
2634
if (user && user.loggedIn) {

src/app/components/pages/MyAssignments.tsx

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {ShowLoading} from "../handlers/ShowLoading";
66
import {AppState} from "../../state/reducers";
77
import {AssignmentDTO} from "../../../IsaacApiTypes";
88
import {Card, CardBody, Container, Row, Col, Nav, NavItem, NavLink} from 'reactstrap';
9-
import {orderBy} from "lodash";
109
import {TitleAndBreadcrumb} from "../elements/TitleAndBreadcrumb";
1110
import {extractTeacherName} from "../../services/user";
1211
import {DATE_FORMATTER, STUDENTS_CRUMB} from "../../services/constants";
12+
import {filterAssignmentsByStatus} from "../../services/assignments";
1313

1414
const stateToProps = (state: AppState) => (state && {assignments: state.assignments});
1515
const dispatchToProps = {loadMyAssignments, logAction};
@@ -76,49 +76,13 @@ const Assignments = ({assignments, showOld}: {assignments: AssignmentDTO[]; show
7676
</ShowLoading>;
7777
};
7878

79-
function notMissing<T>(item: T | undefined): T {
80-
if (item === undefined) throw new Error("Missing item");
81-
return item;
82-
}
79+
8380

8481
const MyAssignmentsPageComponent = ({assignments, loadMyAssignments, logAction}: MyAssignmentsPageProps) => {
8582
useEffect(() => {loadMyAssignments();}, []);
8683
useEffect(() => {logAction({type: "VIEW_MY_ASSIGNMENTS"})}, []);
8784

88-
const now = new Date();
89-
const fourWeeksAgo = new Date(now.valueOf() - (4 * 7 * 24 * 60 * 60 * 1000));
90-
// Midnight five days ago:
91-
const fiveDaysAgo = new Date(now);
92-
fiveDaysAgo.setDate(now.getDate() - 5);
93-
fiveDaysAgo.setHours(0, 0, 0, 0);
94-
95-
const myAssignments: {inProgressRecent: AssignmentDTO[]; inProgressOld: AssignmentDTO[]; completed: AssignmentDTO[]} = {
96-
inProgressRecent: [],
97-
inProgressOld: [],
98-
completed: []
99-
};
100-
101-
if (assignments) {
102-
assignments.forEach(assignment => {
103-
assignment.gameboard = notMissing(assignment.gameboard);
104-
assignment.creationDate = notMissing(assignment.creationDate);
105-
if (assignment.gameboard.percentageCompleted === undefined || assignment.gameboard.percentageCompleted < 100) {
106-
let noDueDateButRecent = !assignment.dueDate && (assignment.creationDate > fourWeeksAgo);
107-
let dueDateAndCurrent = assignment.dueDate && (assignment.dueDate >= fiveDaysAgo);
108-
if (noDueDateButRecent || dueDateAndCurrent) {
109-
// Assignment either not/only just overdue, or else set within last month but no due date.
110-
myAssignments.inProgressRecent.push(assignment);
111-
} else {
112-
myAssignments.inProgressOld.push(assignment);
113-
}
114-
} else {
115-
myAssignments.completed.push(assignment);
116-
}
117-
});
118-
myAssignments.inProgressRecent = orderBy(myAssignments.inProgressRecent, ["dueDate", "creationDate"], ["asc", "desc"]);
119-
myAssignments.inProgressOld = orderBy(myAssignments.inProgressOld, ["dueDate", "creationDate"], ["asc", "desc"]);
120-
myAssignments.completed = orderBy(myAssignments.completed, ["creationDate"], ["desc"]);
121-
}
85+
const myAssignments = filterAssignmentsByStatus(assignments)
12286

12387
const [activeTab, setActiveTab] = useState(0);
12488

src/app/services/assignments.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {AssignmentDTO} from "../../IsaacApiTypes";
2+
import {orderBy} from "lodash";
3+
4+
function notMissing<T>(item: T | undefined): T {
5+
if (item === undefined) throw new Error("Missing item");
6+
return item;
7+
}
8+
export const filterAssignmentsByStatus = (assignments: AssignmentDTO[] | null) => {
9+
const now = new Date();
10+
const fourWeeksAgo = new Date(now.valueOf() - (4 * 7 * 24 * 60 * 60 * 1000));
11+
// Midnight five days ago:
12+
const fiveDaysAgo = new Date(now);
13+
fiveDaysAgo.setDate(now.getDate() - 5);
14+
fiveDaysAgo.setHours(0, 0, 0, 0);
15+
16+
const myAssignments: {inProgressRecent: AssignmentDTO[]; inProgressOld: AssignmentDTO[]; completed: AssignmentDTO[]} = {
17+
inProgressRecent: [],
18+
inProgressOld: [],
19+
completed: []
20+
};
21+
22+
if (assignments) {
23+
assignments.forEach(assignment => {
24+
assignment.gameboard = notMissing(assignment.gameboard);
25+
assignment.creationDate = notMissing(assignment.creationDate);
26+
if (assignment.gameboard.percentageCompleted === undefined || assignment.gameboard.percentageCompleted < 100) {
27+
let noDueDateButRecent = !assignment.dueDate && (assignment.creationDate > fourWeeksAgo);
28+
let dueDateAndCurrent = assignment.dueDate && (assignment.dueDate >= fiveDaysAgo);
29+
if (noDueDateButRecent || dueDateAndCurrent) {
30+
// Assignment either not/only just overdue, or else set within last month but no due date.
31+
myAssignments.inProgressRecent.push(assignment);
32+
} else {
33+
myAssignments.inProgressOld.push(assignment);
34+
}
35+
} else {
36+
myAssignments.completed.push(assignment);
37+
}
38+
});
39+
myAssignments.inProgressRecent = orderBy(myAssignments.inProgressRecent, ["dueDate", "creationDate"], ["asc", "desc"]);
40+
myAssignments.inProgressOld = orderBy(myAssignments.inProgressOld, ["dueDate", "creationDate"], ["asc", "desc"]);
41+
myAssignments.completed = orderBy(myAssignments.completed, ["creationDate"], ["desc"]);
42+
}
43+
44+
return myAssignments;
45+
};

0 commit comments

Comments
 (0)