Skip to content

Commit fa8186f

Browse files
committed
Persist document field search in URL
1 parent 4c29acd commit fa8186f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

frontend/src/document-details/document-details.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ module.exports = app => app.component('document-details', {
3737
this.initializeFieldValueEditor();
3838
}
3939
});
40+
41+
const searchFromUrl = this.getSearchQueryFromRoute();
42+
if (searchFromUrl) {
43+
this.searchQuery = searchFromUrl;
44+
}
4045
},
4146
beforeDestroy() {
4247
this.destroyFieldValueEditor();
@@ -55,6 +60,17 @@ module.exports = app => app.component('document-details', {
5560
});
5661
}
5762
}
63+
},
64+
searchQuery(newValue) {
65+
this.syncSearchQueryToUrl(newValue);
66+
},
67+
'$route.query.fieldSearch': {
68+
handler(newValue) {
69+
const nextValue = typeof newValue === 'string' ? newValue : '';
70+
if (nextValue !== this.searchQuery) {
71+
this.searchQuery = nextValue;
72+
}
73+
}
5874
}
5975
},
6076
computed: {
@@ -245,6 +261,38 @@ module.exports = app => app.component('document-details', {
245261
}
246262
},
247263
methods: {
264+
getSearchQueryFromRoute() {
265+
if (!this.$route) {
266+
return '';
267+
}
268+
const queryValue = this.$route.query?.fieldSearch;
269+
if (typeof queryValue === 'string') {
270+
return queryValue;
271+
}
272+
return '';
273+
},
274+
syncSearchQueryToUrl(value) {
275+
if (!this.$router || !this.$route) {
276+
return;
277+
}
278+
279+
const normalizedValue = typeof value === 'string' ? value : '';
280+
const shouldStore = normalizedValue.trim().length > 0;
281+
const currentValue = typeof this.$route.query.fieldSearch === 'string' ? this.$route.query.fieldSearch : '';
282+
283+
if (normalizedValue === currentValue || (!shouldStore && !currentValue)) {
284+
return;
285+
}
286+
287+
const nextQuery = { ...this.$route.query };
288+
if (shouldStore) {
289+
nextQuery.fieldSearch = normalizedValue;
290+
} else {
291+
delete nextQuery.fieldSearch;
292+
}
293+
294+
this.$router.replace({ query: nextQuery }).catch(() => {});
295+
},
248296
toggleVirtualField(fieldName) {
249297
if (this.collapsedVirtuals.has(fieldName)) {
250298
this.collapsedVirtuals.delete(fieldName);

0 commit comments

Comments
 (0)