Skip to content
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

Set cache on queries preview results #1360

Merged
merged 27 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
747a4de
feat(query-preview): Add updateQueryPreviewHistory action/commits/types
annacv Nov 16, 2023
766639e
feat(query-preview): Call updateQueryPreviewHistory action & fetch on…
annacv Nov 16, 2023
101c6ce
feat(query-preview): Use deepEqual in updateQueryPreviewHistory action
annacv Nov 17, 2023
492815b
feat(query-preview): wip render query preview from history
annacv Nov 17, 2023
f398456
feat(query-preview): fix removeFromQueryPreviewHistory
annacv Nov 20, 2023
3b2ccd3
feat(query-preview): rm popQueryPreviewHistory as it doesn't work pro…
annacv Nov 20, 2023
5a7d661
feat(query-preview): update event payload to suit deleting both from …
annacv Nov 20, 2023
b1b0394
feat(query-preview-list): make clearOnDestroy accessible to pass it a…
annacv Nov 20, 2023
98951e8
doc(query-preview): update queryPreviewHistory description
annacv Nov 20, 2023
f28778f
feat(query-preview): check if the item has been saved in the componen…
annacv Nov 27, 2023
69d09ad
feat(query-preview): rm first queryPreviewHistory item if the list le…
annacv Nov 27, 2023
5d723ae
feat(query-preview): save only queries in the history list
annacv Nov 27, 2023
4692d20
chore: check if loadedQueryPreview exists
annacv Nov 27, 2023
e9ae00a
feat: rollback query as payload in QueryPreviewUnmountedHook event
annacv Nov 28, 2023
c9ddcc1
test: Add updateQueryPreviewHistory action test
annacv Nov 28, 2023
db38849
Update packages/x-components/src/x-modules/queries-preview/components…
annacv Nov 28, 2023
18d11a5
feat: Set cleanOnDestroy false as default
annacv Nov 28, 2023
3ee5177
feat(query-preview): remove queryPreviewHistory list
lauramargar Dec 5, 2023
1b668c6
chore: fix method documentation
lauramargar Dec 5, 2023
42f9c44
feat: update and fix query-preview tests
lauramargar Dec 6, 2023
4f32b65
feat: add test when saveCache is true
lauramargar Dec 6, 2023
56ddc60
feat: complete test implementation
lauramargar Dec 6, 2023
e49867e
chore: fix pr comments
lauramargar Dec 6, 2023
c948340
chore: fix pr comments
lauramargar Dec 6, 2023
ed0c717
feat: update tests and check status in cache
lauramargar Dec 7, 2023
583b30b
chore: fix pr comments
lauramargar Dec 7, 2023
5f2913d
chore: fix query preview test
lauramargar Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/x-components/src/views/home/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
:queries-preview-info="queries.map(q => ({ query: q }))"
#default="{ queryPreviewInfo: { query }, results }"
queryFeature="semantics"
:clearOnDestroy="true"
>
<div
class="x-flex x-flex-col x-gap-8 x-mb-16"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@error="flagAsFailed"
v-bind="$attrs"
:queryPreviewInfo="queryPreview"
:clearOnDestroy="clearOnDestroy"
>
<template v-for="(_, slotName) in $scopedSlots" v-slot:[slotName]="scope">
<slot :name="slotName" v-bind="scope" />
Expand Down Expand Up @@ -65,6 +66,15 @@
@Prop({ required: true })
public queriesPreviewInfo!: QueryPreviewInfo[];

/**
* Controls whether the QueryPreview should be removed from the state
* when the component is destroyed.
*
* @public
*/
@Prop({ default: false })
annacv marked this conversation as resolved.
Show resolved Hide resolved
public clearOnDestroy!: boolean;

/**
* Contains the status of the preview requests, indexed by query.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
*
* @public
*/
@Prop({ default: true })
@Prop({ default: false })
public clearOnDestroy!: boolean;

