Search code examples
reactjsreduxreact-reduxredux-thunkredux-toolkit

builder.addCase does not change the state


I have a reset password slice where I make an async request. The slice also updates the request progress state like this:

export const initialBasicAsyncState: BasicAsyncState = {
  isLoading: false,
  isSuccess: false,
  errors: { status: null, message: "" },
};

interface ResetPasswordData {
  password: string;
  confirmPassword: string;
  token: string;
}

const initialResetPasswordErrors = {
  passwordErrorMessage: "",
  confirmPasswordErrorMessage: "",
};

export const resetPassword = createAsyncThunk<
  void,
  ResetPasswordData,
  { rejectValue: CustomAuthError }
>(
  "resetPassword/resetPassword",
  async (resetPasswordData, { rejectWithValue }) => {
    const { password, confirmPassword, token } = resetPasswordData;
    try {
      await axios.post(resetPasswordTokenURL(token), {
        password,
        confirmPassword,
      });
    } catch (err) {
      console.error(err);
      const { status, statusText, data } = err.response;
      if (status === 422) {
        const formattedErrors = setupMultipleErrors(
          err,
          initialResetPasswordErrors
        );
        return rejectWithValue({ status, message: formattedErrors });
      }
      if (status === 401) {
        return rejectWithValue({
          status,
          message: data.message,
        });
      }
      return rejectWithValue({ status, message: statusText });
    }
  }
);

const resetPasswordSlice = createSlice({
  name: "resetPassword",
  initialState: {
    ...initialBasicAsyncState,
  },
  reducers: {},
  extraReducers: (builder) =>
    builder
      .addCase(resetPassword.pending, (state) => {
        state.isLoading = true;
      })
      .addCase(resetPassword.fulfilled, (state) => {
        state.isLoading = false;
        state.isSuccess = true;
      })
      .addCase(resetPassword.rejected, (state, action) => {
        state.isLoading = false;
        if (action.payload) state.errors = action.payload;
      }),
});

export const {} = resetPasswordSlice.actions;

export default resetPasswordSlice.reducer;

I get the correct errors from the resetPassword function as a payload. However, the state inside of the addCases does not change, even the errors which I get as a payload.

Here is how things look inside redux DevTools:

enter image description here

enter image description here


Solution

  • Thank you for your troubles. As mentioned in the comments looks like I made a silly error of not including correctly the resetPasswordSlice.reducer.