From c912a61b5724a3d30661bbceedba38c80743e232 Mon Sep 17 00:00:00 2001 From: cv <34649812+cpvalente@users.noreply.github.com> Date: Thu, 20 May 2021 08:34:03 +0200 Subject: [PATCH] feat: expand collapse --- client/src/app/context/collapseAtom.js | 60 +++++++++++++++++++ .../common/components/buttons/CollapseBtn.jsx | 2 +- .../common/components/buttons/ExpandBtn.jsx | 2 +- .../src/features/editors/list/EventBlock.jsx | 48 +++++++++------ .../editors/list/EventListWrapper.jsx | 14 ++++- client/src/features/menu/EventListMenu.jsx | 7 ++- 6 files changed, 110 insertions(+), 23 deletions(-) create mode 100644 client/src/app/context/collapseAtom.js diff --git a/client/src/app/context/collapseAtom.js b/client/src/app/context/collapseAtom.js new file mode 100644 index 000000000..30d1e18de --- /dev/null +++ b/client/src/app/context/collapseAtom.js @@ -0,0 +1,60 @@ +const { atom } = require('jotai'); + +const PATH = 'option-collapse'; + +// collapse options object, initialised from local storage +// REFRACT: When do we read from local storage? +// on every render? + +export const collapseAtom = atom(JSON.parse(localStorage.getItem(PATH)) || {}); + +// get a single option, if it exists +export const SelectCollapse = (id) => { + return atom((get) => get(collapseAtom)[id]); +}; + +// change a single item in object +export const HandleCollapse = atom(null, (get, set, payload) => { + const updatedVal = { + ...get(collapseAtom), + ...payload, + }; + set(collapseAtom, updatedVal); + localStorage.setItem(PATH, JSON.stringify(updatedVal)); +}); + +// change collapsed in several items +export const BatchOperation = atom(null, (get, set, payload) => { + let prevOptions = get(collapseAtom); + + let newOptions = {}; + + for (const item of payload.items) { + newOptions[item.id] = payload.isCollapsed; + } + + if (payload.clear) { + // clear object + prevOptions = {}; + // clear localstorage + localStorage.removeItem(PATH); + } + + const options = { ...prevOptions, ...newOptions }; + + set(collapseAtom, options); + + localStorage.setItem(PATH, JSON.stringify(options)); +}); + +// change collapsed in all items +export const setAll = async (items, isCollapsed) => { + // clear storage + localStorage.removeItem(PATH); + + // clear local object + console.log('debug collapse here', isCollapsed); + + // call batch + BatchOperation({ items: items, isCollapsed: isCollapsed }); +}; diff --git a/client/src/common/components/buttons/CollapseBtn.jsx b/client/src/common/components/buttons/CollapseBtn.jsx index 5e98ef4d1..490af34bb 100644 --- a/client/src/common/components/buttons/CollapseBtn.jsx +++ b/client/src/common/components/buttons/CollapseBtn.jsx @@ -7,7 +7,7 @@ export default function CollapseBtn(props) {