feat: implement compare functionality, checkout flow, and add 30 blog posts
This commit is contained in:
+19
-71
@@ -2,16 +2,12 @@ import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
|
||||
export interface CartItem {
|
||||
id: string; // Unikalne ID, połączenie slug + wybrane opcje
|
||||
id: string;
|
||||
slug: string;
|
||||
name: string;
|
||||
brand: string;
|
||||
image: string;
|
||||
power?: number;
|
||||
label?: string; // Np. "2.5 kW" z wariantu
|
||||
price: number; // Podstawowa cena za 1 szt. bez montażu
|
||||
priceWithInstall: number; // Cena za 1 szt. z montażem
|
||||
withInstall: boolean;
|
||||
price: number;
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
@@ -20,9 +16,9 @@ interface CartState {
|
||||
addItem: (item: Omit<CartItem, 'id' | 'quantity'>) => void;
|
||||
removeItem: (id: string) => void;
|
||||
updateQuantity: (id: string, quantity: number) => void;
|
||||
toggleInstall: (id: string) => void;
|
||||
clearCart: () => void;
|
||||
getTotals: () => { subtotal: number; vat: number; total: number };
|
||||
getTotal: () => number;
|
||||
getCount: () => number;
|
||||
}
|
||||
|
||||
export const useCartStore = create<CartState>()(
|
||||
@@ -32,89 +28,41 @@ export const useCartStore = create<CartState>()(
|
||||
|
||||
addItem: (newItem) => {
|
||||
set((state) => {
|
||||
const id = `${newItem.slug}-${newItem.label || 'base'}-${newItem.withInstall}`;
|
||||
const existingItem = state.items.find((item) => item.id === id);
|
||||
|
||||
if (existingItem) {
|
||||
const id = newItem.slug;
|
||||
const existing = state.items.find((i) => i.id === id);
|
||||
if (existing) {
|
||||
return {
|
||||
items: state.items.map((item) =>
|
||||
item.id === id ? { ...item, quantity: item.quantity + 1 } : item
|
||||
items: state.items.map((i) =>
|
||||
i.id === id ? { ...i, quantity: i.quantity + 1 } : i
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
return { items: [...state.items, { ...newItem, id, quantity: 1 }] };
|
||||
});
|
||||
},
|
||||
|
||||
removeItem: (id) => {
|
||||
set((state) => ({
|
||||
items: state.items.filter((item) => item.id !== id),
|
||||
}));
|
||||
},
|
||||
removeItem: (id) =>
|
||||
set((state) => ({ items: state.items.filter((i) => i.id !== id) })),
|
||||
|
||||
updateQuantity: (id, quantity) => {
|
||||
if (quantity < 1) return;
|
||||
set((state) => ({
|
||||
items: state.items.map((item) =>
|
||||
item.id === id ? { ...item, quantity } : item
|
||||
),
|
||||
items: state.items.map((i) => (i.id === id ? { ...i, quantity } : i)),
|
||||
}));
|
||||
},
|
||||
|
||||
toggleInstall: (id) => {
|
||||
set((state) => {
|
||||
const itemToUpdate = state.items.find(item => item.id === id);
|
||||
if (!itemToUpdate) return state;
|
||||
|
||||
const newWithInstall = !itemToUpdate.withInstall;
|
||||
const newId = `${itemToUpdate.slug}-${itemToUpdate.label || 'base'}-${newWithInstall}`;
|
||||
|
||||
const targetExists = state.items.find(item => item.id === newId);
|
||||
|
||||
if (targetExists) {
|
||||
return {
|
||||
items: state.items
|
||||
.map(item => item.id === newId ? { ...item, quantity: item.quantity + itemToUpdate.quantity } : item)
|
||||
.filter(item => item.id !== id)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
items: state.items.map((item) =>
|
||||
item.id === id ? { ...item, withInstall: newWithInstall, id: newId } : item
|
||||
),
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
clearCart: () => set({ items: [] }),
|
||||
|
||||
getTotals: () => {
|
||||
getTotal: () => {
|
||||
const { items } = get();
|
||||
let subtotal = 0;
|
||||
let vat = 0;
|
||||
|
||||
items.forEach((item) => {
|
||||
const itemPrice = item.withInstall ? item.priceWithInstall : item.price;
|
||||
const itemTotalGross = itemPrice * item.quantity;
|
||||
const vatRate = item.withInstall ? 0.08 : 0.23;
|
||||
const itemTotalNet = itemTotalGross / (1 + vatRate);
|
||||
const itemVat = itemTotalGross - itemTotalNet;
|
||||
return items.reduce((sum, i) => sum + i.price * i.quantity, 0);
|
||||
},
|
||||
|
||||
subtotal += itemTotalNet;
|
||||
vat += itemVat;
|
||||
});
|
||||
|
||||
return {
|
||||
subtotal,
|
||||
vat,
|
||||
total: subtotal + vat
|
||||
};
|
||||
getCount: () => {
|
||||
const { items } = get();
|
||||
return items.reduce((sum, i) => sum + i.quantity, 0);
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'air-project-cart',
|
||||
}
|
||||
{ name: 'thermcool-cart' }
|
||||
)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user