1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| import React, { useReducer, useContext, createContext } from 'react';
const initialState = { user: { profile: null, preferences: {}, isLoading: false, error: null }, products: { items: [], filters: { category: '', priceRange: [0, 1000], rating: 0 }, pagination: { page: 1, pageSize: 20, total: 0 }, isLoading: false, error: null }, cart: { items: [], total: 0, isLoading: false } };
const ActionTypes = { SET_USER_LOADING: 'SET_USER_LOADING', SET_USER_PROFILE: 'SET_USER_PROFILE', SET_USER_ERROR: 'SET_USER_ERROR', SET_PRODUCTS_LOADING: 'SET_PRODUCTS_LOADING', SET_PRODUCTS: 'SET_PRODUCTS', SET_PRODUCTS_FILTER: 'SET_PRODUCTS_FILTER', SET_PRODUCTS_PAGINATION: 'SET_PRODUCTS_PAGINATION', ADD_TO_CART: 'ADD_TO_CART', REMOVE_FROM_CART: 'REMOVE_FROM_CART', UPDATE_CART_QUANTITY: 'UPDATE_CART_QUANTITY', CLEAR_CART: 'CLEAR_CART' };
const appReducer = (state, action) => { switch (action.type) { case ActionTypes.SET_USER_LOADING: return { ...state, user: { ...state.user, isLoading: action.payload } };
case ActionTypes.SET_USER_PROFILE: return { ...state, user: { ...state.user, profile: action.payload, isLoading: false, error: null } };
case ActionTypes.SET_PRODUCTS: return { ...state, products: { ...state.products, items: action.payload.items, pagination: { ...state.products.pagination, total: action.payload.total }, isLoading: false, error: null } };
case ActionTypes.SET_PRODUCTS_FILTER: return { ...state, products: { ...state.products, filters: { ...state.products.filters, ...action.payload }, pagination: { ...state.products.pagination, page: 1 } } };
case ActionTypes.ADD_TO_CART: const existingItem = state.cart.items.find( item => item.id === action.payload.id );
let updatedItems; if (existingItem) { updatedItems = state.cart.items.map(item => item.id === action.payload.id ? { ...item, quantity: item.quantity + 1 } : item ); } else { updatedItems = [...state.cart.items, { ...action.payload, quantity: 1 }]; }
const newTotal = updatedItems.reduce( (sum, item) => sum + (item.price * item.quantity), 0 );
return { ...state, cart: { ...state.cart, items: updatedItems, total: newTotal } };
default: return state; } };
const AppContext = createContext();
export const AppProvider = ({ children }) => { const [state, dispatch] = useReducer(appReducer, initialState);
const actions = { setUserLoading: (loading) => dispatch({ type: ActionTypes.SET_USER_LOADING, payload: loading }), setUserProfile: (profile) => dispatch({ type: ActionTypes.SET_USER_PROFILE, payload: profile }), setProducts: (products) => dispatch({ type: ActionTypes.SET_PRODUCTS, payload: products }), setProductsFilter: (filters) => dispatch({ type: ActionTypes.SET_PRODUCTS_FILTER, payload: filters }), addToCart: (product) => dispatch({ type: ActionTypes.ADD_TO_CART, payload: product }) };
return ( <AppContext.Provider value={{ state, actions }}> {children} </AppContext.Provider> ); };
export const useAppState = () => { const context = useContext(AppContext); if (!context) { throw new Error('useAppState must be used within AppProvider'); } return context; };
|