Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | 'use client';
import React, { useState, useMemo } from 'react';
import { useShop } from '../context/ShopContext';
import { Section } from '../components/Section';
import Link from 'next/link';
import Image from 'next/image';
import { Trash2, CheckCircle2, ChevronRight, ArrowLeft, Minus, Plus } from 'lucide-react';
const Cart: React.FC = () => {
const { cart, removeFromCart, updateQuantity, clearCart, submitOrder } = useShop();
const [step, setStep] = useState<'cart' | 'checkout' | 'success'>('cart');
// Form State
const [formData, setFormData] = useState({
name: '',
email: '',
phone: '',
address: '',
zip: '',
city: '',
notes: ''
});
const [shipping, setShipping] = useState('postitus');
const [termsAccepted, setTermsAccepted] = useState(false);
// OPTIMIZATION: Memoize total to avoid recalculation on every keystroke in the form
const total = useMemo(() => cart.reduce((sum, item) => sum + (item.salePrice || item.price) * item.quantity, 0), [cart]);
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (!termsAccepted) {
alert("Sinun on hyväksyttävä toimitusehdot jatkaaksesi.");
return;
}
submitOrder({
name: formData.name,
email: formData.email,
phone: formData.phone,
address: formData.address,
city: formData.city,
zip: formData.zip,
notes: formData.notes
}, shipping);
setStep('success');
window.scrollTo(0, 0);
};
Iif (step === 'success') {
return (
<Section className="min-h-[60vh] flex items-center justify-center text-center">
<div className="bg-white p-12 rounded-[2.5rem] border border-gray-200 shadow-2xl max-w-lg mx-auto animate-in fade-in zoom-in duration-500">
{/* CHANGED: Removed Green, now Black for professional look */}
<div className="w-20 h-20 bg-black text-white rounded-full flex items-center justify-center mx-auto mb-8 shadow-xl shadow-black/20">
<CheckCircle2 size={40} strokeWidth={3} />
</div>
<h2 className="text-4xl font-black mb-4 tracking-tight">Kiitos tilauksestasi!</h2>
<p className="text-gray-600 mb-8 max-w-md mx-auto text-lg leading-relaxed font-medium">
Olemme vastaanottaneet tilauksesi. Lähetämme tilausvahvistuksen ja laskun osoitteeseen <span className="text-black font-bold">{formData.email}</span>.
</p>
<Link href="/" className="inline-block bg-black text-white px-8 py-4 rounded-xl font-bold hover:bg-gray-800 transition-all">
Palaa etusivulle
</Link>
</div>
</Section>
);
}
Eif (cart.length === 0) {
return (
<Section className="min-h-[50vh] flex flex-col items-center justify-center">
<h2 className="text-3xl font-black mb-4">Ostoskori on tyhjä</h2>
<p className="text-gray-500 mb-8 font-medium">Lisää tuotteita koriin aloittaaksesi tilauksen.</p>
<Link href="/kauppa" className="bg-black text-white px-8 py-4 rounded-xl font-bold shadow-lg hover:shadow-xl transition-all hover:-translate-y-1">
Selaa tuotteita
</Link>
</Section>
);
}
return (
<Section variant="white" className="pt-12">
{/* PROFESSIONAL STEPPER DESIGN (Refined & Smaller) */}
<div className="max-w-4xl mx-auto mb-16 px-4 md:px-0">
{/* 1. Progress Bar (Top) - Thinner and cleaner */}
<div className="w-full h-1.5 bg-gray-100 rounded-full mb-6 relative overflow-hidden">
<div
className="absolute top-0 left-0 h-full bg-black rounded-full transition-all duration-700 ease-in-out"
style={{ width: step === 'cart' ? '33%' : step === 'checkout' ? '66%' : '100%' }}
></div>
</div>
{/* 2. Steps Row (Bottom) - Smaller circles, cleaner typography */}
<div className="flex justify-between items-center relative">
{/* Step 1: Ostoskori */}
<div className="flex items-center gap-3 group">
<div className={`
w-8 h-8 rounded-full flex items-center justify-center font-bold text-sm transition-colors duration-300 shadow-sm
bg-black text-white
`}>
1
</div>
<span className={`font-bold text-sm uppercase tracking-wider text-black`}>
Ostoskori
</span>
</div>
{/* Step 2: Tilaus */}
<div className="flex items-center gap-3 group">
<div className={`
w-8 h-8 rounded-full flex items-center justify-center font-bold text-sm transition-colors duration-300 shadow-sm
${step === 'checkout' ? 'bg-black text-white' : 'border border-gray-200 text-gray-400 bg-white'}
`}>
2
</div>
<span className={`font-bold text-sm uppercase tracking-wider ${step === 'checkout' ? 'text-black' : 'text-gray-300'}`}>
Tilaus
</span>
</div>
{/* Step 3: Valmis */}
<div className="flex items-center gap-3 group">
<div className={`
w-8 h-8 rounded-full flex items-center justify-center font-bold text-sm transition-colors duration-300 shadow-sm
border border-gray-200 text-gray-400 bg-white
`}>
3
</div>
<span className={`font-bold text-sm uppercase tracking-wider text-gray-300`}>
Valmis
</span>
</div>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-12 gap-12 lg:gap-16 items-start">
{/* LEFT COLUMN: Items or Form */}
<div className="lg:col-span-7 xl:col-span-8">
{step === 'cart' ? (
<div className="space-y-6">
<div className="flex justify-between items-center mb-6 border-b border-gray-100 pb-4">
<div className="flex items-baseline gap-4">
<h1 className="text-2xl font-black tracking-tight uppercase">Tuotteet</h1>
<span className="text-gray-500 font-bold text-xs uppercase tracking-wider">{cart.length} tuotetta</span>
</div>
<button onClick={clearCart} className="text-xs font-bold text-red-500 hover:text-red-700 uppercase tracking-wider">
Tyhjennä
</button>
</div>
{cart.map(item => {
// Compute effective stock for this cart line item
const sizeVariant = item.selectedSize && item.sizes
? item.sizes.find(s => s.name === item.selectedSize)
: undefined;
const effectiveStock = (() => {
// Both size + color: use nested colorStocks
if (sizeVariant?.colorStocks && item.selectedColor) {
return sizeVariant.colorStocks.find(c => c.colorName === item.selectedColor)?.stock ?? 0;
}
// Size only
if (sizeVariant) return sizeVariant.stock;
// Color only
if (item.selectedColor && item.colors) {
return item.colors.find(c => c.name === item.selectedColor)?.stock ?? item.stock;
}
return item.stock;
})();
return (
<div key={`${item.id}-${item.selectedColor || 'default'}-${item.selectedSize || 'default'}`} className="flex flex-col sm:flex-row gap-6 bg-white p-6 rounded-[2rem] border border-gray-200 shadow-sm items-center hover:shadow-md transition-shadow group relative">
<div className="relative w-24 h-24 md:w-32 md:h-32 bg-gray-50 rounded-2xl overflow-hidden flex-shrink-0 border border-gray-100 p-2 flex items-center justify-center group-hover:border-gray-300 transition-colors">
<Image
src={item.images[0] || '/placeholder.svg'}
alt={item.name}
fill
sizes="(max-width: 768px) 96px, 128px"
className="object-contain mix-blend-multiply"
/>
</div>
<div className="flex-grow w-full">
<div className="flex justify-between items-start mb-1">
<div>
<Link href={`/tuote/${item.id}`} className="font-bold text-lg hover:underline decoration-2 decoration-black underline-offset-4">{item.name}</Link>
{item.selectedColor && item.colors && (
<div className="flex items-center gap-2 mt-1">
<div
className="w-4 h-4 rounded-full border border-gray-300"
style={{ backgroundColor: item.colors.find(c => c.name === item.selectedColor)?.hex }}
/>
<span className="text-sm text-gray-600 font-medium">{item.selectedColor}</span>
</div>
)}
{item.selectedSize && (
<div className="flex items-center gap-2 mt-1">
<span className="text-sm text-gray-600 font-medium">Koko: {item.selectedSize}</span>
</div>
)}
</div>
<button onClick={() => removeFromCart(item.id, item.selectedColor, item.selectedSize)} className="text-gray-300 hover:text-red-500 transition-colors p-2 -mr-2">
<Trash2 size={20} />
</button>
</div>
<p className="text-xs text-gray-400 font-bold uppercase tracking-wider mb-4">{item.category}</p>
<div className="flex justify-between items-end">
{/* Quantity Controls */}
<div className="flex items-center gap-3 bg-gray-50 rounded-xl p-1 border border-gray-100">
<button
onClick={() => updateQuantity(item.id, item.quantity - 1, item.selectedColor, item.selectedSize)}
className="w-8 h-8 flex items-center justify-center bg-white rounded-lg shadow-sm border border-gray-200 hover:bg-gray-100 transition-colors active:scale-95"
>
<Minus size={14} strokeWidth={3} />
</button>
<span className="w-6 text-center text-sm font-bold">{item.quantity}</span>
<button
onClick={() => updateQuantity(item.id, item.quantity + 1, item.selectedColor, item.selectedSize)}
disabled={item.quantity >= effectiveStock}
className="w-8 h-8 flex items-center justify-center bg-white rounded-lg shadow-sm border border-gray-200 hover:bg-gray-100 transition-colors active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed"
>
<Plus size={14} strokeWidth={3} />
</button>
</div>
<div className="text-right">
{item.quantity >= effectiveStock && (
<p className="text-[10px] text-red-500 font-bold uppercase mb-1">Varasto täynnä</p>
)}
<span className="font-black text-xl">{(item.salePrice || item.price) * item.quantity} €</span>
</div>
</div>
</div>
</div>
);
})}
</div>
) : (
<div className="animate-in slide-in-from-right-4 fade-in duration-300">
<button onClick={() => setStep('cart')} className="flex items-center gap-2 text-gray-500 font-bold hover:text-black mb-8 group text-sm uppercase tracking-wide">
<ArrowLeft size={16} className="group-hover:-translate-x-1 transition-transform" /> Muokkaa ostoskoria
</button>
<form id="checkout-form" onSubmit={handleSubmit} className="space-y-8">
{/* Section 1: Contact */}
<div className="bg-white p-8 md:p-10 rounded-[2.5rem] border border-gray-200 shadow-sm">
<h3 className="font-black mb-8 text-lg uppercase tracking-wider flex items-center gap-3 border-b border-gray-100 pb-4">
<span className="text-gray-300">01.</span>
Yhteystiedot
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<div className="md:col-span-2">
<label className="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-500">Koko nimi</label>
<input required type="text" name="name" value={formData.name} onChange={handleInputChange} className="w-full p-4 border border-gray-200 rounded-xl bg-gray-50 focus:bg-white focus:border-black focus:ring-0 transition-colors font-bold" placeholder="Matti Meikäläinen" />
</div>
<div>
<label className="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-500">Sähköposti</label>
<input required type="email" name="email" value={formData.email} onChange={handleInputChange} className="w-full p-4 border border-gray-200 rounded-xl bg-gray-50 focus:bg-white focus:border-black focus:ring-0 transition-colors font-bold" placeholder="matti@esimerkki.fi" />
</div>
<div>
<label className="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-500">Puhelin</label>
<input required type="tel" name="phone" value={formData.phone} onChange={handleInputChange} className="w-full p-4 border border-gray-200 rounded-xl bg-gray-50 focus:bg-white focus:border-black focus:ring-0 transition-colors font-bold" placeholder="040 123 4567" />
</div>
</div>
</div>
{/* Section 2: Shipping Address */}
<div className="bg-white p-8 md:p-10 rounded-[2.5rem] border border-gray-200 shadow-sm">
<h3 className="font-black mb-8 text-lg uppercase tracking-wider flex items-center gap-3 border-b border-gray-100 pb-4">
<span className="text-gray-300">02.</span>
Toimitusosoite
</h3>
<div className="space-y-6">
<div>
<label className="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-500">Katuosoite</label>
<input required type="text" name="address" value={formData.address} onChange={handleInputChange} className="w-full p-4 border border-gray-200 rounded-xl bg-gray-50 focus:bg-white focus:border-black focus:ring-0 transition-colors font-bold" placeholder="Esimerkkikatu 1 A" />
</div>
<div className="grid grid-cols-2 gap-6">
<div>
<label className="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-500">Postinumero</label>
<input required type="text" name="zip" value={formData.zip} onChange={handleInputChange} className="w-full p-4 border border-gray-200 rounded-xl bg-gray-50 focus:bg-white focus:border-black focus:ring-0 transition-colors font-bold" placeholder="00100" />
</div>
<div>
<label className="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-500">Kunta</label>
<input required type="text" name="city" value={formData.city} onChange={handleInputChange} className="w-full p-4 border border-gray-200 rounded-xl bg-gray-50 focus:bg-white focus:border-black focus:ring-0 transition-colors font-bold" placeholder="Helsinki" />
</div>
</div>
</div>
</div>
{/* Section 3: Delivery Method */}
<div className="bg-white p-8 md:p-10 rounded-[2.5rem] border border-gray-200 shadow-sm">
<h3 className="font-black mb-8 text-lg uppercase tracking-wider flex items-center gap-3 border-b border-gray-100 pb-4">
<span className="text-gray-300">03.</span>
Toimitustapa
</h3>
<div className="space-y-4">
<label className={`flex items-center p-5 border-2 rounded-2xl cursor-pointer transition-all ${shipping === 'postitus' ? 'border-black bg-gray-50 shadow-md' : 'border-gray-100 hover:border-gray-200'}`}>
<input type="radio" name="shipping" value="postitus" checked={shipping === 'postitus'} onChange={(e) => setShipping(e.target.value)} className="mr-5 w-6 h-6 accent-black" />
<div className="flex-grow">
<span className="font-bold text-lg block">Postipaketti</span>
<span className="text-gray-500 text-sm font-medium">Toimitus lähimpään noutopisteeseen</span>
</div>
<span className="font-black text-lg">10,00 €</span>
</label>
<label className={`flex items-center p-5 border-2 rounded-2xl cursor-pointer transition-all ${shipping === 'nouto' ? 'border-black bg-gray-50 shadow-md' : 'border-gray-100 hover:border-gray-200'}`}>
<input type="radio" name="shipping" value="nouto" checked={shipping === 'nouto'} onChange={(e) => setShipping(e.target.value)} className="mr-5 w-6 h-6 accent-black" />
<div className="flex-grow">
<span className="font-bold text-lg block">Nouto myymälästä</span>
<span className="text-gray-500 text-sm font-medium">Helsingin myymälä (arkisin 10-17)</span>
</div>
<span className="font-black text-lg">0,00 €</span>
</label>
</div>
</div>
<div className="bg-white p-8 md:p-10 rounded-[2.5rem] border border-gray-200 shadow-sm">
<label className="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-500">Lisätiedot tilaukseen (valinnainen)</label>
<textarea name="notes" value={formData.notes} onChange={handleInputChange} className="w-full p-4 border border-gray-200 rounded-xl h-32 bg-gray-50 focus:bg-white focus:border-black focus:ring-0 transition-colors font-medium"></textarea>
</div>
</form>
</div>
)}
</div>
{/* RIGHT COLUMN: Summary & Action */}
<div className="lg:col-span-5 xl:col-span-4">
<div className="bg-white p-8 rounded-[2.5rem] border border-gray-200 sticky top-28 shadow-xl shadow-gray-100/50">
<h3 className="font-black mb-8 text-xl uppercase tracking-tight">Yhteenveto</h3>
<div className="space-y-4 mb-8 text-base font-medium">
<div className="flex justify-between">
<span className="text-gray-500">Välisumma</span>
<span className="font-bold">{total} €</span>
</div>
{step === 'checkout' && (
<div className="flex justify-between animate-in slide-in-from-top-2">
<span className="text-gray-500">Toimitus</span>
<span className="font-bold">{shipping === 'postitus' ? '10' : '0'} €</span>
</div>
)}
<div className="flex justify-between items-end pt-6 border-t border-gray-100">
<span className="font-bold text-lg">Yhteensä</span>
<span className="font-black text-3xl tracking-tight">{total + (step === 'checkout' && shipping === 'postitus' ? 10 : 0)} €</span>
</div>
<p className="text-xs text-gray-400 text-right font-bold uppercase tracking-wider">Sis. alv 24%</p>
</div>
{/* Trust Badge / Payment Method - Highlighting Benefit */}
<div className="mb-8 bg-gray-50 text-black p-5 rounded-2xl border border-gray-100 flex gap-4 items-start">
<CheckCircle2 size={24} className="flex-shrink-0 mt-0.5" />
<div>
<h4 className="font-bold text-sm uppercase tracking-wide mb-1">Riskitön lasku</h4>
<p className="text-xs text-gray-600 font-medium leading-relaxed">
Maksa vasta kun olet saanut tuotteet. Lasku toimitetaan sähköpostitse. Ei piilokuluja.
</p>
</div>
</div>
{step === 'cart' ? (
<button
onClick={() => { setStep('checkout'); window.scrollTo(0, 0); }}
className="w-full bg-black text-white py-5 rounded-2xl font-black text-lg hover:bg-gray-800 shadow-xl hover:shadow-2xl hover:-translate-y-1 transition-all flex items-center justify-center gap-2 group"
>
Siirry kassalle <ChevronRight className="group-hover:translate-x-1 transition-transform" />
</button>
) : (
<div className="space-y-4">
<label className="flex items-start gap-3 cursor-pointer group">
<div className={`mt-0.5 w-5 h-5 rounded border-2 flex items-center justify-center transition-colors flex-shrink-0 ${termsAccepted ? 'bg-black border-black' : 'border-gray-300 bg-white group-hover:border-black'}`}>
{termsAccepted && <CheckCircle2 size={16} className="text-white" />}
</div>
<input type="checkbox" checked={termsAccepted} onChange={(e) => setTermsAccepted(e.target.checked)} className="hidden" />
<span className="text-xs font-medium text-gray-600">
Olen lukenut ja hyväksyn <Link href="/toimitusehdot" className="underline hover:text-black">toimitusehdot</Link> ja <Link href="/tietosuojaseloste" className="underline hover:text-black">tietosuojaselosteen</Link>.
</span>
</label>
<button
type="submit"
form="checkout-form"
className="w-full bg-black text-white py-5 rounded-2xl font-black text-lg hover:bg-green-600 shadow-xl hover:shadow-2xl hover:-translate-y-1 transition-all flex items-center justify-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed"
disabled={!termsAccepted}
>
Vahvista tilaus
</button>
</div>
)}
<div className="mt-6 flex justify-center gap-2 text-gray-400">
<span className="text-[10px] font-bold uppercase tracking-widest flex items-center gap-1">
<div className="w-2 h-2 rounded-full bg-black"></div> SSL-suojattu
</span>
</div>
</div>
</div>
</div>
</Section>
);
};
export default Cart;
|