Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Created June 15, 2022 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heytulsiprasad/1db3265a121b23fd84615eeb1bab1355 to your computer and use it in GitHub Desktop.
Save heytulsiprasad/1db3265a121b23fd84615eeb1bab1355 to your computer and use it in GitHub Desktop.
import * as Actions from './../../../constants/actionTypes';
const initialState = {
pages: [],
};
const pageReducer = (state = initialState, action) => {
switch (action.type) {
// New page open
case Actions.ADD_PAGE: {
return {
...state,
pages: [action.payload, ...state.pages],
};
}
case Actions.REMOVE_PAGE: {
return {
...state,
pages: state.pages.filter((page) => page.id !== action.payload),
};
}
case Actions.ADD_CONTENT: {
const pageId = state.pages.findIndex(
(page) => page.id === action.payload.pageId
);
const newPages = [...state.pages];
newPages[pageId].content.push(action.payload.content);
return {
...state,
pages: newPages,
};
}
case Actions.DELETE_CONTENT: {
const pageId = state.pages.findIndex(
(page) => page.id === action.payload.pageId
);
const newContent = state.pages[pageId].content.filter(
(item) => item.id !== action.payload.contentId
);
const newPages = [...state.pages];
newPages[pageId].content = newContent;
return {
...state,
pages: newPages,
};
}
case Actions.UPDATE_PROGRESS: {
const pageId = state.pages.findIndex(
(page) => page.id === action.payload.pageId
);
const newPages = [...state.pages];
newPages[pageId].progress = action.payload.progress;
return {
...state,
pages: newPages,
};
}
default:
return state;
}
};
export default pageReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment