All files / brand-store/slices brandStoreSlice.ts

12.5% Statements 2/16
0% Branches 0/2
0% Functions 0/6
12.5% Lines 2/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52      6x                                                           6x                                    
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { AppDispatch } from "../store";
 
export const slice = createSlice({
  name: "brandStores",
  initialState: {
    brandStoresList: [],
    loading: true,
    notFound: false,
  },
  reducers: {
    getBrandStoresLoading: (state) => {
      state.loading = true;
    },
    getBrandStoresSuccess: (state, action: PayloadAction<[]>) => {
      state.brandStoresList = action.payload || [];
      state.loading = false;
    },
    getBrandStoresNotFound: (state) => {
      state.notFound = true;
      state.loading = false;
    },
    getBrandStoresError: (state) => {
      state.loading = false;
    },
  },
});
 
export const {
  getBrandStoresLoading,
  getBrandStoresSuccess,
  getBrandStoresNotFound,
  getBrandStoresError,
} = slice.actions;
 
export function fetchStores() {
  return async (dispatch: AppDispatch) => {
    dispatch(getBrandStoresLoading());
 
    try {
      const response = await fetch("/admin/stores");
      const data = await response.json();
      dispatch(getBrandStoresSuccess(data));
    } catch (error) {
      dispatch(getBrandStoresNotFound());
      dispatch(getBrandStoresError());
    }
  };
}
 
export default slice.reducer;