|
|
@@ -1,5 +1,7 @@
|
|
|
<script>
|
|
|
+ import { applyAction, enhance } from '$app/forms';
|
|
|
import logoMark from '$lib/images/logo_white.svg';
|
|
|
+ import { notifier } from '$lib/notifier.svelte.js';
|
|
|
|
|
|
const EXERCISE_TYPES = [
|
|
|
{ value: 'weights', label: 'Weights' },
|
|
|
@@ -18,6 +20,7 @@
|
|
|
|
|
|
let title = $state('');
|
|
|
let exercises = $state([emptyExercise()]);
|
|
|
+ let submitting = $state(false);
|
|
|
|
|
|
function addExercise() {
|
|
|
exercises = [...exercises, emptyExercise()];
|
|
|
@@ -30,13 +33,6 @@
|
|
|
if (exercises.length <= 1) return;
|
|
|
exercises = exercises.filter((exercise) => exercise.key !== key);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * @param {SubmitEvent} event
|
|
|
- */
|
|
|
- function handleSubmit(event) {
|
|
|
- event.preventDefault();
|
|
|
- }
|
|
|
</script>
|
|
|
|
|
|
<svelte:head>
|
|
|
@@ -50,7 +46,7 @@
|
|
|
<span class="brand-name">Torus</span>
|
|
|
</a>
|
|
|
<div class="top-actions">
|
|
|
- <a class="btn btn-ghost" href="/workouts">Back</a>
|
|
|
+ <a class="btn btn-ghost" href="/workout">Back</a>
|
|
|
<a class="btn btn-ghost" href="/logout">Log out</a>
|
|
|
</div>
|
|
|
</header>
|
|
|
@@ -62,7 +58,33 @@
|
|
|
<p class="lede">Name the session, then add as many exercises as you need.</p>
|
|
|
</div>
|
|
|
|
|
|
- <form class="form" method="POST" onsubmit={handleSubmit}>
|
|
|
+ <form
|
|
|
+ class="form"
|
|
|
+ method="POST"
|
|
|
+ use:enhance={() => {
|
|
|
+ submitting = true;
|
|
|
+ return async ({ result, update }) => {
|
|
|
+ submitting = false;
|
|
|
+
|
|
|
+ if (result.type === 'failure') {
|
|
|
+ const message =
|
|
|
+ result.data && typeof result.data === 'object' && 'message' in result.data
|
|
|
+ ? String(result.data.message)
|
|
|
+ : 'Could not create workout';
|
|
|
+ notifier.fail(message);
|
|
|
+ await update({ reset: false });
|
|
|
+ } else if (result.type === 'redirect') {
|
|
|
+ notifier.ok('Workout created');
|
|
|
+ await applyAction(result);
|
|
|
+ } else if (result.type === 'error') {
|
|
|
+ notifier.fail(result.error?.message ?? 'Could not create workout');
|
|
|
+ await update({ reset: false });
|
|
|
+ } else {
|
|
|
+ await update({ reset: false });
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }}
|
|
|
+ >
|
|
|
<label class="field">
|
|
|
<span class="label">Workout name</span>
|
|
|
<input
|
|
|
@@ -71,6 +93,7 @@
|
|
|
name="title"
|
|
|
required
|
|
|
placeholder="Push A"
|
|
|
+ disabled={submitting}
|
|
|
bind:value={title}
|
|
|
/>
|
|
|
</label>
|
|
|
@@ -78,7 +101,12 @@
|
|
|
<div class="exercises" aria-label="Exercises">
|
|
|
<div class="exercises-head">
|
|
|
<h2>Exercises</h2>
|
|
|
- <button type="button" class="btn btn-ghost" onclick={addExercise}>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ class="btn btn-ghost"
|
|
|
+ disabled={submitting}
|
|
|
+ onclick={addExercise}
|
|
|
+ >
|
|
|
Add exercise
|
|
|
</button>
|
|
|
</div>
|
|
|
@@ -97,13 +125,19 @@
|
|
|
name="exerciseName"
|
|
|
required
|
|
|
placeholder="Bench press"
|
|
|
+ disabled={submitting}
|
|
|
bind:value={exercise.name}
|
|
|
/>
|
|
|
</label>
|
|
|
|
|
|
<label class="field field-type">
|
|
|
<span class="label">Type</span>
|
|
|
- <select class="input select" name="exerciseType" bind:value={exercise.type}>
|
|
|
+ <select
|
|
|
+ class="input select"
|
|
|
+ name="exerciseType"
|
|
|
+ disabled={submitting}
|
|
|
+ bind:value={exercise.type}
|
|
|
+ >
|
|
|
{#each EXERCISE_TYPES as option}
|
|
|
<option value={option.value}>{option.label}</option>
|
|
|
{/each}
|
|
|
@@ -113,7 +147,7 @@
|
|
|
<button
|
|
|
type="button"
|
|
|
class="btn btn-ghost remove"
|
|
|
- disabled={exercises.length <= 1}
|
|
|
+ disabled={submitting || exercises.length <= 1}
|
|
|
onclick={() => removeExercise(exercise.key)}
|
|
|
>
|
|
|
Remove
|
|
|
@@ -125,8 +159,10 @@
|
|
|
</div>
|
|
|
|
|
|
<div class="form-actions">
|
|
|
- <a class="btn btn-ghost" href="/workouts">Cancel</a>
|
|
|
- <button class="btn btn-primary" type="submit">Create workout</button>
|
|
|
+ <a class="btn btn-ghost" href="/workout">Cancel</a>
|
|
|
+ <button class="btn btn-primary" type="submit" disabled={submitting}>
|
|
|
+ {submitting ? 'Creating…' : 'Create workout'}
|
|
|
+ </button>
|
|
|
</div>
|
|
|
</form>
|
|
|
</section>
|