Skip to content

Commit

Permalink
Merge pull request #73 from fs-jun24-team-3/feature/orderPage
Browse files Browse the repository at this point in the history
Feature/order page
  • Loading branch information
k-marchuk authored Sep 25, 2024
2 parents 7cfa244 + 592739c commit 0e489de
Show file tree
Hide file tree
Showing 13 changed files with 658 additions and 113 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"classnames": "^2.5.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.53.0",
"react-redux": "^9.1.2",
"react-router-dom": "^6.26.2",
"redux": "^5.0.1",
Expand Down
4 changes: 4 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { CartPage } from './pages/CartPage/CartPage';
import { NotFoundPage } from './pages/NotFoundPage';
import { PhonesPage } from './pages/PhonesPage/PhonesPage';
import { FavoritesPage } from './pages/FavoritesPage';
import { UserPage } from './pages/UserPage/UserPage';
import { OrderPage } from './pages/OrderPage/OrderPage';

export const Root = () => {
return (
Expand All @@ -38,6 +40,8 @@ export const Root = () => {
</Route>
<Route path="favorites" element={<FavoritesPage />} />
<Route path="cart" element={<CartPage />} />
<Route path="user" element={<UserPage />} />
<Route path="order" element={<OrderPage />} />

<Route path="*" element={<NotFoundPage />} />
</Route>
Expand Down
124 changes: 124 additions & 0 deletions src/app/slices/orderSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { CartItems } from '../../utils/types/CartItem';

// Тип для користувача
type User = {
name: string;
surname: string;
phone: string;
post: string;
city: string;
};

// Тип для замовлення
type Order = {
user: User | null;
cartItems: CartItems[]; // Використовуємо тип CartItems
totalPrice: number;
};

// Функція для отримання даних корзини з LocalStorage
const getCartItemsFromLocalStorage = (): CartItems[] => {
const storedCartItems = localStorage.getItem('orderCart');
return storedCartItems ? JSON.parse(storedCartItems) : [];
};

// Функція для отримання даних користувача з LocalStorage
const getUserFromLocalStorage = (): User | null => {
const storedUser = localStorage.getItem('userData');
return storedUser ? JSON.parse(storedUser) : null;
};

// Ініціалізація початкового стану з LocalStorage
const initialState: Order = {
user: getUserFromLocalStorage(), // Ініціалізація даними з LocalStorage
cartItems: getCartItemsFromLocalStorage(), // Ініціалізація даними з LocalStorage
totalPrice: 0,
};

export const orderSlice = createSlice({
name: 'order',
initialState,
reducers: {
addToOrder: (state, action: PayloadAction<CartItems[]>) => {
action.payload.forEach(newProduct => {
const existingItem = state.cartItems.find(
cartItem => cartItem.item.id === newProduct.item.id, // Працюємо через item.id
);

if (existingItem) {
existingItem.count += newProduct.count;
} else {
state.cartItems.push(newProduct);
}
});

localStorage.setItem('orderCart', JSON.stringify(state.cartItems));
},
removeFromOrder: (state, action: PayloadAction<string>) => {
// id у вас типу string
state.cartItems = state.cartItems.filter(
cartItem => cartItem.item.id !== action.payload, // Працюємо через item.id
);
localStorage.setItem('orderCart', JSON.stringify(state.cartItems));
},
increaseQuantity: (state, action: PayloadAction<string>) => {
const existingItem = state.cartItems.find(
cartItem => cartItem.item.id === action.payload, // Працюємо через item.id
);

if (existingItem) {
existingItem.count += 1;
}

localStorage.setItem('orderCart', JSON.stringify(state.cartItems));
},
decreaseQuantity: (state, action: PayloadAction<string>) => {
const existingItem = state.cartItems.find(
cartItem => cartItem.item.id === action.payload, // Працюємо через item.id
);

if (existingItem && existingItem.count > 1) {
existingItem.count -= 1;
} else {
state.cartItems = state.cartItems.filter(
cartItem => cartItem.item.id !== action.payload, // Працюємо через item.id
);
}

localStorage.setItem('orderCart', JSON.stringify(state.cartItems));
},
clearOrder: state => {
state.cartItems = [];
localStorage.removeItem('orderCart');
},
setUser: (state, action: PayloadAction<User | null>) => {
state.user = action.payload;
localStorage.setItem('userData', JSON.stringify(action.payload));
},
},
selectors: {
totalCost: (state: Order) => {
return state.cartItems.reduce((total, cartItem) => {
return total + cartItem.item.priceRegular * cartItem.count;
}, 0);
},
totalQuantity: (state: Order) => {
return state.cartItems.reduce((total, cartItem) => {
return total + cartItem.count;
}, 0);
},
},
});

// Експорт дій для використання в компонентах
export const {
setUser,
addToOrder,
removeFromOrder,
increaseQuantity,
decreaseQuantity,
clearOrder,
} = orderSlice.actions;

export default orderSlice.reducer;
2 changes: 2 additions & 0 deletions src/app/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { productsSlice } from './slices/productsSlice';
import { tabletsSlice } from './slices/tabletsSlice';
import { cartSlice } from './slices/cartSlise';
import { favoritesSlice } from './slices/favoritesSlice';
import { orderSlice } from './slices/orderSlice';

const rootReducer = combineSlices(
accessoriesSlice,
Expand All @@ -13,6 +14,7 @@ const rootReducer = combineSlices(
tabletsSlice,
cartSlice,
favoritesSlice,
orderSlice,
);

export const store = configureStore({
Expand Down
Loading

0 comments on commit 0e489de

Please sign in to comment.