todo/frontend/src/reducers/localStorage.js

29 lines
924 B
JavaScript

import { createAction, createAsyncAction } from './utils';
import { createReducer } from '@reduxjs/toolkit';
const actions = {
refresh: 'LOCAL_STORAGE_REFRESH'
};
export const readLocalStorage = createAsyncAction((dispatch, getState, config, key) => {
const payload = {
key: key,
value: JSON.parse(localStorage.getItem(key)) || undefined
};
return dispatch(refreshLocalStorage(payload));
});
export const updateLocalStorage = createAsyncAction((dispatch, getState, config, key, value) => {
localStorage.setItem(key, JSON.stringify(value));
return dispatch(refreshLocalStorage({ key: key, value: value }));
});
export const refreshLocalStorage = createAction(actions.refresh, (payload) => {
return payload;
});
export default createReducer({}, (builder) => {
builder.addDefaultCase((state, action) => {
state[action.payload?.key] = action.payload?.value;
});
});