тут нихуя не работает, ебаный рот этого казино
This commit is contained in:
33
frontend/style/contexts/auth-context.tsx
Normal file
33
frontend/style/contexts/auth-context.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
"use client"
|
||||
|
||||
import React, { createContext, useContext, useState } from 'react'
|
||||
|
||||
type AuthContextType = {
|
||||
isLoggedIn: boolean
|
||||
login: () => void
|
||||
logout: () => void
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextType | undefined>(undefined)
|
||||
|
||||
export const useAuth = () => {
|
||||
const context = useContext(AuthContext)
|
||||
if (!context) {
|
||||
throw new Error('useAuth must be used within an AuthProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [isLoggedIn, setIsLoggedIn] = useState(false)
|
||||
|
||||
const login = () => setIsLoggedIn(true)
|
||||
const logout = () => setIsLoggedIn(false)
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isLoggedIn, login, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
99
frontend/style/contexts/cart-context.tsx
Normal file
99
frontend/style/contexts/cart-context.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
"use client"
|
||||
|
||||
import React, { createContext, useContext, useState, useCallback, useEffect } from 'react'
|
||||
import { CartItem, saveCart, getCart, clearCart } from '@/lib/cartStorage'
|
||||
|
||||
type CartContextType = {
|
||||
items: CartItem[]
|
||||
addToCart: (item: Omit<CartItem, 'quantity'>) => void
|
||||
removeFromCart: (id: number) => void
|
||||
removeAllFromCart: (id: number) => void
|
||||
updateQuantity: (id: number, quantity: number) => void
|
||||
clearCart: () => void
|
||||
getTotalItems: () => number
|
||||
}
|
||||
|
||||
const CartContext = createContext<CartContextType | undefined>(undefined)
|
||||
|
||||
export const useCart = () => {
|
||||
const context = useContext(CartContext)
|
||||
if (!context) {
|
||||
throw new Error('useCart must be used within a CartProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
export const CartProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [items, setItems] = useState<CartItem[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
const savedCart = getCart()
|
||||
if (savedCart.length > 0) {
|
||||
setItems(savedCart)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
saveCart(items)
|
||||
}, [items])
|
||||
|
||||
const addToCart = useCallback((newItem: Omit<CartItem, 'quantity'>) => {
|
||||
setItems(currentItems => {
|
||||
const existingItem = currentItems.find(item => item.id === newItem.id)
|
||||
if (existingItem) {
|
||||
return currentItems.map(item =>
|
||||
item.id === newItem.id ? { ...item, quantity: item.quantity + 1 } : item
|
||||
)
|
||||
}
|
||||
return [...currentItems, { ...newItem, quantity: 1 }]
|
||||
})
|
||||
}, [])
|
||||
|
||||
const removeFromCart = useCallback((id: number) => {
|
||||
setItems(currentItems => {
|
||||
const existingItem = currentItems.find(item => item.id === id)
|
||||
if (existingItem && existingItem.quantity > 1) {
|
||||
return currentItems.map(item =>
|
||||
item.id === id ? { ...item, quantity: item.quantity - 1 } : item
|
||||
)
|
||||
}
|
||||
return currentItems.filter(item => item.id !== id)
|
||||
})
|
||||
}, [])
|
||||
|
||||
const removeAllFromCart = useCallback((id: number) => {
|
||||
setItems(currentItems => currentItems.filter(item => item.id !== id))
|
||||
}, [])
|
||||
|
||||
const updateQuantity = useCallback((id: number, quantity: number) => {
|
||||
setItems(currentItems =>
|
||||
currentItems.map(item =>
|
||||
item.id === id ? { ...item, quantity: Math.max(1, quantity) } : item
|
||||
)
|
||||
)
|
||||
}, [])
|
||||
|
||||
const clearCartItems = useCallback(() => {
|
||||
setItems([])
|
||||
clearCart()
|
||||
}, [])
|
||||
|
||||
const getTotalItems = useCallback(() => {
|
||||
return items.reduce((total, item) => total + item.quantity, 0)
|
||||
}, [items])
|
||||
|
||||
return (
|
||||
<CartContext.Provider value={{
|
||||
items,
|
||||
addToCart,
|
||||
removeFromCart,
|
||||
removeAllFromCart,
|
||||
updateQuantity,
|
||||
clearCart: clearCartItems,
|
||||
getTotalItems
|
||||
}}>
|
||||
{children}
|
||||
</CartContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
59
frontend/style/contexts/favorites-context.tsx
Normal file
59
frontend/style/contexts/favorites-context.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
"use client"
|
||||
|
||||
import React, { createContext, useContext, useState, useCallback } from 'react'
|
||||
|
||||
type FavoriteItem = {
|
||||
id: number
|
||||
title: string
|
||||
price: number
|
||||
}
|
||||
|
||||
type FavoritesContextType = {
|
||||
items: FavoriteItem[]
|
||||
addToFavorites: (item: FavoriteItem) => void
|
||||
removeFromFavorites: (id: number) => void
|
||||
isFavorite: (id: number) => boolean
|
||||
getTotalFavorites: () => number
|
||||
}
|
||||
|
||||
const FavoritesContext = createContext<FavoritesContextType | undefined>(undefined)
|
||||
|
||||
export const useFavorites = () => {
|
||||
const context = useContext(FavoritesContext)
|
||||
if (!context) {
|
||||
throw new Error('useFavorites must be used within a FavoritesProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
export const FavoritesProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [items, setItems] = useState<FavoriteItem[]>([])
|
||||
|
||||
const addToFavorites = useCallback((newItem: FavoriteItem) => {
|
||||
setItems(currentItems => {
|
||||
if (!currentItems.some(item => item.id === newItem.id)) {
|
||||
return [...currentItems, newItem]
|
||||
}
|
||||
return currentItems
|
||||
})
|
||||
}, [])
|
||||
|
||||
const removeFromFavorites = useCallback((id: number) => {
|
||||
setItems(currentItems => currentItems.filter(item => item.id !== id))
|
||||
}, [])
|
||||
|
||||
const isFavorite = useCallback((id: number) => {
|
||||
return items.some(item => item.id === id)
|
||||
}, [items])
|
||||
|
||||
const getTotalFavorites = useCallback(() => {
|
||||
return items.length
|
||||
}, [items])
|
||||
|
||||
return (
|
||||
<FavoritesContext.Provider value={{ items, addToFavorites, removeFromFavorites, isFavorite, getTotalFavorites }}>
|
||||
{children}
|
||||
</FavoritesContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user