Skip to content

Commit 90cddfd

Browse files
committed
course: refactor add assignment code to be easier to understand
- in prep for refactoring the component - and there's just some really annoying "too clever" code in here that j3 originally wrote and is more efficient but much harder to work with.
1 parent f390836 commit 90cddfd

File tree

2 files changed

+74
-61
lines changed

2 files changed

+74
-61
lines changed

src/packages/frontend/course/assignments/actions.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,43 @@ export class AssignmentsActions {
9191
return store.get("course_filename").slice(0, i) + "-collect/" + path;
9292
}
9393

94-
public async add_assignment(path: string): Promise<void> {
95-
// Add an assignment to the course, which is defined by giving a directory in the project.
96-
// Where we collect homework that students have done (in teacher project)
94+
// slight warning -- this is linear in the number of assignments (so do not overuse)
95+
// Returns
96+
private getAssignmentWithPath = (
97+
path: string,
98+
): AssignmentRecord | undefined => {
99+
const store = this.get_store();
100+
if (store == null) return;
101+
return store
102+
.get("assignments")
103+
.valueSeq()
104+
.filter((x) => x.get("path") == path)
105+
.get(0);
106+
};
107+
108+
addAssignment = async (path: string | string[]): Promise<void> => {
109+
// Add one or more assignment to the course, which is defined by giving a directory in the project.
110+
// Where we collect homework that students have done (in teacher project).
111+
// If the assignment was previously deleted, this undeletes it.
112+
if (typeof path != "string") {
113+
// handle case of array of inputs
114+
for (const p of path) {
115+
await this.addAssignment(p);
116+
}
117+
return;
118+
}
119+
const cur = this.getAssignmentWithPath(path);
120+
if (cur != null) {
121+
// either undelete or nothing to do.
122+
if (cur.get("deleted")) {
123+
// undelete
124+
this.undelete_assignment(cur.get("assignment_id"));
125+
} else {
126+
// nothing to do
127+
}
128+
return;
129+
}
130+
97131
const collect_path = this.collect_path(path);
98132
const path_parts = path_split(path);
99133
// folder that we return graded homework to (in student project)
@@ -122,7 +156,7 @@ export class AssignmentsActions {
122156
table: "assignments",
123157
assignment_id: uuid(),
124158
});
125-
}
159+
};
126160

127161
public delete_assignment(assignment_id: string): void {
128162
this.course_actions.set({

src/packages/frontend/course/assignments/assignments-panel.tsx

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -86,48 +86,45 @@ export const AssignmentsPanel: React.FC<Props> = React.memo((props: Props) => {
8686
return assignment as any;
8787
}
8888

89-
const { shown_assignments, deleted_assignments, num_omitted, num_deleted } =
90-
useMemo((): {
91-
shown_assignments: any[];
92-
deleted_assignments: any[];
93-
num_omitted: number;
94-
num_deleted: number;
95-
} => {
96-
let deleted, f, num_deleted, num_omitted;
97-
let list = util.immutable_to_list(assignments, "assignment_id");
89+
const { shown_assignments, num_omitted, num_deleted } = useMemo((): {
90+
shown_assignments: any[];
91+
num_omitted: number;
92+
num_deleted: number;
93+
} => {
94+
let f, num_deleted, num_omitted;
95+
let list = util.immutable_to_list(assignments, "assignment_id");
9896

99-
({ list, num_omitted } = util.compute_match_list({
100-
list,
101-
search_key: "path",
102-
search: search.trim(),
103-
}));
97+
({ list, num_omitted } = util.compute_match_list({
98+
list,
99+
search_key: "path",
100+
search: search.trim(),
101+
}));
104102

105-
if (active_assignment_sort.get("column_name") === "due_date") {
106-
f = (a) => [
107-
a.due_date != null ? a.due_date : 0,
108-
a.path != null ? a.path.toLowerCase() : undefined,
109-
];
110-
} else if (active_assignment_sort.get("column_name") === "dir_name") {
111-
f = (a) => [
112-
a.path != null ? a.path.toLowerCase() : undefined,
113-
a.due_date != null ? a.due_date : 0,
114-
];
115-
}
103+
if (active_assignment_sort.get("column_name") === "due_date") {
104+
f = (a) => [
105+
a.due_date != null ? a.due_date : 0,
106+
a.path != null ? a.path.toLowerCase() : undefined,
107+
];
108+
} else if (active_assignment_sort.get("column_name") === "dir_name") {
109+
f = (a) => [
110+
a.path != null ? a.path.toLowerCase() : undefined,
111+
a.due_date != null ? a.due_date : 0,
112+
];
113+
}
116114

117-
({ list, deleted, num_deleted } = util.order_list({
118-
list,
119-
compare_function: (a, b) => cmp_array(f(a), f(b)),
120-
reverse: active_assignment_sort.get("is_descending"),
121-
include_deleted: show_deleted,
122-
}));
115+
({ list, num_deleted } = util.order_list({
116+
list,
117+
compare_function: (a, b) => cmp_array(f(a), f(b)),
118+
reverse: active_assignment_sort.get("is_descending"),
119+
include_deleted: show_deleted,
120+
}));
123121

124-
return {
125-
shown_assignments: list,
126-
deleted_assignments: deleted,
127-
num_omitted,
128-
num_deleted,
129-
};
130-
}, [assignments, active_assignment_sort, show_deleted, search]);
122+
return {
123+
shown_assignments: list,
124+
num_omitted,
125+
num_deleted,
126+
};
127+
}, [assignments, active_assignment_sort, show_deleted, search]);
131128

132129
function render_sort_link(
133130
column_name: string,
@@ -277,25 +274,7 @@ export const AssignmentsPanel: React.FC<Props> = React.memo((props: Props) => {
277274
}
278275
}
279276

280-
function yield_adder(deleted_assignments): (string) => void {
281-
const deleted_paths = {};
282-
deleted_assignments.map((obj) => {
283-
if (obj.path) {
284-
deleted_paths[obj.path] = obj.assignment_id;
285-
}
286-
});
287-
288-
return (path) => {
289-
if (deleted_paths[path] != null) {
290-
course_actions.assignments.undelete_assignment(deleted_paths[path]);
291-
} else {
292-
course_actions.assignments.add_assignment(path);
293-
}
294-
};
295-
}
296-
297277
function header() {
298-
const add_assignment = yield_adder(deleted_assignments);
299278
return (
300279
<div style={{ marginBottom: "15px" }}>
301280
<FoldersToolbar
@@ -304,7 +283,7 @@ export const AssignmentsPanel: React.FC<Props> = React.memo((props: Props) => {
304283
num_omitted={num_omitted}
305284
project_id={project_id}
306285
items={assignments}
307-
add_folders={(paths) => paths.map(add_assignment)}
286+
add_folders={course_actions.assignments.addAssignment}
308287
item_name={"assignment"}
309288
plural_item_name={"assignments"}
310289
/>

0 commit comments

Comments
 (0)