I'm building an app with React, TS, Redux and Ducks (https://github.com/erikras/ducks-modular-redux)
I have several actions and reducers already built. But I am finding some troubles with the Post actions. I will leave you the code here as an example:
export const getChallenges: ReduxActionCreator = () => async (dispatch, getState) => {
try {
const data = await ApiClient.get('/challenges');
console.log('get', data);
dispatch({
type: types.GET_CHALLENGES_SUCCESS,
payload: data.data,
});
} catch (error) {
console.log(error);
}
};
export const postChallenges: ReduxActionCreator = (body) => async (dispatch, getState) => {
try {
const data = await ApiClient.post('/challenges', body);
console.log('post', data);
dispatch({
type: types.ADD_CHALLENGE_SUCCESS,
payload: data.data,
});
} catch (error) {
console.log(error);
}
};
So, getChallenges is working fine and I already render it in the browser.
For some reason, postChallenges is not even firing in Redux devtools, not even giving me an error.
I'm calling it simply like this:
const dispatch = useDispatch();
const handleSubmitForm = () => {
dispatch(postChallenges(values));
push(Path.AddMissions);
};
ApiClient code
import axios from 'axios';
export const ApiClient = axios.create({
baseURL: 'https://thnk.gendesa.genit.com.ar/api/v1/',
timeout: 20 * 1000,
headers: {
'Content-Type': 'application/json',
},
});
ApiClient.interceptors.response.use(
(response) => response.data,
(error) => Promise.reject(error),
);
Any ideas?
Thanks!
Thanks everybody for your questions and suggestions. I'll definetely check the ducks documentation to improve the code once I have the time.
Regarding the solution, it was so simple:
Just adding the challenges reducer to the store.
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const rootReducer = combineReducers({
assesments: assessmentsReducer,
auth: authReducer,
events: eventsReducer,
elements: elementsReducer,
challenges: challengesReducer,
questions: questionsReducer,
surveys: surveysReducer,
missions: missionsReducer,
});
export const store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)));
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
Thanks for taking your time to read my question
Kindly,