Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Storing cubes in localstorage with key appid/cubes/cubeid
Browse files Browse the repository at this point in the history
  • Loading branch information
Helene Rigner committed Apr 25, 2019
1 parent d658dd3 commit 64b7b0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 deletions.
40 changes: 8 additions & 32 deletions src/components/cube.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,20 @@ const Cube = forwardRef(({
const columnToReplace = useRef(null);
const addOpen = useRef(false);
const forceUpdate = useForce();
const key = `${app.id}/cubes/${id}`;
let model = null;
let hypercubeProps = null;

const modifyLocalStorage = (action) => {
if (!isLocalStorage) return;
let storedCubes = localStorage.getItem(app.id);
const currentCube = { id, columns };
if (storedCubes) {
storedCubes = JSON.parse(storedCubes);
const index = storedCubes.findIndex(cube => cube.id === id);
if (action === 'add') {
if (index >= 0) {
// the cube is already stored. Update the stored item.
storedCubes.splice(index, 1, currentCube);
} else {
storedCubes.push(currentCube);
}
} else if (action === 'remove') {
storedCubes.splice(index, 1);
}
localStorage.setItem(app.id, JSON.stringify(storedCubes));
} else if (action === 'add') {
localStorage.setItem(app.id, currentCube);
}
};

const beforeunload = () => {
modifyLocalStorage('add');
};

useEffect(() => {
window.addEventListener('beforeunload', beforeunload);

if (isLocalStorage) {
localStorage.setItem(key, JSON.stringify(columns));
}
return () => {
modifyLocalStorage('remove');
window.removeEventListener('beforeunload', beforeunload);
if (isLocalStorage) {
localStorage.removeItem(key);
}
};
});
}, [columns]);

// Any instance of the component is extended with what is returned from the
// callback passed as the second argument.
Expand Down
18 changes: 7 additions & 11 deletions src/components/cubes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,14 @@ export function Cubes({ app, closeOnClickOutside, isLocalStorage }) {
useEffect(() => {
// Check if cubes are stored in localstorage.
if (!isLocalStorage) return;
let storedCubes = localStorage.getItem(app.id);
const key = `${app.id}/cubes/`;
const storedCubeList = [];
if (storedCubes) {
storedCubes = JSON.parse(storedCubes);
if (storedCubes && storedCubes.length) {
storedCubes.forEach((cube) => {
storedCubeList.push({ id: cube.id, initialColumns: cube.columns });
refs.current[cube.id] = React.createRef();
});
}
setCubeList(storedCubeList);
}
Object.keys(localStorage).filter(item => item.indexOf(key) >= 0).forEach((item) => {
const cube = { id: Number(item.replace(key, '')), initialColumns: JSON.parse(localStorage.getItem(item)) };
storedCubeList.push(cube);
refs.current[cube.id] = React.createRef();
});
setCubeList(storedCubeList);
}, []);

function removeCube(id) {
Expand Down

0 comments on commit 64b7b0c

Please sign in to comment.