From b9a20044d3df04370c62ab392b5144a62fbb57a9 Mon Sep 17 00:00:00 2001 From: Maciej D <7597086+mdanilowicz@users.noreply.github.com> Date: Tue, 18 Jul 2023 15:33:04 +0200 Subject: [PATCH] feat: add new link builder (#322) --- .changeset/strong-toes-add.md | 5 ++++ .changeset/wild-bees-laugh.md | 5 ++++ apps/docs/src/getting-started/languages.md | 5 ++-- .../src/useInternationalization.ts | 30 ++++++++++++++++++- templates/vue-demo-store/app.vue | 2 +- .../components/account/AccountLoginForm.vue | 3 +- .../components/account/AccountMenu.vue | 4 +-- .../components/checkout/CheckoutSideCart.vue | 5 ++-- .../components/errors/RoutingNotFound.vue | 3 +- .../components/layout/LayoutBreadcrumbs.vue | 5 ++-- .../layout/LayoutCheckoutHeader.vue | 3 +- .../components/layout/LayoutFooter.vue | 5 ++-- .../components/layout/LayoutHeader.vue | 5 ++-- .../components/layout/LayoutSideMenu.vue | 5 ++-- .../components/layout/LayoutStoreSearch.vue | 5 ++-- .../components/layout/LayoutTopNavigation.vue | 5 ++-- .../components/product/ProductCard.vue | 7 +++-- templates/vue-demo-store/layouts/account.vue | 11 +++---- .../vue-demo-store/pages/account/index.vue | 9 +++--- .../vue-demo-store/pages/account/recover.vue | 5 ++-- .../vue-demo-store/pages/checkout/cart.vue | 3 +- .../vue-demo-store/pages/checkout/index.vue | 3 +- .../pages/checkout/success/[id]/unpaid.vue | 3 +- templates/vue-demo-store/pages/error.vue | 3 +- templates/vue-demo-store/pages/login.vue | 5 ++-- .../vue-demo-store/pages/reset-password.vue | 3 +- templates/vue-demo-store/pages/wishlist.vue | 3 +- 27 files changed, 105 insertions(+), 45 deletions(-) create mode 100644 .changeset/strong-toes-add.md create mode 100644 .changeset/wild-bees-laugh.md diff --git a/.changeset/strong-toes-add.md b/.changeset/strong-toes-add.md new file mode 100644 index 000000000..2a5ba1aaa --- /dev/null +++ b/.changeset/strong-toes-add.md @@ -0,0 +1,5 @@ +--- +"vue-demo-store": minor +--- + +Replace `localePath` with `formatLink` diff --git a/.changeset/wild-bees-laugh.md b/.changeset/wild-bees-laugh.md new file mode 100644 index 000000000..8eccd88c9 --- /dev/null +++ b/.changeset/wild-bees-laugh.md @@ -0,0 +1,5 @@ +--- +"@shopware-pwa/composables-next": minor +--- + +Add `formatLink` method to the `useInternationalization` composable diff --git a/apps/docs/src/getting-started/languages.md b/apps/docs/src/getting-started/languages.md index bb7092bb9..555adecf6 100644 --- a/apps/docs/src/getting-started/languages.md +++ b/apps/docs/src/getting-started/languages.md @@ -104,15 +104,16 @@ www.example2.com // DE site ## Routing -When you are using _prefix_ domain languages, you have to use `useLocalePath()` composable for building URLs. +When you are using _prefix_ domain languages, you have to use `formatLink()` method from `useInternationalization` composable for building URLs. The main task of this composable is to add a prefix to URL if needed. ```vue ``` diff --git a/packages/composables/src/useInternationalization.ts b/packages/composables/src/useInternationalization.ts index e0693e026..02f906a97 100644 --- a/packages/composables/src/useInternationalization.ts +++ b/packages/composables/src/useInternationalization.ts @@ -61,6 +61,16 @@ export type UseInternationalizationReturn = { * Current prefix from the context */ currentPrefix: Ref; + /** + * Add prefix to the Url + * @param {string | RouteObject} link + */ + formatLink(link: string | RouteObject): string | RouteObject; +}; + +export type RouteObject = { + path: string; + [key: string]: any; }; /** @@ -68,7 +78,9 @@ export type UseInternationalizationReturn = { * @public * @category Context & Language */ -export function useInternationalization(): UseInternationalizationReturn { +export function useInternationalization( + pathResolver?: Function, +): UseInternationalizationReturn { const { devStorefrontUrl } = useShopwareContext(); const { apiInstance } = useShopwareContext(); @@ -114,6 +126,21 @@ export function useInternationalization(): UseInternationalizationReturn { : url; } + function formatLink(link: string | RouteObject) { + if (!pathResolver) return link; + + if (typeof link === "string") { + return pathResolver(link); + } + + if (link.path) { + link.path = pathResolver(link.path); + return link; + } + + return link; + } + return { getAvailableLanguages, getStorefrontUrl, @@ -121,6 +148,7 @@ export function useInternationalization(): UseInternationalizationReturn { getLanguageCodeFromId, getLanguageIdFromCode, replaceToDevStorefront, + formatLink, languages: _storeLanguages, currentLanguage: _storeCurrentLanguage, currentPrefix: _storeCurrentPrefix, diff --git a/templates/vue-demo-store/app.vue b/templates/vue-demo-store/app.vue index 7116a2174..d74bc3203 100644 --- a/templates/vue-demo-store/app.vue +++ b/templates/vue-demo-store/app.vue @@ -80,7 +80,7 @@ if (languages.value?.elements.length && router.currentRoute.value.name) { const sessionLanguage = getLanguageCodeFromId(languageIdChain.value); // If languages are not the same, set one from prefix - if (sessionLanguage !== prefix && sessionLanguage !== getDefaultLocale()) { + if (sessionLanguage !== prefix) { await changeLanguage( getLanguageIdFromCode(prefix ? prefix : getDefaultLocale()), ); diff --git a/templates/vue-demo-store/components/account/AccountLoginForm.vue b/templates/vue-demo-store/components/account/AccountLoginForm.vue index 7aa1d257a..73e8fc0a8 100644 --- a/templates/vue-demo-store/components/account/AccountLoginForm.vue +++ b/templates/vue-demo-store/components/account/AccountLoginForm.vue @@ -12,6 +12,7 @@ const { refreshSessionContext } = useSessionContext(); const { mergeWishlistProducts } = useWishlist(); const { pushSuccess } = useNotifications(); const localePath = useLocalePath(); +const { formatLink } = useInternationalization(localePath); const loginErrors = ref([]); const formData = ref({ @@ -22,7 +23,7 @@ const formData = ref({ const goToRegister = () => { emits("close"); - router.push(localePath("/register")); + router.push(formatLink("/register")); }; const invokeLogin = async (): Promise => { diff --git a/templates/vue-demo-store/components/account/AccountMenu.vue b/templates/vue-demo-store/components/account/AccountMenu.vue index ddd188197..49aeab2b9 100644 --- a/templates/vue-demo-store/components/account/AccountMenu.vue +++ b/templates/vue-demo-store/components/account/AccountMenu.vue @@ -3,7 +3,7 @@ const { isLoggedIn, logout, user } = useUser(); const loginModalController = useModal(); const localePath = useLocalePath(); - +const { formatLink } = useInternationalization(localePath); const isAccountMenuOpen = ref(false); async function invokeLogout() { @@ -79,7 +79,7 @@ async function invokeLogout() { >