|
1 | 1 | <script lang="ts"> |
2 | 2 | import { useSvelteFlow, type NodeProps, Handle, Position } from '@xyflow/svelte'; |
3 | | - import { type Task, Status } from '$lib/api'; |
4 | | - import { Modal } from 'flowbite-svelte'; |
| 3 | + import { type Task, Status, api } from '$lib/api'; |
| 4 | + import { Modal, Button } from 'flowbite-svelte'; |
5 | 5 | // import { transformWithEsbuild } from 'vite'; |
6 | 6 |
|
7 | 7 | let { id, data }: NodeProps<Task> = $props(); |
8 | 8 | let { updateNodeData } = useSvelteFlow(); |
9 | 9 |
|
| 10 | + // Get graph ID from the data passed from parent |
| 11 | + let currentGraphId = data.graphId || 'abc123'; |
| 12 | +
|
10 | 13 | // Local state for editing |
11 | 14 | let isEditing = $state(false); |
12 | 15 | let editName = $state(data.name || ''); |
13 | 16 | let editDescription = $state(data.description || ''); |
14 | 17 |
|
15 | 18 | let editModal = $state(false); |
| 19 | + let isSaving = $state(false); |
| 20 | + let errorMessage = $state(''); |
| 21 | +
|
| 22 | + async function saveTask() { |
| 23 | + if (isSaving) return; |
| 24 | +
|
| 25 | + isSaving = true; |
| 26 | + errorMessage = ''; |
| 27 | +
|
| 28 | + try { |
| 29 | + // Prepare the updated task data |
| 30 | + const updatedTask = { |
| 31 | + ...data, |
| 32 | + name: data.name, |
| 33 | + description: data.description, |
| 34 | + tags: |
| 35 | + typeof data.tags === 'string' |
| 36 | + ? data.tags |
| 37 | + .split(',') |
| 38 | + .map((t) => t.trim()) |
| 39 | + .filter((t) => t) |
| 40 | + : data.tags, |
| 41 | + users: |
| 42 | + typeof data.users === 'string' |
| 43 | + ? data.users |
| 44 | + .split(',') |
| 45 | + .map((u) => u.trim()) |
| 46 | + .filter((u) => u) |
| 47 | + : data.users |
| 48 | + }; |
| 49 | +
|
| 50 | + // Call the backend API |
| 51 | + const response = await api.updateTask(currentGraphId, id, updatedTask); |
| 52 | +
|
| 53 | + // Update the node data in the flow |
| 54 | + updateNodeData(id, updatedTask); |
| 55 | +
|
| 56 | + // Close the modal |
| 57 | + editModal = false; |
| 58 | +
|
| 59 | + console.log('Task updated successfully:', response); |
| 60 | + } catch (error) { |
| 61 | + console.error('Failed to update task:', error); |
| 62 | + errorMessage = 'Failed to save task. Please try again.'; |
| 63 | + } finally { |
| 64 | + isSaving = false; |
| 65 | + } |
| 66 | + } |
16 | 67 |
|
17 | 68 | function toggleEdit() { |
18 | 69 | if (isEditing) { |
|
59 | 110 | <h3 class="task-title">{data.name || 'Unnamed Task'}</h3> |
60 | 111 | <button class="edit-btn" onclick={() => (editModal = true)}> ✎ </button> |
61 | 112 | <Modal title="Edit the task" bind:open={editModal} autoclose> |
62 | | - <p class="text-base leading-relaxed text-gray-500 dark:text-gray-400"> |
63 | | - {data.name} |
64 | | - </p> |
65 | | - <p class="text-base leading-relaxed text-gray-500 dark:text-gray-400"> |
66 | | - {data.description} |
67 | | - </p> |
| 113 | + <form class="flex flex-col space-y-2" onsubmit={saveTask}> |
| 114 | + {#if errorMessage} |
| 115 | + <div class="alert alert-error rounded bg-red-100 p-2 text-sm text-red-700"> |
| 116 | + {errorMessage} |
| 117 | + </div> |
| 118 | + {/if} |
| 119 | + <input |
| 120 | + type="text" |
| 121 | + placeholder="Task name" |
| 122 | + class="input input-bordered w-full" |
| 123 | + bind:value={data.name} |
| 124 | + required |
| 125 | + /> |
| 126 | + <textarea |
| 127 | + bind:value={data.description} |
| 128 | + class="textarea textarea-bordered w-full" |
| 129 | + placeholder="Task description" |
| 130 | + rows="3" |
| 131 | + ></textarea> |
| 132 | + <input |
| 133 | + type="text" |
| 134 | + placeholder="Tags (comma separated)" |
| 135 | + class="input input-bordered w-full" |
| 136 | + bind:value={data.tags} |
| 137 | + /> |
| 138 | + <input |
| 139 | + type="text" |
| 140 | + placeholder="Users (comma separated)" |
| 141 | + class="input input-bordered w-full" |
| 142 | + bind:value={data.users} |
| 143 | + /> |
| 144 | + <Button type="submit" class="w-full" disabled={isSaving}> |
| 145 | + {isSaving ? 'Saving...' : 'Save'} |
| 146 | + </Button> |
| 147 | + </form> |
68 | 148 | </Modal> |
69 | 149 | </div> |
70 | 150 |
|
|
0 commit comments