All files / brand-store/slices currentStoreSlice.ts

75% Statements 12/16
0% Branches 0/2
83.33% Functions 5/6
75% Lines 12/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             6x 6x     6x                   6x     6x 6x   6x 6x       6x 6x            
import { createSlice } from "@reduxjs/toolkit";
import { AppDispatch } from "../store";
 
export const slice = createSlice({
  name: "currentStore",
  initialState: {
    currentStore: {},
    loading: true,
    notFound: false,
  },
  reducers: {
    getCurrentStoreLoading: (state) => {
      state.loading = true;
    },
    getCurrentStoreSuccess: (state, { payload }) => {
      state.currentStore = payload || [];
      state.loading = false;
    },
    getCurrentStoreNotFound: (state) => {
      state.notFound = true;
      state.loading = false;
    },
    getCurrentStoreError: (state) => {
      state.loading = false;
    },
  },
});
 
export const {
  getCurrentStoreLoading,
  getCurrentStoreSuccess,
  getCurrentStoreNotFound,
  getCurrentStoreError,
} = slice.actions;
 
export function fetchStore(storeId: string) {
  return async (dispatch: AppDispatch) => {
    dispatch(getCurrentStoreLoading());
 
    try {
      const response = await fetch(`/admin/store/${storeId}`);
      const data = await response.json();
      dispatch(getCurrentStoreSuccess(data));
    } catch (error) {
      dispatch(getCurrentStoreNotFound());
      dispatch(getCurrentStoreError());
    }
  };
}
 
export default slice.reducer;