37 lines
1006 B
TypeScript
37 lines
1006 B
TypeScript
import { RootState, SavedState } from "./root";
|
|
import { Module, Plugin, Store } from "vuex";
|
|
import * as utils from "../utils";
|
|
|
|
export interface Favourite {
|
|
name: string;
|
|
state: SavedState;
|
|
}
|
|
|
|
export interface FavouritesState {
|
|
favourites: Favourite[];
|
|
}
|
|
|
|
const favourites: Module<FavouritesState, RootState> = {
|
|
namespaced: true,
|
|
state: {
|
|
favourites: []
|
|
},
|
|
mutations: {
|
|
add(state, favourite: Favourite) {
|
|
state.favourites.push(favourite);
|
|
},
|
|
remove(state, favourite: Favourite) {
|
|
state.favourites = state.favourites.filter(f => f != favourite);
|
|
}
|
|
}
|
|
};
|
|
|
|
export const localStorageSaver = (path: string, key: string): Plugin<any> => (store: Store<any>) => {
|
|
utils.set(store.state, path, JSON.parse(window.localStorage.getItem(key) || '[]'));
|
|
|
|
store.subscribe((mutation, state) => {
|
|
window.localStorage.setItem(key, JSON.stringify(utils.get(state, path)));
|
|
})
|
|
};
|
|
|
|
export default favourites; |