Skip to content

Commit

Permalink
use toggleComplete from TaskCheckbox.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Nov 20, 2024
1 parent a963292 commit 7d20443
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 43 deletions.
22 changes: 2 additions & 20 deletions src/components/TaskCheckbox.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi } from 'vitest';
import { updateTask } from '@taskratchet/sdk';
import { toggleComplete } from './TaskCheckbox';

vi.mock('@taskratchet/sdk', () => ({
updateTask: vi.fn(),
Expand All @@ -20,27 +21,8 @@ describe('TaskCheckbox', () => {
vi.setSystemTime(new Date('2023-12-01'));
const event = new Event('change');

await toggleComplete(event, task);
await toggleComplete({ event, task, isConfirming: false });

expect(updateTask).toHaveBeenCalledWith('123', { complete: true });
});
});

async function toggleComplete(event: Event, task: TaskType) {
if (!task.id) return;

const taskDue = new Date(task.due);
const now = new Date();
const isPastDue = taskDue < now;

if (isPastDue && task.complete) {
const confirmed = confirm(
'This task is past due. Marking it incomplete will require contacting support to undo. Continue?'
);
if (!confirmed) return;
}

await updateTask(task.id, { complete: !task.complete });
task.complete = !task.complete;
task.status = task.complete ? 'complete' : 'pending';
}
27 changes: 4 additions & 23 deletions src/components/TaskCheckbox.svelte
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
<script lang="ts">
import { updateTask } from '@taskratchet/sdk';
import { toggleComplete } from './TaskCheckbox';
export let task: TaskType;
export let disabled = false;
let isConfirming = false;
async function toggleComplete(event: Event) {
if (!task.id) return;
const checkbox = event.target as HTMLInputElement;
const taskDue = new Date(task.due);
const now = new Date();
const isPastDue = taskDue < now;
if (isPastDue && task.complete && !isConfirming) {
checkbox.checked = true;
const confirmed = confirm(
'This task is past due. Marking it incomplete will require contacting support to undo. Continue?'
);
if (!confirmed) return;
isConfirming = true;
}
await updateTask(task.id, { complete: !task.complete });
task.complete = !task.complete;
task.status = task.complete ? 'complete' : 'pending';
isConfirming = false;
async function handleChange(event: Event) {
await toggleComplete({ event, task, isConfirming });
}
</script>

<input
type="checkbox"
checked={task.complete}
{disabled}
on:change={toggleComplete}
on:change={handleChange}
/>

0 comments on commit 7d20443

Please sign in to comment.