-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow queries preview with same query but different filters or params #1392
feat: allow queries preview with same query but different filters or params #1392
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also tested with:{ query: 'wallet', filters: ['brand:valentino']},
as a queryPreview + searching for charger
to check that Semantic Queries will show results for the query wallet
without being filtered.
packages/x-components/src/x-modules/queries-preview/components/query-preview.vue
Outdated
Show resolved
Hide resolved
packages/x-components/src/x-modules/queries-preview/components/query-preview.vue
Outdated
Show resolved
Hide resolved
packages/x-components/src/x-modules/queries-preview/components/query-preview.vue
Outdated
Show resolved
Hide resolved
packages/x-components/src/x-modules/queries-preview/events.types.ts
Outdated
Show resolved
Hide resolved
packages/x-components/src/x-modules/queries-preview/utils/get-hash-from-query-preview.ts
Show resolved
Hide resolved
* | ||
* @internal | ||
*/ | ||
public queryOfQueryPreviewHash = getHashFromQueryPreviewInfo(this.queryPreviewInfo); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to queryPreviewHash
* | ||
* @internal | ||
*/ | ||
public queryOfQueryPreviewHash = getHashFromQueryPreviewInfo(this.queryPreviewInfo); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This uses a dynamic value to generate a non-dynamic value. Make it a computed property.
/** | ||
* The query preview has been mounted. | ||
* Payload: The query preview query as a key converted into a unique id (query hash). | ||
* When QueryPreviewMounted is emitted, the instances count is increased. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to explain this here. What the module does when the event fires is not relevant here.
* When QueryPreviewUnmounted is emitted, the instance count for that query hash | ||
* will be decreased. If the cache is set to false, the query will be removed | ||
* safely from the state since there will be no component showing that data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
@@ -41,7 +42,21 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { | |||
state.selectedQueryPreview = selectedQueryPreview; | |||
}, | |||
setConfig, | |||
mergeConfig | |||
mergeConfig, | |||
addQueryPreviewInstance(state, query: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the query
, is the queryPreviewHash
let queryPreviewFilters = ''; | ||
if (queryPreviewInfo.filters) { | ||
queryPreviewInfo.filters.forEach(filter => { | ||
queryPreviewFilters = queryPreviewFilters.concat('-' + filter); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can refactor to this:
let queryPreviewFilters = ''; | |
if (queryPreviewInfo.filters) { | |
queryPreviewInfo.filters.forEach(filter => { | |
queryPreviewFilters = queryPreviewFilters.concat('-' + filter); | |
}); | |
} | |
const queryPreviewFilters = queryPreviewInfo.filters ? queryPreviewInfo.filters.join('-') : ''; |
let queryPreviewFilters = ''; | ||
if (queryPreview.request.filters) { | ||
const queryPreviewFiltersKeys = Object.keys(queryPreview.request.filters); | ||
queryPreviewFiltersKeys.forEach(key => { | ||
queryPreview.request.filters![key].forEach(filter => { | ||
queryPreviewFilters = queryPreviewFilters.concat('-' + filter.id.toString()); | ||
}); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can refactor this to:
let queryPreviewFilters = ''; | |
if (queryPreview.request.filters) { | |
const queryPreviewFiltersKeys = Object.keys(queryPreview.request.filters); | |
queryPreviewFiltersKeys.forEach(key => { | |
queryPreview.request.filters![key].forEach(filter => { | |
queryPreviewFilters = queryPreviewFilters.concat('-' + filter.id.toString()); | |
}); | |
}); | |
} | |
const queryPreviewFilters = Object.values(queryPreview.request.filters) | |
.flat() | |
.map(filter => filter.id.toString()) | |
.join('-'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the QP in the tests have filters. Half of the implementation is not being tested right now
this.queriesStatus[query] === 'success' || this.queriesStatus[query] === 'loading' | ||
); | ||
return this.queriesPreviewInfo.filter(item => { | ||
const query = getHashFromQueryPreviewInfo(item); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to queryPreviewHash
@@ -86,7 +86,9 @@ describe('testing QueryPreviewList', () => { | |||
expect(queryPreviews.at(1).text()).toEqual('jeans - Sick jeans'); | |||
}); | |||
|
|||
it('hides queries with no results', async () => { | |||
// TODO Uncomment when the 'error' event is fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's a task already, add the task ID here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reference task EMP-3402
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the conflicts with main too
addQueryPreviewInstance(state, query: string) { | ||
if (state.queriesPreview[query]) { | ||
state.queriesPreview[query].instances += 1; | ||
addQueryPreviewInstance(state, queryPreviewHash: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to specify the type in queryPreviewHash: string
, it's already being done in the types file, remove the string
.
state.queriesPreview[query].instances -= 1; | ||
removeQueryPreviewInstance( | ||
state, | ||
{ queryPreviewHash, cache }: { queryPreviewHash: string; cache: boolean } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, you don't need the type in { queryPreviewHash, cache }: { queryPreviewHash: string; cache: boolean }
, leave it as { queryPreviewHash, cache }
@@ -111,8 +111,14 @@ export interface QueriesPreviewMutations extends ConfigMutations<QueriesPreviewS | |||
* @param selectedQueryPreview - The selected query preview to save to the state. | |||
*/ | |||
setSelectedQueryPreview(selectedQueryPreview: QueryPreviewInfo | null): void; | |||
addQueryPreviewInstance(query: string): void; | |||
removeQueryPreviewInstance({ query, cache }: { query: string; cache: boolean }): void; | |||
addQueryPreviewInstance(queryPreviewHash: string): void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add docs
addQueryPreviewInstance(query: string): void; | ||
removeQueryPreviewInstance({ query, cache }: { query: string; cache: boolean }): void; | ||
addQueryPreviewInstance(queryPreviewHash: string): void; | ||
removeQueryPreviewInstance({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add docs
@@ -86,7 +86,9 @@ describe('testing QueryPreviewList', () => { | |||
expect(queryPreviews.at(1).text()).toEqual('jeans - Sick jeans'); | |||
}); | |||
|
|||
it('hides queries with no results', async () => { | |||
// TODO Uncomment when the 'error' event is fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reference task EMP-3402
Pull request template
In the current implementation, we are only saving queries. As a second step, we should also support filters and extraparams, but then we should save the hash of query+filters.
We have used
js-md5
hash to make an id to differentiate the same queries but with different filters applied.There was a problem when dealing with multiple instances of the
QueryPreview
component with the same query hash. In that case, the order of mounting and unmounting the components matter and it could lead to a scenario where a component is mounted but afterwards, the other one with the same query hash is unmounted and both of them will be gone from the DOM.We have applied the following SOLUTION:
Refactor the events emitted from
QueryPreview
. RemoveNonCacheableQueryPreviewUnmounted
and addQueryPreviewMounted: string;
QueryPreviewUnmounted: { query: string, cache: boolean };
Enhance the
QueryPreviewItem
from the state with a fieldThe
fetchAndSaveQueryPreview
action will set the instances field to 1 since it will be the first instance of aQueryPreview
using this query hash.Every
QueryPreview
instance when is created and picks the data from the cache will emitQueryPreviewMounted
so the instances count is increased. Every instance ofQueryPreview
will emitQueryPreviewUnmounted
so the instances count for that query hash will be decreased and if the cache is set to false, then the query will be removed safely from the state since there will be no component showing that data.Motivation and context
Type of change
What is the destination branch of this PR?
Main
How has this been tested?
You can test 2 scenarios:
In scenario 1 --> you can add in
Home.vue
file this new query preview inqueriesPreviewInfo
:You should see 2 carrousels with 'summer dress' as the title but with different results.
In scenario 2 --> add in the same place as the previous scenario the following query preview:
Then search for
battery
in the search box. This query has the querycharger
as a semantic query.As we had saved
charger
in the state from the brand recommendations, in semantic queries should be loaded from the state instead of being requested again.Once we clear the query in the search box, we should see that
charger
keeps saved in the query preview state and is displayed as a brand recommendation.Checklist: