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 | 4x 10x 10x 10x 10x 10x 10x 14x 10x 2x 10x 10x 1x 30x 1x 9x 45x 5x | 'use client';
import React from 'react';
import { Star, X, Filter } from 'lucide-react';
import { Review } from '../types';
import { ReviewSummary } from './ReviewSummary';
import { useToast } from '../context/ToastContext';
import { useShop } from '../context/ShopContext';
interface ReviewSectionProps {
reviews: Review[];
}
export const ReviewSection: React.FC<ReviewSectionProps> = ({ reviews }) => {
const { refreshReviews } = useShop();
const [isModalOpen, setIsModalOpen] = React.useState(false);
const [filterStars, setFilterStars] = React.useState<number | null>(null);
const [formData, setFormData] = React.useState({
author: '',
rating: 5,
text: ''
});
const [isSubmitting, setIsSubmitting] = React.useState(false);
const { addToast } = useToast();
// Filter approved reviews
const approvedReviews = reviews.filter(r => r.status === 'approved');
// Apply star filter
const filteredReviews = filterStars
? approvedReviews.filter(r => r.rating === filterStars)
: approvedReviews;
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsSubmitting(true);
try {
const response = await fetch('/api/reviews', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
});
if (!response.ok) throw new Error('Failed to submit review');
addToast('Arvostelu lähetetty tarkistettavaksi!', 'success');
setIsModalOpen(false);
setFormData({ author: '', rating: 5, text: '' });
// Refresh reviews to show the new one (though it will be pending)
await refreshReviews();
} catch {
addToast('Arvostelun lähettäminen epäonnistui', 'error');
} finally {
setIsSubmitting(false);
}
};
return (
<div className="space-y-6">
{/* Review Summary */}
<ReviewSummary
reviews={reviews}
onWriteReview={() => setIsModalOpen(true)}
/>
{/* Filter and Reviews */}
{approvedReviews.length > 0 && (
<div className="space-y-4">
{/* Filter buttons */}
<div className="flex items-center gap-2 flex-wrap">
<span className="text-sm font-medium text-gray-600 flex items-center gap-2">
<Filter size={16} /> Suodata:
</span>
<button
onClick={() => setFilterStars(null)}
className={`px-4 py-2 rounded-full text-sm font-bold transition-all ${
filterStars === null
? 'bg-black text-white'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200'
}`}
>
Kaikki
</button>
{[5, 4, 3, 2, 1].map((stars) => (
<button
key={stars}
onClick={() => setFilterStars(stars)}
className={`px-4 py-2 rounded-full text-sm font-bold transition-all flex items-center gap-1 ${
filterStars === stars
? 'bg-black text-white'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200'
}`}
>
{stars} <Star size={14} className="fill-current" />
</button>
))}
</div>
{/* Reviews list - scrollable */}
<div className="max-h-[350px] overflow-y-auto space-y-4 pr-2">
{filteredReviews.map((review) => (
<div
key={review.id}
className="bg-white rounded-xl p-6 border border-gray-200 hover:border-gray-300 transition-colors"
>
<div className="flex items-start gap-4">
<div className="w-12 h-12 rounded-full bg-black text-white flex items-center justify-center font-bold text-lg flex-shrink-0">
{review.author.charAt(0).toUpperCase()}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-3 mb-2">
<span className="font-bold text-black">{review.author}</span>
<div className="flex gap-0.5">
{[...Array(5)].map((_, i) => (
<Star
key={i}
size={14}
className={i < review.rating ? 'fill-yellow-500 text-yellow-500' : 'fill-gray-300 text-gray-300'}
/>
))}
</div>
</div>
<p className="text-gray-700 leading-relaxed">{review.text}</p>
<span className="text-xs text-gray-400 mt-2 block">
{new Date(review.date).toLocaleDateString('fi-FI')}
</span>
</div>
</div>
</div>
))}
</div>
{filteredReviews.length === 0 && (
<p className="text-center text-gray-500 py-8">
Ei arvosteluja valitulla tähtimäärällä
</p>
)}
</div>
)}
{/* Review Modal */}
{isModalOpen && (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm">
<div className="bg-white rounded-2xl max-w-lg w-full p-8 shadow-2xl">
{/* Modal Header */}
<div className="flex items-center justify-between mb-6">
<h3 className="text-2xl font-bold text-black">Kirjoita arvostelu</h3>
<button
onClick={() => setIsModalOpen(false)}
className="w-10 h-10 rounded-full hover:bg-gray-100 flex items-center justify-center transition-colors"
>
<X size={20} />
</button>
</div>
{/* Form */}
<form onSubmit={handleSubmit} className="space-y-6">
{/* Name */}
<div>
<label className="block text-sm font-bold text-gray-700 mb-2">
Nimesi
</label>
<input
type="text"
required
value={formData.author}
onChange={(e) => setFormData({ ...formData, author: e.target.value })}
className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 focus:border-black focus:outline-none transition-colors"
placeholder="Esim. Matti Meikäläinen"
/>
</div>
{/* Rating */}
<div>
<label className="block text-sm font-bold text-gray-700 mb-3">
Arvosana
</label>
<div className="flex gap-2">
{[1, 2, 3, 4, 5].map((rating) => (
<button
key={rating}
type="button"
onClick={() => setFormData({ ...formData, rating })}
className="transition-transform hover:scale-110"
>
<Star
size={32}
className={rating <= formData.rating ? 'fill-yellow-500 text-yellow-500' : 'fill-gray-300 text-gray-300'}
/>
</button>
))}
</div>
</div>
{/* Review text */}
<div>
<label className="block text-sm font-bold text-gray-700 mb-2">
Arvostelusi
</label>
<textarea
required
value={formData.text}
onChange={(e) => setFormData({ ...formData, text: e.target.value })}
className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 focus:border-black focus:outline-none transition-colors resize-none"
rows={5}
placeholder="Kerro kokemuksestasi..."
/>
</div>
{/* Submit */}
<div className="flex gap-3">
<button
type="button"
onClick={() => setIsModalOpen(false)}
className="flex-1 px-6 py-4 rounded-full font-bold text-sm bg-gray-100 text-gray-700 hover:bg-gray-200 transition-all"
>
Peruuta
</button>
<button
type="submit"
disabled={isSubmitting}
className="flex-1 px-6 py-4 rounded-full font-bold text-sm bg-black text-white hover:bg-gray-800 transition-all hover:scale-105 disabled:opacity-50 disabled:cursor-not-allowed shadow-lg"
>
{isSubmitting ? 'Lähetetään...' : 'Lähetä arvostelu'}
</button>
</div>
<p className="text-xs text-gray-500 text-center">
Arvostelusi tarkistetaan ennen julkaisua
</p>
</form>
</div>
</div>
)}
</div>
);
};
|