import { ADD_TASK, ADD_CDB_TASK, TOGGLE_COMPLETED } from "../actionTypes"; import { v4 as uuidv4 } from "uuid"; const initialState = { tasks: [] }; export default function taskReducer(state = initialState, action) { switch (action.type) { // Used when task is added during session but not committed to cloud case ADD_TASK: { return { ...state, tasks: [ ...state.tasks, { taskId: action.taskId, taskName: action.taskName, description: action.description, status: action.status, parentIds: action.parentIds, completed: action.completed, }, ], }; } // Used during initial synchronization of the local storage using cloud data // a local pouchdb database is also created into which task commits can be done // during initialization of the database case ADD_CDB_TASK: { return { ...state, tasks: [ ...state.tasks, { _id: action.task.taskId, taskId: action.task.taskId, taskName: action.task.taskName, description: action.task.description, status: action.task.status, parentIds: action.task.parentIds, completed: action.task.completed, }, ], }; } // Toggle completed case TOGGLE_COMPLETED: { console.log(action.id); return { ...state, tasks: state.tasks.map((task) => task.taskId === action.id ? { ...task, completed: !task.completed } : task ), }; } default: return state; } }