import { createSlice , PayloadAction } from "@reduxjs/toolkit" ; import { Product } from "../types" ; export type productsType = { count : number ; products : Product [ ] ; } ; export type StateType = { products : null | productsType ; } ; const initialState = { products : null } ; const MarketplaceSlice = createSlice ( { name : "marketslice" , initialState , reducers : { setProduct : ( state , { payload } ) => { state . products = payload ; } , addProduct : ( state , { payload } ) => { if ( state . products ) { state . products . count += 1 ; const arr = [ ... state . products . products ] ; arr . push ( payload ) ; state . products = { ... state . products , products : arr } ; } } , updateProduct : ( state , { payload } ) => { if ( state . products ) { const arr = [ ... state . products . products ] ; arr . map ( item => item . id === payload . id ? ( item . purchased = true , item . owner = payload . buyer ) : null ) ; state . products = { ... state . products , products : arr } ; } } , } , } ) ; export default MarketplaceSlice . reducer ; export const { setProduct , addProduct , updateProduct } = MarketplaceSlice . actions ;