Created
June 15, 2022 09:27
-
-
Save heytulsiprasad/1db3265a121b23fd84615eeb1bab1355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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