Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into dev
  • Loading branch information
michaelkoelle committed May 13, 2021
2 parents b8f513c + 1406902 commit a761858
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
55 changes: 43 additions & 12 deletions app/features/schema-generator/SchemeGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
schemaSetClipboard,
schemaAddSimpleTask,
} from '../../model/SchemaSlice';
import { tasksUpsertMany } from '../../model/TaskSlice';
import { tasksRemoveMany, tasksUpsertMany } from '../../model/TaskSlice';
import { selectAllSheets, sheetsUpsertOne } from '../../model/SheetSlice';
import RateableTask from '../../model/RateableTask';
import CommentEntity from '../../model/CommentEntity';
Expand All @@ -59,6 +59,7 @@ import {
import { RatingsSchema, TasksSchema } from '../../model/NormalizationSchema';
import TaskEntity from '../../model/TaskEntity';
import {
flatMapTasksFromSheetEntity,
getTopLevelTasks,
hasTasksWithZeroMax,
isParentTaskEntity,
Expand All @@ -69,9 +70,12 @@ import Rating from '../../model/Rating';
import Task from '../../model/Task';
import ConfirmDialog from '../../components/ConfirmDialog';
import { correctionsUpsertMany } from '../../model/CorrectionsSlice';
import { commentsUpsertMany } from '../../model/CommentSlice';
import {
commentsRemoveOne,
commentsUpsertMany,
} from '../../model/CommentSlice';
import CorrectionEntity from '../../model/CorrectionEntity';
import { ratingsUpsertMany } from '../../model/RatingSlice';
import { ratingsRemoveOne, ratingsUpsertMany } from '../../model/RatingSlice';
import { save } from '../../utils/FileAccess';
import SingleChoiceTask from '../../model/SingleChoiceTask';
import SchemaTaskList from './SchemaTaskList';
Expand All @@ -90,24 +94,51 @@ function initializeSheet(
) {
return (dispatch, getState) => {
const state = getState();
const sheet: SheetEntity = state.sheets.entities[sheetId];
const tasksOfSheet: TaskEntity[] = flatMapTasksFromSheetEntity(
sheet,
state
);
const correctionsOfSheet = Object.values<CorrectionEntity>(
state.corrections.entities
).filter((c) => state.submissions.entities[c.submission].sheet === sheetId);

// Delete all unused tasks and tasks that were previously a parent task
dispatch(
tasksRemoveMany(
tasksOfSheet
.filter((t) => {
const updatedTask = tasks.find((tsk) => tsk.id === t.id);
// Unused tasks
const unusedTask = updatedTask === undefined;
// Tasks that were previously a parent task
const previouslyParent =
updatedTask !== undefined &&
isParentTaskEntity(t) &&
!isParentTaskEntity(updatedTask);
return unusedTask || previouslyParent;
})
.map((t) => t.id)
)
);

// Delete all unused ratings and comments
correctionsOfSheet.forEach((c) =>
c.ratings?.forEach((r) => {
const rating: RatingEntity = state.ratings.entities[r];
dispatch(ratingsRemoveOne(rating.id));
dispatch(commentsRemoveOne(rating.comment));
})
);

dispatch(tasksUpsertMany(tasks));

const sheet = state.sheets.entities[sheetId];
if (sheet) {
const temp = { ...sheet };
temp.tasks = topLevelTaskIds;
dispatch(sheetsUpsertOne(temp));
}

const correctionsOfSheet = Object.entries<CorrectionEntity>(
state.corrections.entities
)
.map(([, v]) => v)
.filter(
(c) => state.submissions.entities[c.submission].sheet === sheetId
);

const allComments: CommentEntity[] = [];
const allRatings: RatingEntity[] = [];

Expand Down
16 changes: 16 additions & 0 deletions app/utils/TaskUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ParentTask from '../model/ParentTask';
import ParentTaskEntity from '../model/ParentTaskEntity';
import RateableTask from '../model/RateableTask';
import SheetEntity from '../model/SheetEntity';
import SingleChoiceTask from '../model/SingleChoiceTask';
import Task from '../model/Task';
import TaskEntity from '../model/TaskEntity';
Expand Down Expand Up @@ -116,6 +117,21 @@ export function flatMapTaskEntity(
return list;
}

export function flatMapTasksFromSheetEntity(sheet: SheetEntity, state) {
const list: TaskEntity[] = [];
if (sheet.tasks) {
sheet.tasks.forEach((tId) => {
const t = state.tasks.entities[tId];
if (isParentTaskEntity(t)) {
list.push(...flatMapTaskEntity(t, state));
} else {
list.push(t);
}
});
}
return list;
}

export function getRateableTasksFromIds(
tasks: string[],
state,
Expand Down

0 comments on commit a761858

Please sign in to comment.