diff --git a/public/snippet-script.js b/public/snippet-script.js index d97d3bae..7cb25acd 100644 --- a/public/snippet-script.js +++ b/public/snippet-script.js @@ -82,7 +82,7 @@ window.initX = { { query: 'dress', title: 'Autumn dresses by Marni', - filters: ['brand:marni', 'collection:autumn - 2022'] + filters: ['brand:marni', 'categoryIds:12fad53d7'] }, { query: 'belted legging', diff --git a/src/App.vue b/src/App.vue index 0278d5b3..877f2a0b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -61,11 +61,6 @@ return this.snippetConfig.queriesPreview; } - @XProvide('experienceControls') - public get experienceControls(): QueryPreviewInfo[] | undefined { - return this.$store.state.x.experienceControls.controls; - } - @Watch('snippetConfig.uiLang') syncLang(uiLang: string): void { this.$setLocale(uiLang); diff --git a/src/adapter/adapter.ts b/src/adapter/adapter.ts index 225b2b9f..d83818b9 100644 --- a/src/adapter/adapter.ts +++ b/src/adapter/adapter.ts @@ -6,8 +6,7 @@ import { recommendationsRequestSchema, resultSchema, semanticQueriesRequestSchema, - experienceControlsResponseSchema, - PlatformExperienceControlsResponse + experienceControlsResponseSchema } from '@empathyco/x-adapter-platform'; import { ExperienceControlsResponse, @@ -63,9 +62,10 @@ semanticQueriesRequestSchema.$override< // eslint-disable-next-line @typescript-eslint/no-unsafe-call experienceControlsResponseSchema.$override< - PlatformExperienceControlsResponse, + Partial, Partial >({ + controls: ({ controls }) => controls, events: { SemanticQueriesConfigProvided: { maxItemsToRequest: 'controls.semanticQueries.numberOfCarousels', diff --git a/src/components/column-picker.vue b/src/components/column-picker.vue index f0aa012f..ca91a75e 100644 --- a/src/components/column-picker.vue +++ b/src/components/column-picker.vue @@ -24,6 +24,7 @@ Grid4ColIcon } from '@empathyco/x-components'; import { useDevice } from '../composables/use-device.composable'; + import { useExperienceControls } from '../composables/use-experience-controls.composable'; export default defineComponent({ components: { @@ -34,8 +35,14 @@ }, setup() { const { isMobile } = useDevice(); + const { getControlFromPath } = useExperienceControls(); + + const columns = computed(() => + isMobile.value ? [2, 1] : getControlFromPath('layout.columnSelector', [4, 2]) + ); + return { - values: computed(() => (isMobile.value ? [2, 1] : [4, 2])), + values: columns, icons: { 1: 'Grid1ColIcon', 2: 'Grid2ColIcon', 4: 'Grid4ColIcon' } }; } diff --git a/src/components/search/custom-semantic-queries.vue b/src/components/search/custom-semantic-queries.vue index e183ebed..efbdf4fd 100644 --- a/src/components/search/custom-semantic-queries.vue +++ b/src/components/search/custom-semantic-queries.vue @@ -41,12 +41,12 @@ diff --git a/src/composables/use-experience-controls.composable.ts b/src/composables/use-experience-controls.composable.ts new file mode 100644 index 00000000..28bf9f10 --- /dev/null +++ b/src/composables/use-experience-controls.composable.ts @@ -0,0 +1,30 @@ +import { computed, ComputedRef } from 'vue'; +import { getSafePropertyChain } from '@empathyco/x-utils'; +import { ExperienceControlsState } from '@empathyco/x-components/experience-controls'; +import { useStore } from './use-store.composable'; + +/** + * Given a controls' object property chain, gets the experience controls values from the response. + * A defaultValue can be set as a safeguard in case the controls response was empty. + * + * @returns An object containing the function to search for a configuration + * and set the experience controls property value. + */ +export const useExperienceControls = (): { + getControlFromPath: (path: string, defaultValue?: SomeType) => ComputedRef; +} => { + const experienceControls = (useStore('experienceControls') as ExperienceControlsState).controls; + + const getControlFromPath = ( + path: string, + defaultValue?: SomeType + ): ComputedRef => { + return computed(() => { + return getSafePropertyChain(experienceControls, path, defaultValue) as SomeType; + }); + }; + + return { + getControlFromPath + }; +}; diff --git a/src/composables/use-store.composable.ts b/src/composables/use-store.composable.ts new file mode 100644 index 00000000..943ef6e5 --- /dev/null +++ b/src/composables/use-store.composable.ts @@ -0,0 +1,16 @@ +// TODO: Change when useStore composable is added in x-components +import { getCurrentInstance } from 'vue'; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import { RootXStoreState, StatusState, XModuleName, XModulesTree } from '@empathyco/x-components'; + +/** + * Provides access to the global store object or to the specific store module if it's provided. + * + * @param storeModule - The {@link XModuleName}. + * @returns A state object. + */ +export function useStore(storeModule?: keyof XModulesTree): RootXStoreState | StatusState { + return storeModule + ? getCurrentInstance()!.proxy.$root.$store.state.x[storeModule] + : getCurrentInstance()!.proxy.$root.$store.state.x; +}