Skip to content

Commit 62dbebc

Browse files
committed
Timers weren't starting until after onSubmit.
Fixes #284
1 parent 36ef4e2 commit 62dbebc

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Timers weren't starting until after `onSubmit`, allowing multiple form submissions on longer async operations. ([#284](https://github.com/ciscoheat/sveltekit-superforms/issues/284))
13+
814
## [1.10.0] - 2023-11-07
915

1016
### Added

src/lib/client/formEnhance.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,21 +254,25 @@ export function formEnhance<T extends AnyZodObject, M>(
254254
let currentRequest: AbortController | null;
255255

256256
return enhance(formEl, async (submit) => {
257-
const submitCancel = submit.cancel;
257+
const _submitCancel = submit.cancel;
258258

259259
let cancelled = false;
260-
function cancel() {
260+
function cancel(resetTimers = true) {
261261
cancelled = true;
262-
return submitCancel();
262+
if (resetTimers && htmlForm.isSubmitting()) {
263+
htmlForm.completed(true);
264+
}
265+
return _submitCancel();
263266
}
264267
submit.cancel = cancel;
265268

266269
if (htmlForm.isSubmitting() && options.multipleSubmits == 'prevent') {
267-
cancel();
270+
cancel(false);
268271
} else {
269272
if (htmlForm.isSubmitting() && options.multipleSubmits == 'abort') {
270273
if (currentRequest) currentRequest.abort();
271274
}
275+
htmlForm.submitting();
272276
currentRequest = submit.controller;
273277

274278
for (const event of formEvents.onSubmit) {
@@ -328,8 +332,6 @@ export function formEnhance<T extends AnyZodObject, M>(
328332
options.flashMessage.module.getFlash(page).set(undefined);
329333
}
330334

331-
htmlForm.submitting();
332-
333335
// Deprecation fix
334336
const submitData =
335337
'formData' in submit

src/routes/tests/redirect-same-route/+page.svelte

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
import { superForm } from '$lib/client';
33
import type { PageData } from './$types';
44
import SuperDebug from '$lib/client/SuperDebug.svelte';
5-
import { schema } from './schema';
65
76
export let data: PageData;
87
8+
let shouldCancel = false;
9+
910
const { form, errors, tainted, message, enhance, submitting } = superForm(
1011
data.form,
1112
{
12-
//dataType: 'json',
13-
//validators: schema
13+
onSubmit({ cancel }) {
14+
if (shouldCancel) cancel();
15+
}
1416
}
1517
);
1618
</script>
@@ -28,17 +30,31 @@
2830
<button>Submit</button>
2931
{#if $submitting}
3032
<svg
31-
xmlns="http://www.w3.org/2000/svg"
3233
width="24"
3334
height="24"
3435
viewBox="0 0 24 24"
35-
><path
36-
fill="currentColor"
37-
d="M12 21a9 9 0 1 1 6.18-15.55a.75.75 0 0 1 0 1.06a.74.74 0 0 1-1.06 0A7.51 7.51 0 1 0 19.5 12a.75.75 0 0 1 1.5 0a9 9 0 0 1-9 9Z"
36+
xmlns="http://www.w3.org/2000/svg"
37+
><style>
38+
.spinner_ajPY {
39+
transform-origin: center;
40+
animation: spinner_AtaB 0.75s infinite linear;
41+
}
42+
@keyframes spinner_AtaB {
43+
100% {
44+
transform: rotate(360deg);
45+
}
46+
}
47+
</style><path
48+
d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
49+
opacity=".25"
50+
/><path
51+
d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
52+
class="spinner_ajPY"
3853
/></svg
3954
>
4055
{/if}
4156
</div>
57+
<input type="checkbox" bind:checked={shouldCancel} /> Cancel submit
4258
</form>
4359

4460
<style lang="scss">

0 commit comments

Comments
 (0)