-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e7440d
commit 904e09d
Showing
17 changed files
with
326 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { createContext, useContext, useReducer } from "react"; | ||
import { cartReducer } from "../reducers"; | ||
|
||
const cartInitialState = { | ||
cartList: [], | ||
total: 0 | ||
} | ||
|
||
const CartContext = createContext(cartInitialState); | ||
|
||
export const CartProvider = ({children}) => { | ||
const [state, dispatch] = useReducer(cartReducer, cartInitialState); | ||
|
||
function addToCart(product){ | ||
const updatedList = state.cartList.concat(product); | ||
const updatedTotal = state.total + product.price; | ||
|
||
dispatch({ | ||
type: "ADD_TO_CART", | ||
payload: { | ||
products: updatedList, | ||
total: updatedTotal | ||
} | ||
}) | ||
} | ||
|
||
function removeFromCart(product){ | ||
const updatedList = state.cartList.filter(item => item.id !== product.id); | ||
const updatedTotal = state.total - product.price; | ||
|
||
dispatch({ | ||
type: "REMOVE_FROM_CART", | ||
payload: { | ||
products: updatedList, | ||
total: updatedTotal | ||
} | ||
}) | ||
} | ||
|
||
function clearCart(){ | ||
dispatch({ | ||
type: "CLEAR_CART", | ||
payload: { | ||
products: [], | ||
total: 0 | ||
} | ||
}) | ||
} | ||
|
||
const value = { | ||
cartList: state.cartList, | ||
total: state.total, | ||
addToCart, | ||
removeFromCart, | ||
clearCart | ||
} | ||
|
||
return ( | ||
<CartContext.Provider value={value}> | ||
{children} | ||
</CartContext.Provider> | ||
) | ||
} | ||
|
||
export const useCart = () => { | ||
const context = useContext(CartContext); | ||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { createContext, useContext, useReducer } from "react" | ||
import { filterReducer } from "../reducers"; | ||
|
||
const filterInitialState = { | ||
productList: [], | ||
onlyInStock: false, | ||
bestSellerOnly: false, | ||
sortBy: null, | ||
ratings: null | ||
} | ||
|
||
const FilterContext = createContext(filterInitialState); | ||
|
||
export const FilterProvider = ({children}) => { | ||
const [state, dispatch] = useReducer(filterReducer, filterInitialState); | ||
|
||
function initialProductList(products){ | ||
dispatch({ | ||
type: "PRODUCT_LIST", | ||
payload: { | ||
products: products | ||
} | ||
}); | ||
} | ||
|
||
function bestSeller(products){ | ||
return state.bestSellerOnly ? products.filter(product => product.best_seller === true) : products; | ||
} | ||
|
||
function inStock(products){ | ||
return state.onlyInStock ? products.filter(product => product.in_stock === true) : products; | ||
} | ||
|
||
function sort(products){ | ||
if(state.sortBy === "lowtohigh"){ | ||
return products.sort((a, b) => Number(a.price) - Number(b.price)); | ||
} | ||
if(state.sortBy === "hightolow"){ | ||
return products.sort((a, b) => Number(b.price) - Number(a.price)); | ||
} | ||
return products; | ||
} | ||
|
||
function rating(products){ | ||
if(state.ratings === "4STARSABOVE"){ | ||
return products.filter(product => product.rating >= 4); | ||
} | ||
if(state.ratings === "3STARSABOVE"){ | ||
return products.filter(product => product.rating >= 3); | ||
} | ||
if(state.ratings === "2STARSABOVE"){ | ||
return products.filter(product => product.rating >= 2); | ||
} | ||
if(state.ratings === "1STARSABOVE"){ | ||
return products.filter(product => product.rating >= 1); | ||
} | ||
return products; | ||
} | ||
|
||
const filteredProductList = rating(sort(inStock(bestSeller(state.productList)))); | ||
|
||
const value = { | ||
state, | ||
dispatch, | ||
products: filteredProductList, | ||
initialProductList | ||
} | ||
return ( | ||
<FilterContext.Provider value={value}> | ||
{children} | ||
</FilterContext.Provider> | ||
) | ||
} | ||
|
||
export const useFilter = () => { | ||
const context = useContext(FilterContext); | ||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { useFilter, FilterProvider } from "./FilterContext"; | ||
export { useCart, CartProvider } from "./CartContext"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.