All files / src/redux/features productDetails.ts

100% Statements 50/50
100% Branches 4/4
100% Functions 4/4
100% Lines 50/50

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 511x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Product } from "@/types/product";
import { RootState } from "../store";
 
/**
 * Payload type for updating product details.
 * Allows any object with an id field - handles various product formats
 * from different API responses (ProductDetails, CartItem, etc.)
 */
type ProductDetailsPayload = { id: number; [key: string]: unknown };
 
interface ProductDetailsState {
  value: Product;
}
 
const initialState: ProductDetailsState = {
  value: {
    title: "",
    reviews: 0,
    price: 0,
    discountedPrice: 0,
    img: "",
    images: [],
    id: 0,
    imgs: { thumbnails: [], previews: [] },
  } as Product,
};
 
export const productDetails = createSlice({
  name: "productDetails",
  initialState,
  reducers: {
    updateProductDetails: (state, action: PayloadAction<ProductDetailsPayload>) => {
      state.value = action.payload as Product;
    },
 
    resetProductDetails: (state) => {
      state.value = initialState.value;
    },
  },
});
 
// Selectors
export const selectProductDetails = (state: RootState) =>
  state.productDetailsReducer.value;
export const selectProductDetailsId = (state: RootState) =>
  state.productDetailsReducer.value.id;
 
export const { updateProductDetails, resetProductDetails } = productDetails.actions;
export default productDetails.reducer;