Skip to content

Commit

Permalink
Merge pull request #99 from hotwax/#2dmrnhx
Browse files Browse the repository at this point in the history
Implemented: added support to pass operator to product query (#2dmrnhx)
  • Loading branch information
adityasharma7 authored May 27, 2022
2 parents c1d3d91 + cd6d37a commit e3fe574
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/components/ProductFilterModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<ion-item v-for="option in facetOptions" :key="option.id">
<ion-label>{{ option.label }}</ion-label>
<!-- Added key on checkbox as when clicking on the checkbox the checked value is changed but not reflected on UI -->
<ion-checkbox v-if="!isAlreadyApplied(option.id)" :checked="appliedFilters[type][searchfield].includes(option.id)" :key="appliedFilters[type][searchfield].includes(option.id)" @click="updateFilter(option.id)"/>
<ion-checkbox v-if="!isAlreadyApplied(option.id)" :checked="appliedFilters[type][searchfield].list.includes(option.id)" :key="appliedFilters[type][searchfield].list.includes(option.id)" @click="updateFilter(option.id)"/>
<ion-note v-else slot="end" color="danger">{{ type === 'included' ? $t("excluded") : $t("included") }}</ion-note>
</ion-item>
</ion-list>
Expand Down Expand Up @@ -126,7 +126,7 @@ export default defineComponent({
},
isAlreadyApplied(value: string) {
const type = this.type === 'included' ? 'excluded' : 'included'
return this.appliedFilters[type][this.searchfield].includes(value)
return this.appliedFilters[type][this.searchfield].list.includes(value)
}
},
setup() {
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"No previous occurrence": "No previous occurrence",
"No time zone found": "No time zone found",
"Only show selected products": "Only show selected products",
"Operator": "Operator",
"Password": "Password",
"Pending": "Pending",
"Please enter a threshold value to set for these products before proceeding.": "Please enter a threshold value to set for these products before proceeding.",
Expand Down
10 changes: 8 additions & 2 deletions src/store/modules/product/ProductState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ export default interface ProductState {
};
appliedFilters: {
included: {
tags: Array<string>
tags: {
list: any;
operator: string;
};
},
excluded: {
tags: Array<string>
tags: {
list: any;
operator: string;
};
}
};
query: any;
Expand Down
18 changes: 13 additions & 5 deletions src/store/modules/product/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,19 @@ const actions: ActionTree<ProductState, RootState> = {
async updateAppliedFilters({ commit, state, dispatch }, payload) {
const value = payload.value
const appliedFilters = JSON.parse(JSON.stringify((state.appliedFilters as any)[payload.type][payload.id]))
appliedFilters.includes(value) ? appliedFilters.splice(appliedFilters.indexOf(value), 1) : appliedFilters.push(value)
appliedFilters.list.includes(value) ? appliedFilters.list.splice(appliedFilters.list.indexOf(value), 1) : appliedFilters.list.push(value)
commit(types.PRODUCT_FILTER_UPDATED, {id: payload.id, type: payload.type, value: appliedFilters})
dispatch('updateQuery')
},

async updateAppliedFilterOperator({ commit, state, dispatch }, payload) {
const appliedFilters = JSON.parse(JSON.stringify((state.appliedFilters as any)[payload.type][payload.id]))
appliedFilters.operator = payload.value;
commit(types.PRODUCT_FILTER_UPDATED, {id: payload.id, type: payload.type, value: appliedFilters})
// If we have list items then only apply filters again
if(appliedFilters.list.length) dispatch('updateQuery')
},

async updateQuery({ commit, dispatch, state }, payload) {
// initializing the filter always on updateQuery call because we are adding values in the filter
// as string and if some value is removed then we need to do multiple operations on the filter string
Expand Down Expand Up @@ -88,16 +96,16 @@ const actions: ActionTree<ProductState, RootState> = {

state.query.json['filter'] = Object.keys(state.appliedFilters.included).reduce((filter, value) => {
const filterValues = (state.appliedFilters.included as any)[value]
if (filterValues.length > 0) {
filter.push(`${value}: ("${filterValues.join('" OR "')}")`)
if (filterValues.list.length > 0) {
filter.push(`${value}: ("${filterValues.list.join('" ' + filterValues.operator + ' "')}")`)
}
return filter
}, state.query.json['filter'])

state.query.json['filter'] = Object.keys(state.appliedFilters.excluded).reduce((filter, value) => {
const filterValues = (state.appliedFilters.excluded as any)[value]
if (filterValues.length > 0) {
filter.push(`-${value}: ("${filterValues.join('" OR "')}")`)
if (filterValues.list.length > 0) {
filter.push(`-${value}: ("${filterValues.list.join('" ' + filterValues.operator + ' "')}")`)
}
return filter
}, state.query.json['filter'])
Expand Down
10 changes: 8 additions & 2 deletions src/store/modules/product/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ const productModule: Module<ProductState, RootState> = {
},
appliedFilters: {
included: {
tags: []
tags: {
list: [],
operator: "OR"
},
},
excluded: {
tags: []
tags: {
list: [],
operator: "OR"
},
}
},
query: {
Expand Down
62 changes: 42 additions & 20 deletions src/views/SelectProduct.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@
</div>
</ion-list-header>
<ion-card>
<ion-toolbar>
<ion-item lines="none">
<ion-label>{{ $t("Tags") }}</ion-label>
<ion-button fill="clear" slot="end" size="small" @click="searchFilter('tags', 'tagsFacet', 'tags', 'included')">
<ion-label>{{ $t('add') }}</ion-label>
<ion-icon :icon="addCircleOutline" />
</ion-button>
</ion-item>
</ion-toolbar>
<ion-item lines="none">
<ion-label>{{ $t("Tags") }}</ion-label>
<ion-button fill="clear" slot="end" size="small" @click="searchFilter('tags', 'tagsFacet', 'tags', 'included')">
<ion-label>{{ $t('add') }}</ion-label>
<ion-icon :icon="addCircleOutline" />
</ion-button>
</ion-item>
<ion-item lines="none">
<ion-label>{{ $t("Operator") }}</ion-label>
<ion-select interface="popover" @ionChange="applyOperator('included', 'tags', $event.detail.value)" :value="appliedFilters['included']['tags'].operator">
<ion-select-option :value="AND">AND</ion-select-option>
<ion-select-option :value="OR">OR</ion-select-option>
</ion-select>
</ion-item>
<ion-card-content>
<ion-chip v-for="(tag, index) in appliedFilters['included']['tags']" :key="index">
<ion-chip v-for="(tag, index) in appliedFilters['included']['tags'].list" :key="index">
<ion-icon :icon="pricetagOutline" />
<ion-label>{{ tag }}</ion-label>
<ion-icon :icon="closeCircle" @click="removeFilters('included', 'tags', tag)"/>
Expand All @@ -57,17 +62,22 @@
</div>
</ion-list-header>
<ion-card>
<ion-toolbar>
<ion-item lines="none">
<ion-label>{{ $t("Tags") }}</ion-label>
<ion-button fill="clear" slot="end" size="small" @click="searchFilter('tags', 'tagsFacet', 'tags', 'excluded')">
<ion-label>{{ $t('add') }}</ion-label>
<ion-icon :icon="addCircleOutline" />
</ion-button>
</ion-item>
</ion-toolbar>
<ion-item lines="none">
<ion-label>{{ $t("Tags") }}</ion-label>
<ion-button fill="clear" slot="end" size="small" @click="searchFilter('tags', 'tagsFacet', 'tags', 'excluded')">
<ion-label>{{ $t('add') }}</ion-label>
<ion-icon :icon="addCircleOutline" />
</ion-button>
</ion-item>
<ion-item lines="none">
<ion-label>{{ $t("Operator") }}</ion-label>
<ion-select @ionChange="applyOperator('excluded', 'tags', $event.detail.value)" interface="popover" :value="appliedFilters['excluded']['tags'].operator">
<ion-select-option :value="AND">AND</ion-select-option>
<ion-select-option :value="OR">OR</ion-select-option>
</ion-select>
</ion-item>
<ion-card-content>
<ion-chip v-for="(tag, index) in appliedFilters['excluded']['tags']" :key="index">
<ion-chip v-for="(tag, index) in appliedFilters['excluded']['tags'].list" :key="index">
<ion-icon :icon="pricetagOutline" />
<ion-label>{{ tag }}</ion-label>
<ion-icon :icon="closeCircle" @click="removeFilters('excluded', 'tags', tag)"/>
Expand Down Expand Up @@ -163,6 +173,8 @@ import {
IonMenuButton,
IonPage,
IonSearchbar,
IonSelect,
IonSelectOption,
IonTitle,
IonToolbar,
modalController,
Expand Down Expand Up @@ -199,6 +211,8 @@ export default defineComponent({
IonMenuButton,
IonPage,
IonSearchbar,
IonSelect,
IonSelectOption,
IonTitle,
IonToolbar,
Image
Expand Down Expand Up @@ -285,6 +299,14 @@ export default defineComponent({
})
this.queryString = ''
},
async applyOperator(type: string, id: string, value: string) {
await this.store.dispatch('product/updateAppliedFilterOperator', {
type,
id,
value
})
this.queryString = ''
},
async resetFilters(type: string) {
// checking that if any of the current type does not have any attribute selected than not making solr query
if (Object.entries(this.appliedFilters[type]).every((filter: any) => filter[1].length <= 0)) {
Expand Down

0 comments on commit e3fe574

Please sign in to comment.