From 58557301c9167ecae189d1661e8aee5814a70ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristo=CC=81fer=20R?= Date: Wed, 23 Oct 2024 23:48:42 -0400 Subject: [PATCH] allow updating task details --- resources/js/src/entrypoints/Tasks.svelte | 68 ++++++++++++++------- resources/js/src/lib/api/tasks.ts | 7 ++- resources/js/src/lib/components/task.svelte | 7 ++- routes/api.php | 27 ++++++++ 4 files changed, 86 insertions(+), 23 deletions(-) diff --git a/resources/js/src/entrypoints/Tasks.svelte b/resources/js/src/entrypoints/Tasks.svelte index 7cb3f72..02d3aed 100644 --- a/resources/js/src/entrypoints/Tasks.svelte +++ b/resources/js/src/entrypoints/Tasks.svelte @@ -2,7 +2,7 @@ import { flip } from 'svelte/animate'; import { fade } from 'svelte/transition'; - import { getTasks, getTags, createTask, moveTask, deleteTask } from '../lib/api/tasks'; + import { getTasks, getTags, createTask, moveTask, updateTask, deleteTask } from '../lib/api/tasks'; import Task from '../lib/components/task.svelte'; import Pill from '../lib/components/pill.svelte'; @@ -24,6 +24,9 @@ let tags: Tag[] = []; let selectedTask: TaskType | undefined = undefined; + let selectedTaskDescription: string = ''; + let selectedTaskDetails: string = ''; + let fetchTasks = async () => { if (await $user) { $tasks = await getTasks(); @@ -94,27 +97,31 @@ }; } - function handleKeyPress(ev: KeyboardEvent) { - // Make sure event propagates if the modal is already open - if (dialog.open) { - return; - } - const { key } = ev; + async function createEmptyTask() { + try { + selectedTask = await createTask('', []); - if (key === 'n') { - console.log('create new task'); - ev.preventDefault(); - dialog.showModal(); + selectedTaskDescription = ''; + selectedTaskDetails = ''; + + $tasks = await getTasks(selectedTag); + } catch (_) { + $tasks = $tasks.filter(t => t.id !== -1); } } - async function createEmptyTask() { + async function updateSelectedTask() { + if (!selectedTask) { + return; + } + try { - await createTask('', []); + await updateTask(selectedTask, { description: selectedTaskDescription, details: selectedTaskDetails }); + } catch (e) { + console.error(e); + } finally { $tasks = await getTasks(selectedTag); - } catch (_) { - $tasks = $tasks.filter(t => t.id !== -1); } } @@ -199,9 +206,28 @@ $tasks.splice(indexOfTask, 0, task); } } - - + function debounce(func: Function, wait: number, immediate?: boolean) { + var timeout: number | undefined; + return function(this: any) { + var context = this; + var args = arguments; + + clearTimeout(timeout); + + if (immediate && !timeout) { + func.apply(context, args); + } + + timeout = setTimeout(function() { + timeout = undefined; + if (!immediate) { + func.apply(context, args); + } + }, wait); + }; + } +
@@ -241,14 +267,14 @@ class={itemBeingDragged ? 'is-dragging' : ''} animate:flip={{ duration: 200 }} in:fade - on:click={() => (selectedTask = task)} + on:click={() => {selectedTask = task; selectedTaskDescription = task.description; selectedTaskDetails = task.details ?? '';}} draggable="true" on:dragstart={onDragStart(task.id)} on:dragend={onDragEnd()} on:dragenter|preventDefault={swapOnEnter(task.id)} on:dragover|preventDefault={() => {}} > - + {/each} @@ -258,8 +284,8 @@
- - + +
diff --git a/resources/js/src/lib/api/tasks.ts b/resources/js/src/lib/api/tasks.ts index fb0bf52..47a1646 100644 --- a/resources/js/src/lib/api/tasks.ts +++ b/resources/js/src/lib/api/tasks.ts @@ -54,6 +54,11 @@ async function createTask(description: string, tags?: string[]) { return task; } +async function updateTask(task: Task, updatedTaskData: Partial) { + const response = await axios.patch(`/api/tasks/update/${task.id}`, updatedTaskData); + return taskSchema.parse(response.data); +} + async function moveTask(task: Task, order: number) { const response = await axios.patch(`/api/tasks/move/${task.id}`, { new_pos: order }); const updatedTask = taskSchema.parse(response.data); @@ -65,4 +70,4 @@ async function deleteTask(task: Task) { await axios.delete(`/api/tasks/${task.id}`); } -export { getTasks, getTags, createTask, moveTask, deleteTask }; +export { getTasks, getTags, createTask, moveTask, updateTask, deleteTask }; diff --git a/resources/js/src/lib/components/task.svelte b/resources/js/src/lib/components/task.svelte index dfec484..564d38e 100644 --- a/resources/js/src/lib/components/task.svelte +++ b/resources/js/src/lib/components/task.svelte @@ -7,13 +7,14 @@ export let isDragging = false; export let onDelete: (t: typeof task) => void; + export let isSelected = false; function buttonHandler(_ev: MouseEvent) { onDelete(task); } -
+

{task.description}