Skip to content

Commit

Permalink
Add Categories and Brands fields in productsSlice and generate them f…
Browse files Browse the repository at this point in the history
…rom server result
  • Loading branch information
webdevsk committed Oct 9, 2023
1 parent 2e9800a commit 2ab3af0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/features/products/productsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const initialState = productsAdapter.getInitialState({
skip: null,
limit: null,
error: null,
categories: [],
brands: []
})

const productsSlice = createSlice({
Expand All @@ -24,6 +26,11 @@ const productsSlice = createSlice({
.addCase(fetchProducts.fulfilled, (state, action) => {
state.status = "success"
productsAdapter.upsertMany(state, action.payload.products)

state.categories.push(...new Map(action.payload.products.map((item) => [item["category"], item])).keys())

state.brands.push(...new Map(action.payload.products.map((item) => [item["brand"], item])).keys())

delete action.payload.products
Object.assign(state, action.payload)
})
Expand All @@ -46,11 +53,18 @@ export const selectProductsByCategory = createSelector([
selectAllProducts, (state, category) => category
],
(products, category) => {
console.log("selector running")
console.log(products)
console.log("Category selector running")
return products.filter(product => product.category === category)
})

export const selectProductsByBrand = createSelector([
selectAllProducts, (state, brand) => brand
],
(products, brand) => {
console.log("Brand selector running")
return products.filter(product => product.brand === brand)
})

export const selectExclusiveProducts = createSelector([
selectAllProducts
], (products) => {
Expand Down

0 comments on commit 2ab3af0

Please sign in to comment.