From f33a4d13f794729433efbb9c8b0d6ae11ad880ce Mon Sep 17 00:00:00 2001 From: Christian Beutel <> Date: Fri, 15 Mar 2024 13:42:27 +0100 Subject: [PATCH] scroll to required fields in trail edit Former-commit-id: 4d4c11bd86760b9ee98b7db188df279cf71e82d1 --- .../components/trail/trail_info_panel.svelte | 279 ++++++++++++++++++ .../lib/vendor/svelte-form-lib/create-form.js | 4 +- web/src/lib/vendor/svelte-form-lib/index.d.ts | 1 + web/src/routes/map/trail/[id]/+page.svelte | 167 +---------- web/src/routes/trail/edit/[id]/+page.svelte | 20 ++ web/src/routes/trail/view/[id]/+page.svelte | 263 +---------------- 6 files changed, 309 insertions(+), 425 deletions(-) create mode 100644 web/src/lib/components/trail/trail_info_panel.svelte diff --git a/web/src/lib/components/trail/trail_info_panel.svelte b/web/src/lib/components/trail/trail_info_panel.svelte new file mode 100644 index 00000000..2f7b1372 --- /dev/null +++ b/web/src/lib/components/trail/trail_info_panel.svelte @@ -0,0 +1,279 @@ + + +
+
+
+ +
+
+
+

+ {trail.name} +

+
+ {#if trail.location} +

+ + {trail.location} +

+ {/if} +

+ + {$_(trail.difficulty ?? "?")} +

+
+
+ {#if $currentUser && $currentUser.id == trail.author} + + {/if} +
+
+ {#if showMap} +
+
+ {$_("distance")} + {formatDistance(trail.distance)} +
+
+ {$_("elevation-gain")} + {formatElevation(trail.elevation_gain)} +
+
+ {$_("est-duration")} + {formatTimeHHMM(trail.duration)} +
+ {#if trail.expand.category} +
+ {$_("category")} + {trail.expand.category.name} +
+ {/if} +
+ {/if} + {#if trail.tags && trail.tags.length > 0} +
+
+ {#each trail.tags as tag} + {tag} + {/each} +
+
+ {/if} +
+ +
+
+
+
+ {#if activeTab == 0} +
+ {trail.description} +
+ {/if} + {#if activeTab == 1} +
    + {#each trail.expand.waypoints ?? [] as waypoint, i} +
  • openMarkerPopup(i)}> + +
  • + {/each} +
+ {/if} + {#if activeTab == 2} + + {/if} + {#if activeTab == 3} +
    + {#each trail.expand.summit_logs ?? [] as log} +
  • + {/each} +
+ {/if} + {#if showMap} +
+
+
+ +
+
+
+ {/if} +
+
+
+ + diff --git a/web/src/lib/vendor/svelte-form-lib/create-form.js b/web/src/lib/vendor/svelte-form-lib/create-form.js index b88c0691..bd25a95c 100644 --- a/web/src/lib/vendor/svelte-form-lib/create-form.js +++ b/web/src/lib/vendor/svelte-form-lib/create-form.js @@ -28,6 +28,7 @@ export const createForm = (config) => { const validationSchema = config.validationSchema; const validateFunction = config.validate; const onSubmit = config.onSubmit; + const onError = config.onError; const getInitial = { values: () => util.cloneDeep(initialValues), @@ -133,6 +134,7 @@ export const createForm = (config) => { return clearErrorsAndSubmit(values); } else { errors.set(error); + onError(error) isSubmitting.set(false); } }) @@ -154,7 +156,7 @@ export const createForm = (config) => { yupErrors.inner.map((error) => util.set(updatedErrors, error.path, error.message), ); - + onError(updatedErrors) errors.set(updatedErrors); } isSubmitting.set(false); diff --git a/web/src/lib/vendor/svelte-form-lib/index.d.ts b/web/src/lib/vendor/svelte-form-lib/index.d.ts index 83a75f6a..2fb3a423 100644 --- a/web/src/lib/vendor/svelte-form-lib/index.d.ts +++ b/web/src/lib/vendor/svelte-form-lib/index.d.ts @@ -61,6 +61,7 @@ type FormState> = { declare function createForm>(formProperties: { initialValues: Inf; onSubmit: (values: Inf) => any | Promise; + onError?: (errors: Record) => any | Promise; validate?: (values: Inf) => any | undefined; validationSchema?: ObjectSchema; }): FormState; diff --git a/web/src/routes/map/trail/[id]/+page.svelte b/web/src/routes/map/trail/[id]/+page.svelte index 39fa3a74..6df874c4 100644 --- a/web/src/routes/map/trail/[id]/+page.svelte +++ b/web/src/routes/map/trail/[id]/+page.svelte @@ -1,79 +1,15 @@ @@ -82,107 +18,8 @@
-
- -
-
-
-

- {$trail.name} -

-
-

- - {$trail.location} -

-

- - {$_($trail.difficulty ?? "?")} -

-
-
- {#if $currentUser && $currentUser.id == $trail.author} - - {/if} -
-
-
-
- {$_('distance')} - {formatDistance($trail.distance)} -
-
- {$_('elevation-gain')} - {formatElevation($trail.elevation_gain)} -
-
- {$_('est-duration')} - {formatTimeHHMM($trail.duration)} -
- {#if $trail.expand.category} -
- {$_('category')} - {$trail.expand.category.name} -
- {/if} -
-
-
- - {#if activeTab == 0} -
- {$trail.description} -
- {/if} - {#if activeTab == 1} -
    - {#each $trail.expand.waypoints ?? [] as waypoint, i} -
  • openMarkerPopup(i)}> - -
  • - {/each} -
- {/if} - {#if activeTab == 2} - - {/if} - {#if activeTab == 3} -
    - {#each $trail.expand.summit_logs ?? [] as log} -
  • - {/each} -
- {/if} -
+
diff --git a/web/src/routes/trail/edit/[id]/+page.svelte b/web/src/routes/trail/edit/[id]/+page.svelte index a5ac8ee2..6350b24b 100644 --- a/web/src/routes/trail/edit/[id]/+page.svelte +++ b/web/src/routes/trail/edit/[id]/+page.svelte @@ -101,6 +101,26 @@ const { form, errors, handleChange, handleSubmit } = createForm({ initialValues: data.trail, validationSchema: trailSchema, + onError: (errors) => { + if (errors.name) { + const nameInput = document.querySelector( + "input[name=name]", + ) as HTMLElement; + + if (window.innerWidth < 768) { + window?.scroll({ + top: nameInput.offsetTop - 24, + behavior: "smooth", + }); + } else { + const form = document.getElementById("trail-form"); + form?.scroll({ + top: nameInput.offsetTop - 130, + behavior: "smooth", + }); + } + } + }, onSubmit: async (submittedTrail) => { loading = true; try { diff --git a/web/src/routes/trail/view/[id]/+page.svelte b/web/src/routes/trail/view/[id]/+page.svelte index 855127a3..4a6bb196 100644 --- a/web/src/routes/trail/view/[id]/+page.svelte +++ b/web/src/routes/trail/view/[id]/+page.svelte @@ -1,267 +1,12 @@ - + + {$trail.name} | {$_("trail", { values: { n: 1 } })} | wanderer - -
-
- -
-
-
-

- {$trail.name} -

-
-

- - {$trail.location} -

-

- - {$_($trail.difficulty ?? "?")} -

-
-
- {#if $currentUser && $currentUser.id == $trail.author} - - {/if} -
-
-
-
- {$_("distance")} - {formatDistance($trail.distance)} -
-
- {$_("elevation-gain")} - {formatElevation($trail.elevation_gain)} -
-
- {$_("est-duration")} - {formatTimeHHMM($trail.duration)} -
- {#if $trail.expand.category} -
- {$_("category")} - {$trail.expand.category.name} -
- {/if} -
- {#if $trail.tags && $trail.tags.length > 0} -
-
- {#each $trail.tags as tag} - {tag} - {/each} -
-
- {/if} -
- -
-
- {#if activeTab == 0} -
- {$trail.description} -
- {/if} - {#if activeTab == 1} -
    - {#each $trail.expand.waypoints ?? [] as waypoint, i} -
  • openMarkerPopup(i)}> - -
  • - {/each} -
- {/if} - {#if activeTab == 2} - - {/if} - {#if activeTab == 3} -
    - {#each $trail.expand.summit_logs ?? [] as log} -
  • - {/each} -
- {/if} -
-
-
-
- -
-
-
-
-
-
- -