Skip to content
This repository was archived by the owner on Nov 29, 2025. It is now read-only.

Commit 9d3f8ba

Browse files
committed
feat: enhance task creation form with loading state and improved accountId handling
1 parent 875648c commit 9d3f8ba

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/routes/(app)/app/tasks/new/+page.server.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const actions = {
9797
});
9898
}
9999
} else {
100-
accountId = null;
100+
accountId = undefined;
101101
}
102102

103103
const dueDate = dueDateStr ? new Date(dueDateStr) : null;
@@ -112,14 +112,10 @@ export const actions = {
112112
description: description || null,
113113
ownerId: ownerId,
114114
createdById: user.id,
115-
organizationId: org.id
115+
organizationId: org.id,
116+
...(accountId && { accountId })
116117
};
117118

118-
// Add accountId if it exists
119-
if (accountId) {
120-
taskData.accountId = accountId;
121-
}
122-
123119
console.log('Creating task with data:', taskData);
124120

125121
const task = await prisma.task.create({
@@ -128,14 +124,18 @@ export const actions = {
128124

129125
console.log('Task created successfully:', task);
130126

131-
// Redirect back to account page if task was created from an account
127+
// Redirect based on where the task was created from
132128
if (accountId) {
133-
return { 'success': "task created successfully" };
129+
// If task was created from an account page, redirect back to that account
130+
throw redirect(303, `/app/accounts/${accountId}`);
131+
} else {
132+
// Otherwise, redirect to the tasks list
133+
throw redirect(303, '/app/tasks/list');
134134
}
135135

136136
} catch (e) {
137137
// If it's a redirect, let it pass through
138-
if (e.status === 303) {
138+
if (e && typeof e === 'object' && 'status' in e && e.status === 303) {
139139
throw e;
140140
}
141141

src/routes/(app)/app/tasks/new/+page.svelte

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
let accountId = $state(form?.accountId ?? urlAccountId ?? '');
2121
let description = $state(form?.description ?? '');
2222
23+
// Form submission state
24+
let isSubmitting = $state(false);
25+
2326
// Update local state if 'form' prop changes (e.g., after a failed form submission)
2427
$effect(() => {
2528
if (form) {
@@ -99,7 +102,17 @@
99102
</div>
100103
{/if}
101104
102-
<form method="POST" use:enhance class="p-6">
105+
<form
106+
method="POST"
107+
use:enhance={() => {
108+
isSubmitting = true;
109+
return async ({ update }) => {
110+
await update();
111+
isSubmitting = false;
112+
};
113+
}}
114+
class="p-6"
115+
>
103116
<!-- Hidden field for pre-selected account -->
104117
{#if urlAccountId}
105118
<input type="hidden" name="accountId" value={urlAccountId} />
@@ -140,7 +153,7 @@
140153
<div>
141154
<label
142155
for="status"
143-
class="mb-1 block flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
156+
class="mb-1 flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
144157
>
145158
<Clock size={14} class="mr-1 text-gray-500 dark:text-gray-400" />
146159
Status
@@ -162,7 +175,7 @@
162175
<div>
163176
<label
164177
for="priority"
165-
class="mb-1 block flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
178+
class="mb-1 flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
166179
>
167180
<Flag size={14} class="mr-1 text-gray-500 dark:text-gray-400" />
168181
Priority
@@ -182,7 +195,7 @@
182195
<div>
183196
<label
184197
for="dueDate"
185-
class="mb-1 block flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
198+
class="mb-1 flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
186199
>
187200
<Calendar size={14} class="mr-1 text-gray-500 dark:text-gray-400" />
188201
Due Date
@@ -235,7 +248,7 @@
235248
<div>
236249
<label
237250
for="accountId"
238-
class="mb-1 block flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
251+
class="mb-1 flex items-center text-sm font-medium text-gray-700 dark:text-gray-300"
239252
>
240253
<Building size={14} class="mr-1 text-gray-500 dark:text-gray-400" />
241254
Related Account
@@ -299,16 +312,26 @@
299312
>
300313
<button
301314
type="button"
302-
class="rounded-lg border border-gray-300 bg-white px-5 py-2.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-50 focus:ring-2 focus:ring-gray-500 focus:ring-offset-2 focus:ring-offset-white focus:outline-none dark:border-gray-600 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 dark:focus:ring-gray-400 dark:focus:ring-offset-gray-800"
315+
disabled={isSubmitting}
316+
class="rounded-lg border border-gray-300 bg-white px-5 py-2.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-50 focus:ring-2 focus:ring-gray-500 focus:ring-offset-2 focus:ring-offset-white focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed dark:border-gray-600 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 dark:focus:ring-gray-400 dark:focus:ring-offset-gray-800"
303317
onclick={handleCancel}
304318
>
305319
Cancel
306320
</button>
307321
<button
308322
type="submit"
309-
class="rounded-lg bg-blue-600 px-5 py-2.5 text-sm font-semibold text-white shadow-sm transition-colors hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white focus:outline-none dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-400 dark:focus:ring-offset-gray-800"
323+
disabled={isSubmitting}
324+
class="rounded-lg bg-blue-600 px-5 py-2.5 text-sm font-semibold text-white shadow-sm transition-colors hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-400 dark:focus:ring-offset-gray-800"
310325
>
311-
Create Task
326+
{#if isSubmitting}
327+
<svg class="inline-block w-4 h-4 mr-2 animate-spin" viewBox="0 0 24 24" fill="none">
328+
<circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" class="opacity-25"/>
329+
<path fill="currentColor" class="opacity-75" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"/>
330+
</svg>
331+
Creating Task...
332+
{:else}
333+
Create Task
334+
{/if}
312335
</button>
313336
</div>
314337
</form>

0 commit comments

Comments
 (0)