Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(composables): proper currency and price context on SSR #179

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-squids-flow.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 9 additions & 7 deletions packages/composables/src/usePrice.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { ref } from "vue";

const currencyLocale = ref<string>("");
const currencyCode = ref<string>("");

// @ToDo make sure why there is no decimal precision in api response
const decimalPrecision = 2;
import { createSharedComposable } from "@vueuse/core";

export type UsePriceReturn = {
/**
Expand All @@ -22,7 +17,12 @@ export type UsePriceReturn = {
* @public
* @category Product
*/
export function usePrice(): UsePriceReturn {
function _usePrice(): UsePriceReturn {
const currencyLocale = ref<string>("");
const currencyCode = ref<string>("");

// TODO: make sure why there is no decimal precision in api response
const decimalPrecision = 2;
/**
* Set init data from backend response
*
Expand Down Expand Up @@ -72,3 +72,5 @@ export function usePrice(): UsePriceReturn {
getFormattedPrice,
};
}

export const usePrice = createSharedComposable(_usePrice);
7 changes: 7 additions & 0 deletions packages/composables/src/useSessionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down