From 08e9b955676f688986a35c7ea9e6f45d0cf52f11 Mon Sep 17 00:00:00 2001 From: Bartosz Herba Date: Tue, 8 Feb 2022 21:21:22 +0100 Subject: [PATCH 1/2] refactor(theme): move stores to pinia store --- packages/theme/components/CurrencySelector.vue | 2 -- packages/theme/components/StoreSwitcher.vue | 4 ---- packages/theme/composables/useConfig/index.ts | 4 ++-- .../theme/composables/useMagentoConfiguration.ts | 4 ++-- packages/theme/composables/useStore/index.ts | 11 ++++++++--- packages/theme/composables/useStore/useStore.d.ts | 2 +- packages/theme/stores/config.ts | 14 +++++++++----- 7 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/theme/components/CurrencySelector.vue b/packages/theme/components/CurrencySelector.vue index e834cd773..bbbafda19 100644 --- a/packages/theme/components/CurrencySelector.vue +++ b/packages/theme/components/CurrencySelector.vue @@ -44,7 +44,6 @@ import { useCurrency, } from '@vue-storefront/magento'; import { - SfImage, SfButton, SfList, SfBottomModal, @@ -61,7 +60,6 @@ import { useHandleChanges } from '~/helpers/magentoConfig/handleChanges'; export default defineComponent({ name: 'CurrencySelector', components: { - SfImage, SfButton, SfList, SfBottomModal, diff --git a/packages/theme/components/StoreSwitcher.vue b/packages/theme/components/StoreSwitcher.vue index 28f422e2d..104a1cb85 100644 --- a/packages/theme/components/StoreSwitcher.vue +++ b/packages/theme/components/StoreSwitcher.vue @@ -77,7 +77,6 @@ import { import { ref, - computed, defineComponent, } from '@nuxtjs/composition-api'; @@ -99,15 +98,12 @@ export default defineComponent({ const { stores, - load: loadStores, change: changeStore, } = useStore(); const { handleChanges } = useHandleChanges(); const isLangModalOpen = ref(false); - loadStores(); - return { availableStores: stores, changeStore, diff --git a/packages/theme/composables/useConfig/index.ts b/packages/theme/composables/useConfig/index.ts index f96a21a81..42d820e27 100644 --- a/packages/theme/composables/useConfig/index.ts +++ b/packages/theme/composables/useConfig/index.ts @@ -16,7 +16,7 @@ const useConfig = (): UseConfig => { try { const { data } = await app.$vsf.$magento.api.storeConfig(); configStore.$patch((state) => { - state.config = data.storeConfig || {}; + state.storeConfig = data.storeConfig || {}; }); } catch (err) { Logger.debug(err); @@ -26,7 +26,7 @@ const useConfig = (): UseConfig => { }; return { - config: computed(() => configStore.config), + config: computed(() => configStore.storeConfig), loading, loadConfig, }; diff --git a/packages/theme/composables/useMagentoConfiguration.ts b/packages/theme/composables/useMagentoConfiguration.ts index a94a773c9..12a8d91d7 100644 --- a/packages/theme/composables/useMagentoConfiguration.ts +++ b/packages/theme/composables/useMagentoConfiguration.ts @@ -1,6 +1,5 @@ import { useCurrency, - useStore, storeConfigGetters, Currency, AvailableStores, @@ -9,7 +8,7 @@ import { import { computed, ComputedRef, useContext } from '@nuxtjs/composition-api'; import cookieNames from '~/enums/cookieNameEnum'; -import { useConfig } from '~/composables'; +import { useConfig, useStore } from '~/composables'; type UseMagentoConfiguration = () => { currencies: ComputedRef; @@ -20,6 +19,7 @@ type UseMagentoConfiguration = () => { selectedStore: ComputedRef; loadConfiguration: (params: { updateCookies: boolean; updateLocale: boolean; }) => Promise; }; + // @ts-ignore export const useMagentoConfiguration: UseMagentoConfiguration = () => { const { app } = useContext(); diff --git a/packages/theme/composables/useStore/index.ts b/packages/theme/composables/useStore/index.ts index 39a23a84c..733a28975 100644 --- a/packages/theme/composables/useStore/index.ts +++ b/packages/theme/composables/useStore/index.ts @@ -1,15 +1,18 @@ import { + computed, ref, Ref, useContext, } from '@nuxtjs/composition-api'; import { Logger } from '@vue-storefront/core'; import { StoreConfig } from '@vue-storefront/magento-api'; import { storeConfigGetters } from '@vue-storefront/magento'; import { UseStoreInterface, UseStore, UseStoreErrors } from '~/composables/useStore/useStore'; +import { useConfigStore } from '~/stores/config'; const useStore: UseStore = (): UseStoreInterface => { const loading: Ref = ref(false); const error: Ref = ref({ load: null, change: null }); - const stores: Ref = ref([]); + const configStore = useConfigStore(); + const { app } = useContext(); const load = async (customQuery = { availableStores: 'availableStores' }): Promise => { @@ -20,7 +23,9 @@ const useStore: UseStore = (): UseStoreInterface => { loading.value = true; const { data } = await app.$vsf.$magento.api.availableStores(customQuery); - stores.value = data.availableStores || []; + configStore.$patch((state) => { + state.stores = data?.availableStores ?? []; + }); } catch (err) { error.value.load = err; } finally { @@ -48,7 +53,7 @@ const useStore: UseStore = (): UseStoreInterface => { return { load, change, - stores, + stores: computed(() => configStore.stores), loading, error, }; diff --git a/packages/theme/composables/useStore/useStore.d.ts b/packages/theme/composables/useStore/useStore.d.ts index 6e88e193b..fca673e9e 100644 --- a/packages/theme/composables/useStore/useStore.d.ts +++ b/packages/theme/composables/useStore/useStore.d.ts @@ -4,7 +4,7 @@ import { UseStoreErrors } from '@vue-storefront/core'; export interface UseStoreInterface { change(store: StoreConfig): void; - load(customQuery: { availableStores: string }): Promise; + load(customQuery?: { availableStores: string }): Promise; stores: Ref, loading: Ref, error: Ref, diff --git a/packages/theme/stores/config.ts b/packages/theme/stores/config.ts index 3477b7d9a..21324b02f 100644 --- a/packages/theme/stores/config.ts +++ b/packages/theme/stores/config.ts @@ -1,10 +1,14 @@ import { defineStore } from 'pinia'; -import { StoreConfig } from '@vue-storefront/magento'; +import { AvailableStores, StoreConfig } from '@vue-storefront/magento'; -const config: StoreConfig = {}; +interface ConfigState { + storeConfig: StoreConfig, + stores: AvailableStores +} -export const useConfigStore = defineStore('storeConfig', { - state: () => ({ - config, +export const useConfigStore = defineStore('magentoConfig', { + state: (): ConfigState => ({ + storeConfig: {}, + stores: [], }), }); From f68855bef26ae989ec99e6ae2165ea750fbd90ef Mon Sep 17 00:00:00 2001 From: Bartosz Herba Date: Wed, 9 Feb 2022 09:51:08 +0100 Subject: [PATCH 2/2] refactor(theme): refactor useCurrency composable BREAKING CHANGES: - mark useCurrency as a deprecated in composables module - add refactored and lightweight useCurrency in theme with usage of pinia store management --- packages/composables/nuxt/plugin.js | 1 + .../src/composables/useCurrency/index.ts | 5 ++ .../src/factories/useCurrencyFactory.ts | 5 ++ .../theme/components/CurrencySelector.vue | 8 +-- packages/theme/composables/index.ts | 1 + packages/theme/composables/useConfig/index.ts | 22 ++++--- .../composables/useConfig/useConfig.d.ts | 6 +- .../theme/composables/useCurrency/index.ts | 63 +++++++++++++++++++ .../composables/useCurrency/useCurrency.d.ts | 16 +++++ .../composables/useMagentoConfiguration.ts | 38 ++++++----- packages/theme/composables/useStore/index.ts | 4 +- packages/theme/stores/config.ts | 4 +- 12 files changed, 140 insertions(+), 33 deletions(-) create mode 100644 packages/theme/composables/useCurrency/index.ts create mode 100644 packages/theme/composables/useCurrency/useCurrency.d.ts diff --git a/packages/composables/nuxt/plugin.js b/packages/composables/nuxt/plugin.js index 00a24f3f1..0a9fc943f 100644 --- a/packages/composables/nuxt/plugin.js +++ b/packages/composables/nuxt/plugin.js @@ -5,6 +5,7 @@ import cookie from '@vue-storefront/magento/nuxt/cookie'; const moduleOptions = JSON.parse('<%= JSON.stringify(options) %>'); +// TODO should be moved to THEME and expose consistent cookie management API export default integrationPlugin(({ app, res, req, integration }) => { const cartCookieName = moduleOptions.cookies?.cartCookieName || defaultConfig.cookies.cartCookieName; const customerCookieName = moduleOptions.cookies?.customerCookieName || defaultConfig.cookies.customerCookieName; diff --git a/packages/composables/src/composables/useCurrency/index.ts b/packages/composables/src/composables/useCurrency/index.ts index f117b04fe..b5a43346d 100644 --- a/packages/composables/src/composables/useCurrency/index.ts +++ b/packages/composables/src/composables/useCurrency/index.ts @@ -1,3 +1,8 @@ +/** + * @deprecated since version + * + * @see + */ import { Context } from '@vue-storefront/core'; import { Currency } from '@vue-storefront/magento-api'; import { useCurrencyFactory, UseCurrencyFactoryParams } from '../../factories/useCurrencyFactory'; diff --git a/packages/composables/src/factories/useCurrencyFactory.ts b/packages/composables/src/factories/useCurrencyFactory.ts index 17e00874a..d93452d0a 100644 --- a/packages/composables/src/factories/useCurrencyFactory.ts +++ b/packages/composables/src/factories/useCurrencyFactory.ts @@ -1,3 +1,8 @@ +/** + * @deprecated since version + * + * @see + */ import { computed } from '@vue/composition-api'; import { Context, diff --git a/packages/theme/components/CurrencySelector.vue b/packages/theme/components/CurrencySelector.vue index bbbafda19..f359c9dcd 100644 --- a/packages/theme/components/CurrencySelector.vue +++ b/packages/theme/components/CurrencySelector.vue @@ -40,9 +40,6 @@