Skip to content

Commit

Permalink
Fix bookmarking of search requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuess committed Feb 5, 2025
1 parent 71ff225 commit 537da3a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 107 deletions.
36 changes: 14 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ There are two options:

> This requires Volto >= 16.10.0 with a pluggable toolbar.
Include bookmarking in your Volto project by integrating a component `Bookmarking`.
This component adds two buttons to the toolbar: one for toggling the bookmark of the current page and one for displaying a menu with a list of bookmarks.
Include bookmarking in your Volto project by integrating the component `Bookmarking`.
This component adds two buttons to the toolbar:
one for toggling the bookmark of the current page and
one for displaying a menu with a list of bookmarks.

`config.js`:

Expand Down Expand Up @@ -98,27 +100,17 @@ config.settings.bookmarks = {
Add a mapping for search filters:

```js
config.settings.bookmarks.filtermapping = {
facet_fields: {
'5237dc43-c573-4651-a5b8-cf24bfde13a6': 'Datendrehscheibe',
allgemeines: 'Allgemeines',
arbeitsliste: 'Arbeitsliste',
beb2k: 'BEB2k',
'release-note': 'Release-Note',
tutorial: 'Tutorial',
'superuser-innen': 'Superuser/innen',
ai: 'AI',
andere: 'Andere',
tg: 'TG',
zh: 'ZH',
},
search_sections: {
others: 'Website',
dokumentation: 'Dokumentation',
inside: 'IGIB-Inside',
geologie: 'Geologie',
},
config.settings.bookmarks.filtermapping = {
facet_fields: {
'News Item': 'Nachricht',
Document: 'Seite',
Event: 'Veranstaltung',
},
search_sections: {
others: 'Website',
nachrichten: 'Nachrichten',
},
};
```

<img align="right" width="50" alt="volto-bookmarks" src="./packages/volto-bookmarks/src/icons/bookmark.svg" />
Expand Down
5 changes: 4 additions & 1 deletion packages/policy/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ const applyConfig = (config) => {

config.settings.bookmarks.filtermapping = {
facet_fields: {
'News Item': 'News',
'News Item': 'News Item',
Document: 'Page',
Event: 'Event',
},
search_sections: {
others: 'Website',
news: 'News',
},
};
Expand Down
10 changes: 5 additions & 5 deletions packages/volto-bookmarks/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { ADD_BOOKMARK, DEL_BOOKMARK, GET_BOOKMARKS } from '../constants';

import { generateSearchQueryObject } from '../helpers';
import { sortQuerystring } from '../helpers';

/**
* addBookmark
Expand All @@ -13,7 +13,7 @@ import { generateSearchQueryObject } from '../helpers';
* @param {String} querystring
* @param {Object} payload
*/
export function addBookmark(uid, group, querystring = '', payload = {}) {
export function addBookmark(uid, group, querystring = null, payload = {}) {
return {
type: ADD_BOOKMARK,
request: {
Expand All @@ -22,7 +22,7 @@ export function addBookmark(uid, group, querystring = '', payload = {}) {
data: {
uid,
group,
queryparams: JSON.stringify(generateSearchQueryObject(querystring)),
queryparams: sortQuerystring(querystring),
payload,
},
},
Expand All @@ -35,7 +35,7 @@ export function addBookmark(uid, group, querystring = '', payload = {}) {
* @param {String} group
* @param {Object} queryObjectStringified
*/
export function deleteBookmark(uid, group, queryObjectStringified = '') {
export function deleteBookmark(uid, group, querystring = null) {
return {
type: DEL_BOOKMARK,
request: {
Expand All @@ -44,7 +44,7 @@ export function deleteBookmark(uid, group, queryObjectStringified = '') {
data: {
uid,
group,
queryparams: queryObjectStringified,
queryparams: sortQuerystring(querystring),
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import './volto-bookmarks.css';
import config from '@plone/volto/registry';

function getTitle(queryparams) {
const searchParamsObject = JSON.parse(queryparams);
console.debug('** getTitle. queryparams', queryparams);
const searchParamsObject = new URLSearchParams(queryparams);

let query = searchParamsObject['query'];
let query = searchParamsObject.get('query');

// default search block
if (query && query.length > 0) {
Expand All @@ -43,8 +44,8 @@ function getTitle(queryparams) {

// searchkit block of volto-searchkit-block
let title_array = [];
query = searchParamsObject['q'];
let filters = searchParamsObject['f'];
query = searchParamsObject.get('q');
let filters = searchParamsObject.getAll('f');
let section = '';

if (query && query[0].length) {
Expand Down Expand Up @@ -85,7 +86,6 @@ function getTitle(queryparams) {
search_bookmark_title = section
? `${section}: ${search_bookmark_title}`
: search_bookmark_title;

return search_bookmark_title;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useDispatch } from 'react-redux';

import { getAllBookmarks } from '@plone-collective/volto-bookmarks/actions';

// event handler for searchkitQueryChanged event of package react-searchkit
const EventListenerSearchkitSearch = () => {
const dispatch = useDispatch();

function searchOnUrlQueryStringChanged(event) {
// event handler for searchkitQueryChanged event of package react-searchkit
dispatch(getAllBookmarks());
}

Expand Down
17 changes: 4 additions & 13 deletions packages/volto-bookmarks/src/components/MenuItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
deleteBookmark,
getAllBookmarks,
} from '@plone-collective/volto-bookmarks/actions';
import { generateSearchQueryParamsString } from '@plone-collective/volto-bookmarks/helpers';

const messages = defineMessages({
title_bookmarks: {
Expand Down Expand Up @@ -44,25 +43,17 @@ const MenuItem = ({ intl, item }) => {
<li className="bookmarkitem" key={item['@id']}>
<Link
title={item.description || ''}
to={`${
flattenToAppURL(item['@id']) +
'?' +
generateSearchQueryParamsString(item.queryparams)
}`}
to={`${flattenToAppURL(item['@id']) + '?' + item.queryparams}`}
onClick={() => {
// Hack: Select a bookmark of a search, then select another bookmark of another search.
// Search is triggered!
// Criterion: search parameters do exist
// Event 'popstate' triggers a search.
const url = `${
flattenToAppURL(item['@id']) +
'?' +
generateSearchQueryParamsString(item.queryparams)
flattenToAppURL(item['@id']) + '?' + item.queryparams
}`;
const queryparams_object = item.queryparams
? JSON.parse(item.queryparams)
: {};
if (Object.keys(queryparams_object).length > 0) {

if (item.queryparams) {
window.history.pushState({}, 'search bookmark', url);
let evt = new CustomEvent('popstate', {
detail: {},
Expand Down
15 changes: 5 additions & 10 deletions packages/volto-bookmarks/src/components/ToggleBookmarkButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getAllBookmarks,
} from '@plone-collective/volto-bookmarks/actions';

import { generateSearchQueryObject } from '@plone-collective/volto-bookmarks/helpers';
import { sortQuerystring } from '@plone-collective/volto-bookmarks/helpers';

import { allBookmarksAtom } from '@plone-collective/volto-bookmarks/atoms';

Expand Down Expand Up @@ -52,14 +52,13 @@ const ToggleBookmarkButton = ({ item = null }) => {
React.useEffect(() => {
// Check if page is bookmarked
setBookmarked(false);
const doLoSearch = generateSearchQueryObject(document.location.search);
const doLoSearch = sortQuerystring(document.location.search);
bookmarksArray &&
bookmarksArray.forEach((element) => {
if (
item
? element.uid === item?.UID
: element.uid === content?.UID &&
isEqual(JSON.parse(element.queryparams), doLoSearch)
: element.uid === content?.UID && element.queryparams === doLoSearch
) {
setBookmarked(true);
}
Expand Down Expand Up @@ -89,11 +88,7 @@ const ToggleBookmarkButton = ({ item = null }) => {
deleteBookmark(
item?.UID || content.UID,
group,
JSON.stringify(
generateSearchQueryObject(
item?.UID ? '' : document.location.search,
),
),
item?.UID ? null : document.location.search,
),
).then(() => {
dispatch(getAllBookmarks());
Expand All @@ -104,7 +99,7 @@ const ToggleBookmarkButton = ({ item = null }) => {
addBookmark(
item?.UID || content.UID,
group,
item?.UID ? '' : document.location.search,
item?.UID ? null : document.location.search,
{},
),
).then(() => {
Expand Down
61 changes: 11 additions & 50 deletions packages/volto-bookmarks/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,19 @@ const translateSearch = (el, type) => {
};

/**
* generateSearchQueryObject
* @function generateSearchQueryObject
* sortQuerystring
* @function sortQuerystring
* @param {string} querystring querystring of url
* @return {string} ready for 'get' request. part of unique identifier of a bookmark
* @return {string} ready for request. part of unique identifier of a bookmark
*/
function generateSearchQueryObject(querystring) {
const params = new URLSearchParams(querystring);
let obj = {};
for (let key of params.keys()) {
const values = params.getAll(key);
if (values.length > 1) {
obj[key] = {};
values.forEach((el) => {
var [k, v] = el.split(':');
obj[key][k] = v;
});
} else {
obj[key] = values[0];
}
}
return obj;
}

/**
* generateSearchQueryParamsString
* counterpart of generateSearchQueryObject
* @function generateSearchQueryParamsString
* @param {string} searchparamstring Json stringified object with search params (values are lists!)
* @return {string} querystring, ready to use in url
*/
function generateSearchQueryParamsString(searchparamstring) {
const obj = JSON.parse(searchparamstring);
const params = new URLSearchParams();
for (const [key, value] of Object.entries(obj)) {
if (typeof value == 'string') {
params.append(key, value);
} else if (value.length > 0) {
for (const el of value) {
params.append(key, el);
}
} else {
Object.keys(value).forEach((filterkey) => {
params.append(key, `${filterkey}:${value[filterkey]}`);
});
}
function sortQuerystring(querystring) {
if (!querystring) {
return '';
}
return params.toString();
let params = new URLSearchParams(querystring);
params.sort();
let sortedParams = params.toString();
return sortedParams;
}

function parseSearchBlockQuery(query) {
Expand All @@ -73,9 +39,4 @@ function parseSearchBlockQuery(query) {
return obj;
}

export {
generateSearchQueryParamsString,
generateSearchQueryObject,
parseSearchBlockQuery,
translateSearch,
};
export { sortQuerystring, parseSearchBlockQuery, translateSearch };
12 changes: 12 additions & 0 deletions packages/volto-bookmarks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ const applyConfig = (config) => {
bookmarkgroupfield: '@type',
};

config.settings.bookmarks.filtermapping = {
facet_fields: {
'News Item': 'News Item',
Document: 'Page',
Event: 'Event',
},
search_sections: {
others: 'Website',
news: 'News',
},
};

// menu body
config.settings.additionalToolbarComponents = {
bookmarksMenu: {
Expand Down

0 comments on commit 537da3a

Please sign in to comment.