2022-03-17 22:18:41 +01:00
|
|
|
import { createSlice } from '@reduxjs/toolkit'
|
|
|
|
import { getCollectionsFromIdb } from 'utils/idb';
|
2022-03-17 22:29:10 +01:00
|
|
|
import { findCollectionByUid } from 'utils/collections';
|
2022-03-17 22:18:41 +01:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
collections: []
|
|
|
|
};
|
|
|
|
|
|
|
|
export const collectionsSlice = createSlice({
|
|
|
|
name: 'app',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
loadCollections: (state, action) => {
|
|
|
|
state.collections = action.payload;
|
2022-03-17 22:29:10 +01:00
|
|
|
},
|
|
|
|
collectionClicked: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
collection.collapsed = !collection.collapsed;
|
|
|
|
}
|
2022-03-17 22:18:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-17 22:29:10 +01:00
|
|
|
export const { loadCollections, collectionClicked } = collectionsSlice.actions;
|
2022-03-17 22:18:41 +01:00
|
|
|
|
|
|
|
export const loadCollectionsFromIdb = () => (dispatch) => {
|
|
|
|
getCollectionsFromIdb(window.__idb)
|
|
|
|
.then((collections) => dispatch(loadCollections(collections)))
|
|
|
|
.catch((err) => console.log(err));
|
|
|
|
};
|
|
|
|
|
|
|
|
export default collectionsSlice.reducer;
|