-
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.
Merge remote-tracking branch 'origin/dev' into test-12
- Loading branch information
Showing
45 changed files
with
2,255 additions
and
1,125 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
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
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
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
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 |
---|---|---|
@@ -1,74 +1,57 @@ | ||
import React, { createContext, useState, useContext, useEffect } from "react"; | ||
|
||
// Создаем контекст | ||
const BasketContext = createContext(); | ||
|
||
// Провайдер контекста для оборачивания компонентов | ||
export const BasketProvider = ({ children }) => { | ||
// Получаем данные из localStorage или начинаем с пустого массива | ||
const loadBasketItems = () => { | ||
const savedItems = localStorage.getItem("basketItems"); | ||
return savedItems ? JSON.parse(savedItems) : []; | ||
}; | ||
|
||
const [basketItems, setBasketItems] = useState(loadBasketItems); | ||
|
||
// Сохраняем корзину в localStorage при изменении состояния | ||
// Сохраняем корзину в localStorage при любом изменении состояния | ||
useEffect(() => { | ||
if (basketItems.length > 0) { | ||
localStorage.setItem("basketItems", JSON.stringify(basketItems)); | ||
} | ||
localStorage.setItem("basketItems", JSON.stringify(basketItems)); | ||
}, [basketItems]); | ||
|
||
const addToBasket = (item) => { | ||
const currentDate = new Date().toLocaleDateString("ru-RU"); // Получаем дату в формате "день.месяц.год" | ||
const currentDate = new Date().toLocaleDateString("ru-RU"); | ||
|
||
setBasketItems((prevItems) => { | ||
const existingItem = prevItems.find((i) => i.id === item.id); | ||
if (existingItem) { | ||
const exists = prevItems.some((i) => i.id === item.id); | ||
if (exists) { | ||
return prevItems.map((i) => | ||
i.id === item.id | ||
? { ...i, quantity: i.quantity + 1, dateAdded: currentDate } // Добавляем или обновляем дату | ||
: i | ||
i.id === item.id ? { ...i, dateAdded: currentDate } : i | ||
); | ||
} | ||
return [ | ||
...prevItems, | ||
{ ...item, quantity: 1, dateAdded: currentDate }, // Добавляем дату добавления | ||
]; | ||
return [...prevItems, { ...item, dateAdded: currentDate }]; | ||
}); | ||
}; | ||
|
||
// Функция для удаления 1 единицы элемента из корзины | ||
const removeOneFromBasket = (id) => { | ||
setBasketItems((prevItems) => { | ||
const updatedItems = prevItems | ||
.map((item) => | ||
item.id === id ? { ...item, quantity: item.quantity - 1 } : item | ||
) | ||
.filter((item) => item.quantity > 0); // Убираем элементы с нулевым количеством | ||
return updatedItems; | ||
}); | ||
const removeFromBasket = (id) => { | ||
setBasketItems((prevItems) => prevItems.filter((item) => item.id !== id)); | ||
}; | ||
|
||
// Функция для полного удаления элемента из корзины | ||
const removeFromBasket = (id) => { | ||
setBasketItems((prevItems) => prevItems.filter((item) => item.id !== id)); // Удаляем элемент по id | ||
const totalItems = basketItems.length; | ||
|
||
const clearBasket = () => { | ||
setBasketItems([]); | ||
}; | ||
|
||
return ( | ||
<BasketContext.Provider | ||
value={{ | ||
basketItems, | ||
addToBasket, | ||
removeFromBasket, | ||
removeOneFromBasket, | ||
}} | ||
> | ||
{children} | ||
</BasketContext.Provider> | ||
<BasketContext.Provider | ||
value={{ | ||
totalItems, | ||
basketItems, | ||
addToBasket, | ||
removeFromBasket, | ||
clearBasket, | ||
}} | ||
> | ||
{children} | ||
</BasketContext.Provider> | ||
); | ||
}; | ||
|
||
// Хук для использования контекста | ||
export const useBasket = () => useContext(BasketContext); |
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.