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) { } - colorScheme='whiteAlpha' + colorScheme='white' variant='outline' onClick={clickhandler} _focus={{ boxShadow: 'none' }} diff --git a/client/src/common/components/buttons/ExpandBtn.jsx b/client/src/common/components/buttons/ExpandBtn.jsx index 0ad8a9296..b0dddf8e7 100644 --- a/client/src/common/components/buttons/ExpandBtn.jsx +++ b/client/src/common/components/buttons/ExpandBtn.jsx @@ -7,7 +7,7 @@ export default function ExpandBtn(props) { } - colorScheme='whiteAlpha' + colorScheme='white' variant='outline' onClick={clickhandler} _focus={{ boxShadow: 'none' }} diff --git a/client/src/features/editors/list/EventBlock.jsx b/client/src/features/editors/list/EventBlock.jsx index dc1b332c4..62cf829b5 100644 --- a/client/src/features/editors/list/EventBlock.jsx +++ b/client/src/features/editors/list/EventBlock.jsx @@ -1,6 +1,6 @@ import Icon from '@chakra-ui/icon'; import { FiChevronDown, FiChevronUp, FiMoreVertical } from 'react-icons/fi'; -import { useState } from 'react'; +import { useMemo } from 'react'; import { Draggable } from 'react-beautiful-dnd'; import EventTimes from 'common/components/eventTimes/EventTimes'; import EventTimesVertical from 'common/components/eventTimes/EventTimesVertical'; @@ -10,6 +10,8 @@ import VisibleIconBtn from 'common/components/buttons/VisibleIconBtn'; import DeleteIconBtn from 'common/components/buttons/DeleteIconBtn'; import { millisToMinutes } from 'common/dateConfig'; import style from './EventBlock.module.css'; +import { SelectCollapse, HandleCollapse } from 'app/context/collapseAtom'; +import { useAtom } from 'jotai'; const ExpandedBlock = (props) => { const { provided, data, next, delay, delayValue, actionHandler } = props; @@ -85,7 +87,7 @@ const ExpandedBlock = (props) => { as={FiChevronUp} marginTop='0.2em' gridArea='more' - onClick={() => props.setExpanded(false)} + onClick={() => props.setCollapsed(true)} />