From bb64070f69e47c14653c524d864f7a8ab8290724 Mon Sep 17 00:00:00 2001 From: Patryk Tomczyk <13100280+patzick@users.noreply.github.com> Date: Thu, 27 Apr 2023 12:40:27 +0200 Subject: [PATCH] fix(composables): proper currency and price context on SSR (#179) --- .changeset/polite-squids-flow.md | 5 +++++ packages/composables/src/usePrice.ts | 16 +++++++++------- packages/composables/src/useSessionContext.ts | 7 +++++++ 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 .changeset/polite-squids-flow.md diff --git a/.changeset/polite-squids-flow.md b/.changeset/polite-squids-flow.md new file mode 100644 index 000000000..f380f6d29 --- /dev/null +++ b/.changeset/polite-squids-flow.md @@ -0,0 +1,5 @@ +--- +"@shopware-pwa/composables-next": patch +--- + +Currency and price context are properly set during the hydration. `usePrice` is not a shared composable. diff --git a/packages/composables/src/usePrice.ts b/packages/composables/src/usePrice.ts index ff70e5a7a..ddd3dbccd 100644 --- a/packages/composables/src/usePrice.ts +++ b/packages/composables/src/usePrice.ts @@ -1,10 +1,5 @@ import { ref } from "vue"; - -const currencyLocale = ref(""); -const currencyCode = ref(""); - -// @ToDo make sure why there is no decimal precision in api response -const decimalPrecision = 2; +import { createSharedComposable } from "@vueuse/core"; export type UsePriceReturn = { /** @@ -22,7 +17,12 @@ export type UsePriceReturn = { * @public * @category Product */ -export function usePrice(): UsePriceReturn { +function _usePrice(): UsePriceReturn { + const currencyLocale = ref(""); + const currencyCode = ref(""); + + // TODO: make sure why there is no decimal precision in api response + const decimalPrecision = 2; /** * Set init data from backend response * @@ -72,3 +72,5 @@ export function usePrice(): UsePriceReturn { getFormattedPrice, }; } + +export const usePrice = createSharedComposable(_usePrice); diff --git a/packages/composables/src/useSessionContext.ts b/packages/composables/src/useSessionContext.ts index b41290c18..b01ed6bab 100644 --- a/packages/composables/src/useSessionContext.ts +++ b/packages/composables/src/useSessionContext.ts @@ -102,6 +102,13 @@ export function useSessionContext( const { apiInstance } = useShopwareContext(); const { init } = usePrice(); + if (newContext) { + init({ + currencyCode: newContext.currency?.isoCode, + localeCode: newContext.salesChannel?.language?.locale?.code, + }); + } + const _sessionContext = _useContext("swSessionContext", { replace: newContext, });