Skip to content

Commit

Permalink
Merge pull request #2992 from ilandikov/refactor-simplify-handleNewSt…
Browse files Browse the repository at this point in the history
…atus

refactor: simplify `Task.handleNewStatus()`
  • Loading branch information
claremacrae authored Jul 30, 2024
2 parents 2d48cf7 + ce5e72f commit fa3236e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/Task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,31 +361,34 @@ export class Task extends ListItem {
today,
);

let nextOccurrence: Occurrence | null = null;
if (newStatus.isCompleted()) {
if (!this.status.isCompleted() && this.recurrence !== null) {
nextOccurrence = this.recurrence.next(today);
}
}

const toggledTask = new Task({
...this,
status: newStatus,
doneDate: newDoneDate,
cancelledDate: newCancelledDate,
});

const newTasks: Task[] = [];
if (!newStatus.isCompleted()) {
return [toggledTask];
}

if (nextOccurrence !== null) {
const nextTask = this.createNextOccurrence(newStatus, nextOccurrence);
newTasks.push(nextTask);
if (this.status.isCompleted()) {
return [toggledTask];
}

// Write next occurrence before previous occurrence.
newTasks.push(toggledTask);
const recurrence = this.recurrence;
if (recurrence === null) {
return [toggledTask];
}

return newTasks;
const nextOccurrence = recurrence.next(today);
if (nextOccurrence === null) {
return [toggledTask];
}

const nextTask = this.createNextOccurrence(newStatus, nextOccurrence);
// Write next occurrence before previous occurrence.
return [nextTask, toggledTask];
}

/**
Expand Down

0 comments on commit fa3236e

Please sign in to comment.