/**
Expand All @@ -128,6 +128,13 @@
@State('queriesPreview', 'config')
public config!: QueriesPreviewConfig;

/**
* The queryPreviewHistory of the queries that have been searched.
* It is a list of queries.
*/
@State('queriesPreview', 'queryPreviewHistory')
public queryPreviewHistory!: string[];

/**
* The results to render from the state.
*
Expand Down Expand Up @@ -208,6 +215,21 @@
}, this.debounceTimeMs);
}

/**
* Checks whether the current queryPreviewItem query has been saved
* in the queryPreviewHistory in the state.
*
* @internal
*
* @returns True if the query has been saved.
*/
protected get isSavedQuery(): boolean {
const previewItemQuery = this.previewResults[this.queryPreviewInfo.query];
return previewItemQuery
? this.queryPreviewHistory.some(item => item === previewItemQuery.request.query)
: false;
}

/**
* Initialises watcher to emit debounced requests, and first value for the requests.
*
Expand All @@ -222,7 +244,13 @@
}
}
);
this.emitQueryPreviewRequestUpdated(this.queryPreviewRequest);

// If the query has been saved it will emit load instead of the emitting the updated request.
if (this.isSavedQuery) {
this.$emit('load', this.queryPreviewInfo.query);
} else {
this.emitQueryPreviewRequestUpdated(this.queryPreviewRequest);
}
}

/**
Expand All @@ -235,7 +263,6 @@
*/
protected beforeDestroy(): void {
this.emitQueryPreviewRequestUpdated.cancel();

if (this.clearOnDestroy) {
this.$x.emit('QueryPreviewUnmountedHook', this.queryPreviewInfo.query, {
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
priority: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ export interface QueriesPreviewConfig {
* Maximum number of items to request.
*/
maxItemsToRequest: number;
/**
* Maximum number of most recent used queries preview.
*/
maxQueryPreviewHistoryLength: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ import { QueriesPreviewXStoreModule } from '../types';
export const fetchAndSaveQueryPreview: QueriesPreviewXStoreModule['actions']['fetchAndSaveQueryPreview'] =
({ dispatch, commit }, request) => {
const { query } = request;

if (!query) {
return;
}

commit('setQueryPreview', {
request,
results: [],
status: 'loading',
totalResults: 0
});

return dispatch('fetchQueryPreview', request)
.then(response => {
commit('setQueryPreview', {
Expand All @@ -36,5 +39,8 @@ export const fetchAndSaveQueryPreview: QueriesPreviewXStoreModule['actions']['fe
// eslint-disable-next-line no-console
console.error(error);
commit('setStatus', { query, status: 'error' });
})
.then(() => {
return dispatch('updateQueryPreviewHistory', request);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { QueriesPreviewXStoreModule } from '../types';

/**
* Default implementation for the {@link QueriesPreviewActions.updateQueryPreviewHistory}.
*
* @param _context - The {@link https://vuex.vuejs.org/guide/actions.html | context}
* of the actions, provided by Vuex.
* @param request - The query preview request.
*
* @public
*/
// eslint-disable-next-line max-len
export const updateQueryPreviewHistory: QueriesPreviewXStoreModule['actions']['updateQueryPreviewHistory'] =
({ commit, state, getters }, request) => {
const { query } = request;
const loadedQueryPreview = getters.loadedQueriesPreview[query]?.request?.query ?? undefined;

// If the query preview was already stored, remove the old one.
if (loadedQueryPreview && state.queryPreviewHistory.some(item => item === loadedQueryPreview)) {
commit('clearFromQueryPreviewHistory', query);
}

// If the queryPreviewHistory list exceeds the configured max.length to store,
// remove the first item from the list and from the QueriesPreview state.
if (state.queryPreviewHistory.length === state.config.maxQueryPreviewHistoryLength) {
commit('shiftQueryPreviewHistory');
commit(
'clearQueryPreview',
state.queriesPreview[Object.keys(state.queriesPreview)[0]].request.query
);
}

// Add the query preview item query to the queryPreviewHistory.
commit('setQueryPreviewHistory', query);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { QueriesPreviewXStoreModule } from './types';
import { fetchQueryPreview } from './actions/fetch-query-preview.action';
import { fetchAndSaveQueryPreview } from './actions/fetch-and-save-query-preview.action';
import { loadedQueriesPreview } from './getters/loaded-queries-preview.getter';
import { updateQueryPreviewHistory } from './actions/update-query-preview-history.action';

/**
* {@link XStoreModule} For the `queries-preview` module.
Expand All @@ -13,9 +14,11 @@ import { loadedQueriesPreview } from './getters/loaded-queries-preview.getter';
export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = {
state: () => ({
config: {
maxItemsToRequest: 24
maxItemsToRequest: 24,
maxQueryPreviewHistoryLength: 5
},
queriesPreview: {},
queryPreviewHistory: [],
selectedQueryPreview: {
query: '',
extraParams: undefined,
Expand All @@ -41,10 +44,20 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = {
state.selectedQueryPreview = selectedQueryPreview;
},
setConfig,
mergeConfig
mergeConfig,
setQueryPreviewHistory(state, query) {
state.queryPreviewHistory.push(query);
},
clearFromQueryPreviewHistory(state, query) {
state.queryPreviewHistory.splice(state.queryPreviewHistory.indexOf(query), 1);
},
shiftQueryPreviewHistory(state) {
state.queryPreviewHistory.shift();
}
},
actions: {
fetchQueryPreview,
fetchAndSaveQueryPreview
fetchAndSaveQueryPreview,
updateQueryPreviewHistory
}
};
26 changes: 26 additions & 0 deletions packages/x-components/src/x-modules/queries-preview/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface QueryPreviewInfo {
export interface QueriesPreviewState {
/* The request and results */
queriesPreview: Dictionary<QueryPreviewItem>;
/* List of the most recent used queries preview queries */
queryPreviewHistory: string[];
/** The configuration of the queries preview module. */
config: QueriesPreviewConfig;
/** The extra params property of the state. */
Expand Down Expand Up @@ -107,6 +109,22 @@ export interface QueriesPreviewMutations extends ConfigMutations<QueriesPreviewS
* @param selectedQueryPreview - The selected query preview to save to the state.
*/
setSelectedQueryPreview(selectedQueryPreview: QueryPreviewInfo | null): void;
/**
* Adds a new entry to the queryPreviewHistory list.
*
* @param query - The query to add.
*/
setQueryPreviewHistory(query: string): void;
/**
* Removes a query from the queryPreviewHistory list.
*
* @param query - The query to remove.
*/
clearFromQueryPreviewHistory(query: string): void;
/**
* Removes the first item from the queryPreviewHistory list.
*/
shiftQueryPreviewHistory(): void;
}

/**
Expand All @@ -130,6 +148,14 @@ export interface QueriesPreviewActions {
* @param request - The request object to retrieve the query preview.
*/
fetchAndSaveQueryPreview(request: SearchRequest): void;

/**
* Prepends the QueryPreviewItem to the queryPreviewHistory in the state.
* Limited by {@link QueriesPreviewConfig.maxQueryPreviewHistoryLength}.
*
* @param request - The request object to retrieve the query preview.
*/
updateQueryPreviewHistory(request: SearchRequest): void;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export const fetchAndSaveQueryPreviewWire = wireDispatch('fetchAndSaveQueryPrevi
*/
export const clearQueryPreviewWire = wireCommit('clearQueryPreview');

/**
* Clears a query preview from the query preview history list.
*
* @public
*/
export const clearFromQueryPreviewHistoryWire = wireCommit('clearFromQueryPreviewHistory');

/**
* Sets the queries preview state `params`.
*
Expand Down Expand Up @@ -70,7 +77,8 @@ export const queriesPreviewWiring = createWiring({
fetchAndSaveQueryPreviewWire
},
QueryPreviewUnmountedHook: {
clearQueryPreviewWire
clearQueryPreviewWire,
clearFromQueryPreviewHistoryWire
},
UserAcceptedAQueryPreview: {
setSelectedQueryPreviewWire
Expand Down