From 07525dffe190a5accbbf95ea81e6463be65841d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Rizio?= Date: Sun, 17 May 2020 15:12:27 +0200 Subject: [PATCH] Correct behavior of page change When changing page, but in the same component, the page was not refreshed. There was these two issues: * No transition * Some part of the page is not updated The reason was because Svelte reused the exact same component (so no mount / destroy and no animation on component creation / destruction). To correctly update the page, we change the `Page` component by adding a keyed-each block (see https://github.com/sveltejs/svelte/issues/1469). There was some issue with this solution because each time we update the component, the render is updated so the Page and create an infinite loop. To avoid this problem, we remove the call to `Page` component from concrete pages and include it directly with the Router. To do so, we create the PageRoute component behaving as expected. --- src/App.svelte | 13 +-- src/components/{ => page}/Page.svelte | 2 +- src/components/page/PageRoute.svelte | 18 ++++ src/pages/category/CategoryPage.svelte | 34 +++---- src/pages/food/CreateFoodPage.svelte | 17 ++-- src/pages/food/FoodPage.svelte | 136 ++++++++++++------------- src/pages/searches/SearchesPage.svelte | 65 ++++++------ src/pages/units/UnitsPage.svelte | 65 ++++++------ 8 files changed, 178 insertions(+), 172 deletions(-) rename src/components/{ => page}/Page.svelte (86%) create mode 100644 src/components/page/PageRoute.svelte diff --git a/src/App.svelte b/src/App.svelte index 80852cf..c033fd0 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -13,6 +13,7 @@ import HomePage from "~/pages/home/HomePage.svelte" import TwoColumns from "~/components/layout/TwoColumns.svelte" + import PageRoute from "~/components/page/PageRoute.svelte" export let url = "" @@ -35,16 +36,16 @@
- + - + - - - + + + - +
diff --git a/src/components/Page.svelte b/src/components/page/Page.svelte similarity index 86% rename from src/components/Page.svelte rename to src/components/page/Page.svelte index 2fef8b3..c8389c9 100644 --- a/src/components/Page.svelte +++ b/src/components/page/Page.svelte @@ -1,5 +1,5 @@ + + + {#each [`${Date.now()}-${Math.random()}`] as x(x)} + + + + {/each} + \ No newline at end of file diff --git a/src/pages/category/CategoryPage.svelte b/src/pages/category/CategoryPage.svelte index 240f595..73e5bed 100644 --- a/src/pages/category/CategoryPage.svelte +++ b/src/pages/category/CategoryPage.svelte @@ -2,7 +2,6 @@ import { onMount } from "svelte" import i18n from "~/i18n" import receptoStore from "~/store/ReceptoStore" - import Page from "~/components/Page.svelte" import UpdatableSection from "~/components/updatable/UpdatableSection.svelte" import InputText from "~/components/fields/InputText.svelte" import { findCategory, saveCategory, deleteCategory } from "./CategoryPage" @@ -30,22 +29,21 @@ } - - {#if category !== undefined} - -
-

{ category.name }

-
- -
- -
-
+{#if category !== undefined} + +
+

{ category.name }

+
-
- +
+
- {:else} -

{$i18n.t("common.notFound")}

- {/if} - \ No newline at end of file + + +
+ +
+{:else} +

{$i18n.t("common.notFound")}

+{/if} \ No newline at end of file diff --git a/src/pages/food/CreateFoodPage.svelte b/src/pages/food/CreateFoodPage.svelte index 449512f..2990f65 100644 --- a/src/pages/food/CreateFoodPage.svelte +++ b/src/pages/food/CreateFoodPage.svelte @@ -2,7 +2,6 @@ import i18n from "~/i18n" import { buildPrimaryInformation, addFood } from "./CreateFoodPage" - import Page from "~/components/Page.svelte" import Button from "~/components/buttons/Button.svelte" import PrimaryInformationForm from "./primaryInformation/PrimaryInformationForm.svelte" @@ -12,14 +11,12 @@ let primaryInformation = buildPrimaryInformation(categoryId) - -

{$i18n.t("pages.food.create.title")}

+

{$i18n.t("pages.food.create.title")}

-
addFood(primaryInformation)}> - + addFood(primaryInformation)}> + - - -
\ No newline at end of file + + \ No newline at end of file diff --git a/src/pages/food/FoodPage.svelte b/src/pages/food/FoodPage.svelte index 62adf77..983b17c 100644 --- a/src/pages/food/FoodPage.svelte +++ b/src/pages/food/FoodPage.svelte @@ -20,7 +20,6 @@ import Button from "~/components/buttons/Button.svelte" import Grid from "~/components/layout/Grid.svelte" import RecipeCard from "~/components/card/RecipeCard.svelte" - import Page from "~/components/Page.svelte" import Collapsable from "~/components/collapsable/Collapsable.svelte" import SectionView from "./section/SectionView.svelte" import UpdatableSection from "~/components/updatable/UpdatableSection.svelte" @@ -99,79 +98,78 @@ } - - {#if food} - -
- -
- -
- -
-
- - {#each sections as section, index} -
- saveSection(index)} - on:cancel={() => cancelSection(index)} - > -
- -
- -
- -
- - +{#if food} + +
+ +
+ +
+ +
+
+ + {#each sections as section, index} +
+ saveSection(index)} + on:cancel={() => cancelSection(index)} + > +
+ +
+ +
+ +
+ + -
-
- {/each} - - {#if !hasUnsavedAddedSection} -
- -
- {/if} - - {#if nonEmpty(usedFor)} -
-

{$i18n.t("pages.food.page.usedFor")}

- - - {#each usedFor as info} - - {/each} - -
- {/if} - - +
+
+ {/each} + {#if !hasUnsavedAddedSection}
- +
- {:else} -

{$i18n.t("common.notFound")}

{/if} -
\ No newline at end of file + + {#if nonEmpty(usedFor)} +
+

{$i18n.t("pages.food.page.usedFor")}

+ + + {#each usedFor as info} + + {/each} + +
+ {/if} + + + +
+ +
+{:else} +

{$i18n.t("common.notFound")}

+{/if} \ No newline at end of file diff --git a/src/pages/searches/SearchesPage.svelte b/src/pages/searches/SearchesPage.svelte index c0b2ec6..40e5811 100644 --- a/src/pages/searches/SearchesPage.svelte +++ b/src/pages/searches/SearchesPage.svelte @@ -4,7 +4,6 @@ import receptoStore from "~/store/ReceptoStore" import Button from "~/components/buttons/Button.svelte" - import Page from "~/components/Page.svelte" import InputCollection from "~/components/fields/InputCollection.svelte" import InputText from "~/components/fields/InputText.svelte" @@ -17,36 +16,34 @@ }) - -

{$i18n.t("pages.searches.page.title")}

- -
updateSearches(searches)}> - - - - - - - -
-
\ No newline at end of file +

{$i18n.t("pages.searches.page.title")}

+ +
updateSearches(searches)}> + + + + + + + +
\ No newline at end of file diff --git a/src/pages/units/UnitsPage.svelte b/src/pages/units/UnitsPage.svelte index 90ea4dd..0df5bb5 100644 --- a/src/pages/units/UnitsPage.svelte +++ b/src/pages/units/UnitsPage.svelte @@ -4,7 +4,6 @@ import receptoStore from "~/store/ReceptoStore" import Button from "~/components/buttons/Button.svelte" - import Page from "~/components/Page.svelte" import InputCollection from "~/components/fields/InputCollection.svelte" import InputText from "~/components/fields/InputText.svelte" @@ -17,36 +16,34 @@ }) - -

{$i18n.t("pages.units.page.title")}

- -
updateUnits(units)}> - - - - - - - -
-
\ No newline at end of file +

{$i18n.t("pages.units.page.title")}

+ +
updateUnits(units)}> + + + + + + + +
\ No newline at end of file