From 44d5099d9da44b62e1bfccd05739f7e25f7d9fae Mon Sep 17 00:00:00 2001 From: K Date: Sun, 24 Dec 2023 22:34:40 +0300 Subject: [PATCH] lab5 --- src/components/Busers/CartContext.jsx | 29 ----------- src/components/Busers/CartHook.js | 27 ----------- src/components/Busers/UsersReducer.js | 70 --------------------------- src/components/cart/Cart.jsx | 2 +- 4 files changed, 1 insertion(+), 127 deletions(-) delete mode 100644 src/components/Busers/CartContext.jsx delete mode 100644 src/components/Busers/CartHook.js delete mode 100644 src/components/Busers/UsersReducer.js diff --git a/src/components/Busers/CartContext.jsx b/src/components/Busers/CartContext.jsx deleted file mode 100644 index 3eecac9..0000000 --- a/src/components/Busers/CartContext.jsx +++ /dev/null @@ -1,29 +0,0 @@ -import { createContext, useEffect, useReducer } from 'react'; -import PropTypes from 'prop-types'; -import { cartReducer, loadCart, saveCart } from './UsersReducer'; - -// Компонент контекста корзины -const CartContext = createContext(null); -// Провайдер контекста корзины -export const CartProvider = ({ children }) => { - - const [cart, dispatch] = useReducer(cartReducer, [], loadCart); - - useEffect(() => { - saveCart(cart || []); - }, [cart]); - - const cartContextValue = { cart, dispatch }; - - return ( - - {children} - - ); -}; - -CartProvider.propTypes = { - children: PropTypes.node, -}; - -export default CartContext; \ No newline at end of file diff --git a/src/components/Busers/CartHook.js b/src/components/Busers/CartHook.js deleted file mode 100644 index 8bfe1e5..0000000 --- a/src/components/Busers/CartHook.js +++ /dev/null @@ -1,27 +0,0 @@ -import { useContext } from 'react'; -import CartContext from './CartContext.jsx'; -import { cartAdd, cartClear, cartRemove } from './UsersReducer.js'; - -const useCart = () => { - - const { cart, dispatch } = useContext(CartContext); - - const cartSum = () => { - return parseFloat( - cart?.reduce((sum, cartItem) => { - return sum + (cartItem.price * cartItem.count); - }, 0) - ?? 0, - ).toFixed(2); - }; - - return { - cart, - getCartSum: () => cartSum(), - addToCart: (item) => dispatch(cartAdd(item)), - removeFromCart: (item) => dispatch(cartRemove(item)), - clearCart: () => dispatch(cartClear()), - }; -}; - -export default useCart; diff --git a/src/components/Busers/UsersReducer.js b/src/components/Busers/UsersReducer.js deleted file mode 100644 index e50882d..0000000 --- a/src/components/Busers/UsersReducer.js +++ /dev/null @@ -1,70 +0,0 @@ -const addToUsersList = (usersList, item) => { - const existsItem = usersList.find((usersItem) => usersItem.id === item.id); - if (existsItem !== undefined) { - throw new Error('Пользователь уже существует!'); - } - return [...usersList, { ...item, count: 1 }]; -}; - -const removeFromUsersList = (usersList, item) => { - const updatedList = usersList.filter((usersItem) => usersItem.id !== item.id); - return updatedList; -}; - -const USERSLIST_KEY = 'localUsersList'; -const USERSLIST_ADD = 'usersList/add'; -const USERSLIST_REMOVE = 'usersList/remove'; -const USER_LOGIN = 'user/login'; -const USER_LOGOUT = 'user/logout'; - -export const saveUsersList = (usersList) => { - localStorage.setItem(USERSLIST_KEY, JSON.stringify(usersList)); -}; - -// Функция для загрузки данных пользователей из LocalStorage -export const loadUsersList = (initialValue = []) => { - const listData = localStorage.getItem(USERSLIST_KEY); - if (listData) { - return JSON.parse(listData); - } - return initialValue; -}; - -// Редюсер для обработки действий с массивом пользователей -export const cartReducer = (usersList, action) => { - const { item } = action; - switch (action.type) { - case USER_LOGOUT: { - return null; - } - case USER_LOGIN: { - return action.user; - } - case USERSLIST_ADD: { - return addToUsersList(usersList, item); - } - case USERSLIST_REMOVE: { - return removeFromUsersList(usersList, item); - } - default: { - throw Error(`Unknown action: ${action.type}`); - } - } -}; - -export const userLogout = () => ({ - type: USER_LOGOUT, -}); - -export const userLogin = (user) => ({ - type: USER_LOGIN, - user, -}); - -export const usersAdd = (item) => ({ - type: USERSLIST_ADD, item, -}); - -export const usersRemove = (item) => ({ - type: USERSLIST_REMOVE, item, -}); diff --git a/src/components/cart/Cart.jsx b/src/components/cart/Cart.jsx index 67ff56b..59677f7 100644 --- a/src/components/cart/Cart.jsx +++ b/src/components/cart/Cart.jsx @@ -69,7 +69,7 @@ const Cart = () => {
-
+
Корзина