All files / src/redux/slices products.ts

100% Statements 11/11
100% Branches 6/6
100% Functions 4/4
100% Lines 10/10

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 53 54 55 56 57 58 59                                540x                 540x               31x 31x         31x           3x           540x   540x 540x 10464x      
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
 
export type SelectedProduct = {
  product_id?: number;
  imgSrc: string;
  title: string;
};
 
export interface Product {
  selectedProduct: SelectedProduct;
  selectedPaymentFormID: string | null;
}
interface RootState {
  product: Product;
}
 
export const initialProductState: Product = {
  selectedProduct: {
    product_id: undefined,
    imgSrc: "",
    title: "",
  },
  selectedPaymentFormID: null,
};
 
const productSlice = createSlice({
  name: "product",
  initialState: initialProductState,
  reducers: {
    setNewSelectedProduct: (
      state: Product,
      action: PayloadAction<SelectedProduct>,
    ) => {
      const payload = action.payload;
      const actionProduct = {
        product_id: payload?.product_id || undefined,
        imgSrc: payload?.imgSrc || "",
        title: payload?.title || "",
      };
      state.selectedProduct = actionProduct;
    },
    setSelectedPaymentFormID: (
      state: Product,
      action: PayloadAction<string | null>,
    ) => {
      state.selectedPaymentFormID = action.payload;
    },
  },
});
 
export const { setNewSelectedProduct, setSelectedPaymentFormID } =
  productSlice.actions;
 
export const selectProduct = (state: RootState) => state.product;
export const selectPaymentFormID = (state: RootState) =>
  state.product.selectedPaymentFormID;
 
export default productSlice.reducer;