These are my reducers:
export default combineReducers({
courses: coursesReducer,
books: booksReducer,
ui: uiReducer
});
I have already coded everything related to the coursesReducer
.
In Next.js I have pages/course/[cid].js
and I ha CRUD operations as reducer actions and everything. An example, store/actions/courseActions.js
:
export const fetchACourse = (payload) => async (dispatch) => {
const res = await fetch(`http://localhost:1338/courses/${payload.id}`);
const data = await res.json();
dispatch({
type: types.FETCH_A_COURSE,
payload: data,
});
}
My dilemma is what everything in booksReducer
is identical apart from the fetch URLs: The data structure is exactly the same.
Even though it would involve a lot of refactoring and if statement, do I refactor the code somehow like
export const fetchACourse = (payload) => async (dispatch) => {
const uri = (payload.type) === 'books' ? `books`: `courses`
const dispatchType = (payload.type) === 'books' ? types.FETCH_A_BOOKS : types.FETCH_A_COURSE
const res = await fetch(`http://localhost:1338/${uri}/${payload.id}`);
const data = await res.json();
dispatch({
type: dispatchType,
payload: data,
});
}
Or should I just create store/actions/courseActions.js
and practically duplicate everything and add the minor tweaks?