Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default duration not always explicitly added to task #424

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/ui/hooks/use-edit/use-edit-actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { get, Readable, Writable } from "svelte/store";

import { OnUpdateFn, Tasks } from "../../../types";
import { areValuesEmpty } from "../../../util/task-utils";
import {
adjustZeroDurationTask,
areValuesEmpty,
} from "../../../util/task-utils";
import { getDiff, updateText } from "../../../util/tasks-utils";

import { EditOperation } from "./types";
Expand Down Expand Up @@ -34,11 +37,15 @@ export function useEditActions({

const currentTasks = get(displayedTasks);

// call adjustZeroDurationTask to ensure that tasks with 0 duration are adjusted
// to the default duration
const currentTasksAdjusted = adjustZeroDurationTask(currentTasks);

editOperation.set(undefined);

// todo: diffing can be moved outside to separate concerns
// but we need to know if something changed to not cause extra rewrites?
const diff = getDiff(get(baselineTasks), currentTasks);
const diff = getDiff(get(baselineTasks), currentTasksAdjusted);

if (areValuesEmpty(diff)) {
return;
Expand Down
17 changes: 16 additions & 1 deletion src/util/task-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
scheduledPropRegExp,
shortScheduledPropRegExp,
} from "../regexp";
import type { Task } from "../types";
import type { Task, Tasks } from "../types";
import { PlacedTask } from "../types";

import { getId } from "./id";
Expand Down Expand Up @@ -72,6 +72,21 @@ export function areValuesEmpty(record: Record<string, [] | object>) {
return Object.values(record).every(isEmpty);
}

export function adjustZeroDurationTask(tasks: Tasks) {
return Object.fromEntries(
Object.entries(tasks).map(([key, tasks]) => [
key,
{
withTime: tasks.withTime.map((task) => ({
...task,
durationMinutes: task.durationMinutes || defaultDurationMinutes,
})),
noTime: tasks.noTime,
},
]),
);
}

function taskLineToString(task: Task) {
return `${task.listTokens}${createTimestamp(
task.startMinutes,
Expand Down