diff --git a/src/components/AddProductFiltersModal.vue b/src/components/AddProductFiltersModal.vue index e3ed9475..72ef8ead 100644 --- a/src/components/AddProductFiltersModal.vue +++ b/src/components/AddProductFiltersModal.vue @@ -14,7 +14,7 @@ - + @@ -22,8 +22,14 @@ - - +
+ + + {{ translate("Fetching filters") }} + +
+ + {{ option.label }} {{ option.label }} @@ -44,7 +50,7 @@ - +
@@ -70,28 +76,43 @@ import { IonNote, IonRow, IonSearchbar, + IonSpinner, IonTitle, IonToolbar, modalController } from "@ionic/vue"; import { closeOutline, saveOutline } from 'ionicons/icons'; import { useStore } from "vuex"; -import { UtilService } from "@/services/UtilService"; import { translate } from '@hotwax/dxp-components'; -import { hasError } from "@/utils"; -const queryString = ref(''); const facetOptions = ref([]) as any; -const isScrollable = ref(true); +const queryString = ref(''); const selectedValues = ref([]) as any; +const filteredOptions = ref([]) as any; +const availableOptions = ref([]) as any; +const pageSize = process.env.VUE_APP_VIEW_SIZE || 0; +const currentPage = ref(0); + +const isScrollable = ref(true); +const isLoading = ref(false); +const isScrollingEnabled = ref(false); +const contentRef = ref({}) as any; +const infiniteScrollRef = ref({}) as any; const props = defineProps(["label", "facetToSelect", "searchfield", "type"]); const store = useStore(); const appliedFilters = computed(() => store.getters["util/getAppliedFilters"]); +const getFacetOptions = computed(() => store.getters["util/getFacetOptions"]); -onMounted(() => { +onMounted(async() => { + isLoading.value = true; + await store.dispatch("util/fetchProductFilters", { facetToSelect: props.facetToSelect, searchfield: props.searchfield }) + facetOptions.value = getFacetOptions.value(props.searchfield); + availableOptions.value = JSON.parse(JSON.stringify(facetOptions.value)) + getFilters(); selectedValues.value = JSON.parse(JSON.stringify(appliedFilters.value[props.type][props.searchfield])) + isLoading.value = false; }) function closeModal() { @@ -99,47 +120,39 @@ function closeModal() { } function search() { - getFilters(); -} - -async function getFilters(vSize?: any, vIndex?: any) { - const viewSize = vSize ? vSize : process.env.VUE_APP_VIEW_SIZE; - const viewIndex = vIndex ? vIndex : 0; - - const payload = { - facetToSelect: props.facetToSelect, - docType: 'PRODUCT', - coreName: 'enterpriseSearch', - searchfield: props.searchfield, - jsonQuery: '{"query":"*:*","filter":["docType:PRODUCT"]}', - noConditionFind: 'N', - limit: viewSize, - q: queryString.value, - term: queryString.value, - offset: viewSize * viewIndex, - } - - const resp = await UtilService.fetchFacets(payload); - if(!hasError(resp)) { - const results = resp.data.facetResponse.response - facetOptions.value = viewIndex === 0 ? results : [...facetOptions.value , ...results]; - isScrollable.value = (facetOptions.value.length % process.env.VUE_APP_VIEW_SIZE) === 0; + isScrollable.value = true; + currentPage.value = 0; + filteredOptions.value = [] + if(queryString.value.trim()) { + availableOptions.value = facetOptions.value.filter((option: any) => option.label.toLowerCase().includes(queryString.value.trim().toLowerCase())) } else { - facetOptions.value = []; - isScrollable.value = false; + availableOptions.value = JSON.parse(JSON.stringify(facetOptions.value)) } + getFilters(); } async function loadMoreFilters(event: any){ - getFilters( - undefined, - Math.ceil(facetOptions.value.length / process.env.VUE_APP_VIEW_SIZE).toString() - ).then(() => { + // Added this check here as if added on infinite-scroll component the Loading content does not gets displayed + if(!(isScrollingEnabled.value && isScrollable.value)) { + await event.target.complete(); + } + + getFilters().then(() => { event.target.complete(); }) } +async function getFilters() { + const nextPageItems = availableOptions.value.slice(currentPage.value * pageSize, (currentPage.value + 1) * pageSize); + filteredOptions.value = filteredOptions.value.concat(nextPageItems); + currentPage.value += 1; + + if(filteredOptions.value.length >= availableOptions.value.length) { + isScrollable.value = false; + } +} + function updateSelectedValues(value: string) { selectedValues.value.includes(value) ? selectedValues.value.splice(selectedValues.value.indexOf(value), 1) : selectedValues.value.push(value); } @@ -156,6 +169,18 @@ async function saveFilters() { await store.dispatch('util/updateAppliedFilters', selectedFilters) modalController.dismiss() } + +function enableScrolling() { + const parentElement = contentRef.value.$el + const scrollEl = parentElement.shadowRoot.querySelector("div[part='scroll']") + let scrollHeight = scrollEl.scrollHeight, infiniteHeight = infiniteScrollRef.value.$el.offsetHeight, scrollTop = scrollEl.scrollTop, threshold = 100, height = scrollEl.offsetHeight + const distanceFromInfinite = scrollHeight - infiniteHeight - scrollTop - threshold - height + if(distanceFromInfinite < 0) { + isScrollingEnabled.value = false; + } else { + isScrollingEnabled.value = true; + } +}