redux-pouchdb icon indicating copy to clipboard operation
redux-pouchdb copied to clipboard

persistentCollectionReducer: Only top level objects persisted.

Open vikikamath opened this issue 4 years ago • 0 comments

I have a following contrived reducer where state resulting from ADD_TODO action is correctly persisted. However, state resulting from COMPLETED action is not persisted. As you can see my initialState is [ ]. FWIW, state in redux is as expected but not synced with db for COMPLETED action.

import { persistentCollectionReducer } from "redux-pouchdb";
import PouchDB from "pouchdb-browser";

const db = new PouchDB("todos");

const initialState = [];

const todosReducer = (state = initialState, { type, payload }) => {
  switch (type) {
    case "ADD_TODO":
      return [
        ...state,
        {
          _id: Math.random().toString(8),
          ...payload,
        },
      ];
    case "COMPLETED":
      return state.map((item) => {
        if (item._id === payload.id) {
          item.completed = !item.completed;
        }
        return item;
      });

    default:
      return state;
  }
};
export default persistentCollectionReducer(db, "todos")(todosReducer);

vikikamath avatar Jun 18 '20 01:06 vikikamath