Новые изменения в проекте
This commit is contained in:
parent
ecb720f751
commit
ddc0a26635
13 changed files with 950 additions and 48 deletions
|
|
@ -1,6 +1,4 @@
|
|||
---
|
||||
import RatingStars from './RatingStars.astro';
|
||||
|
||||
export interface Props {
|
||||
name: string;
|
||||
profession: string;
|
||||
|
|
@ -10,7 +8,7 @@ export interface Props {
|
|||
color: string;
|
||||
date: string;
|
||||
votesCount?: number;
|
||||
isHelpful?: boolean;
|
||||
reviewId?: string;
|
||||
}
|
||||
|
||||
const {
|
||||
|
|
@ -22,7 +20,7 @@ const {
|
|||
color,
|
||||
date,
|
||||
votesCount = 0,
|
||||
isHelpful = false
|
||||
reviewId = ''
|
||||
} = Astro.props;
|
||||
|
||||
// Форматируем дату
|
||||
|
|
@ -53,7 +51,18 @@ const formatDate = (dateStr: string) => {
|
|||
|
||||
<!-- Рейтинг отзыва -->
|
||||
<div class="review-rating">
|
||||
<RatingStars rating={rating} size="md" />
|
||||
<div class="rating-stars-display">
|
||||
{[1, 2, 3, 4, 5].map((star) => (
|
||||
<svg
|
||||
class={`star ${star <= rating ? 'filled' : ''}`}
|
||||
viewBox="0 0 24 24"
|
||||
fill={star <= rating ? 'currentColor' : 'none'}
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z" />
|
||||
</svg>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Текст отзыва -->
|
||||
|
|
@ -62,26 +71,41 @@ const formatDate = (dateStr: string) => {
|
|||
</div>
|
||||
|
||||
<!-- Блок голосования -->
|
||||
<div class="voting-section">
|
||||
<div class="voting-section" data-review-id={reviewId}>
|
||||
<p class="voting-question">Полезен ли этот отзыв?</p>
|
||||
<div class="voting-stars">
|
||||
<RatingStars rating={0} size="lg" interactive />
|
||||
<div class="voting-buttons">
|
||||
<button
|
||||
type="button"
|
||||
class="vote-btn vote-btn-up"
|
||||
data-vote="like"
|
||||
aria-label="Полезно"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="reaction-icon">
|
||||
<path d="M7 10v12"></path>
|
||||
<path d="M15 5.88 14 10h5.83a2 2 0 0 1 1.92 2.56l-2.33 8A2 2 0 0 1 17.5 22H4a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h2.76a2 2 0 0 0 1.79-1.11L12 2a3.13 3.13 0 0 1 3 3.88Z"></path>
|
||||
</svg>
|
||||
<span class="vote-label">Да</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="vote-btn vote-btn-down"
|
||||
data-vote="dislike"
|
||||
aria-label="Бесполезно"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="reaction-icon">
|
||||
<path d="M17 14V2"></path>
|
||||
<path d="M9 18.12 10 14H4.17a2 2 0 0 1-1.92-2.56l2.33-8A2 2 0 0 1 6.5 2H20a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-2.76a2 2 0 0 0-1.79 1.11L12 22a3.13 3.13 0 0 1-3-3.88Z"></path>
|
||||
</svg>
|
||||
<span class="vote-label">Нет</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="voting-stats">
|
||||
<span class="votes-count">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path d="M2 10.5a1.5 1.5 0 113 0v6a1.5 1.5 0 01-3 0v-6zM6 10.333v5.43a2 2 0 001.106 1.79l.05.025A4 4 0 008.943 18h5.416a2 2 0 001.962-1.608l1.2-6A2 2 0 0015.56 8H12V4a2 2 0 00-2-2 1 1 0 00-1 1v.667a4 4 0 01-.8 2.4L6.8 7.933a4 4 0 00-.8 2.4z" />
|
||||
</svg>
|
||||
{votesCount}
|
||||
<span class="votes-number">{votesCount}</span>
|
||||
</span>
|
||||
{isHelpful && (
|
||||
<span class="helpful-badge">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
Полезно
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
|
|
@ -160,6 +184,21 @@ const formatDate = (dateStr: string) => {
|
|||
align-items: center;
|
||||
}
|
||||
|
||||
.rating-stars-display {
|
||||
display: flex;
|
||||
gap: 0.125rem;
|
||||
}
|
||||
|
||||
.rating-stars-display .star {
|
||||
width: 1.25rem;
|
||||
height: 1.25rem;
|
||||
color: #d1d5db;
|
||||
}
|
||||
|
||||
.rating-stars-display .star.filled {
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.review-text {
|
||||
color: #334155;
|
||||
line-height: 1.7;
|
||||
|
|
@ -186,6 +225,83 @@ const formatDate = (dateStr: string) => {
|
|||
margin: 0;
|
||||
}
|
||||
|
||||
.voting-auth-note {
|
||||
color: #dc2626 !important;
|
||||
font-size: 0.7rem !important;
|
||||
font-style: italic;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.voting-buttons {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.vote-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.375rem;
|
||||
padding: 0.5rem 0.875rem;
|
||||
border: 2px solid #e2e8f0;
|
||||
border-radius: 0.5rem;
|
||||
background: #ffffff;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.vote-btn svg {
|
||||
width: 1.25rem;
|
||||
height: 1.25rem;
|
||||
}
|
||||
|
||||
.reaction-icon {
|
||||
width: 1.25rem;
|
||||
height: 1.25rem;
|
||||
}
|
||||
|
||||
.vote-btn-up:hover,
|
||||
.vote-btn-up.active {
|
||||
border-color: #22c55e;
|
||||
color: #22c55e;
|
||||
background: rgba(34, 197, 94, 0.1);
|
||||
}
|
||||
|
||||
.vote-btn-down:hover,
|
||||
.vote-btn-down.active {
|
||||
border-color: #ef4444;
|
||||
color: #ef4444;
|
||||
background: rgba(239, 68, 68, 0.1);
|
||||
}
|
||||
|
||||
.vote-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.vote-label {
|
||||
display: none;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.voting-buttons {
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.vote-btn {
|
||||
width: 100%;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.vote-label {
|
||||
display: inline;
|
||||
}
|
||||
}
|
||||
|
||||
.voting-stars {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
|
@ -195,6 +311,7 @@ const formatDate = (dateStr: string) => {
|
|||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.votes-count {
|
||||
|
|
@ -211,6 +328,36 @@ const formatDate = (dateStr: string) => {
|
|||
height: 1.125rem;
|
||||
}
|
||||
|
||||
.auth-prompt {
|
||||
position: fixed;
|
||||
bottom: 1rem;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background: #1e293b;
|
||||
color: #fff;
|
||||
padding: 1rem 1.5rem;
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
z-index: 1000;
|
||||
animation: fadeInUp 0.3s ease;
|
||||
}
|
||||
|
||||
.auth-prompt a {
|
||||
color: #d4af37;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
@keyframes fadeInUp {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateX(-50%) translateY(1rem);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateX(-50%) translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.helpful-badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
|
|
@ -248,6 +395,22 @@ const formatDate = (dateStr: string) => {
|
|||
}
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.voting-buttons {
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.vote-btn {
|
||||
width: 100%;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.vote-label {
|
||||
display: inline;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.review-card {
|
||||
padding: 1.5rem;
|
||||
|
|
@ -279,3 +442,124 @@ const formatDate = (dateStr: string) => {
|
|||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
function showAuthPrompt(section: Element) {
|
||||
const votingSection = section as HTMLElement;
|
||||
let authNote = votingSection.querySelector('.voting-auth-note');
|
||||
if (!authNote) {
|
||||
authNote = document.createElement('p');
|
||||
authNote.className = 'voting-auth-note';
|
||||
authNote.style.cssText = 'color: #dc2626 !important; font-size: 0.7rem !important; font-style: italic; margin: 0;';
|
||||
authNote.innerHTML = 'Чтобы голосовать, <a href="/auth/sign-in">войдите</a> или <a href="/auth/sign-up">зарегистрируйтесь</a>';
|
||||
votingSection.insertBefore(authNote, votingSection.querySelector('.voting-buttons'));
|
||||
setTimeout(() => authNote?.remove(), 3000);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const votingSections = document.querySelectorAll('.voting-section');
|
||||
|
||||
votingSections.forEach(section => {
|
||||
const reviewId = section.getAttribute('data-review-id');
|
||||
const buttons = section.querySelectorAll('.vote-btn');
|
||||
|
||||
buttons.forEach(btn => {
|
||||
btn.addEventListener('click', async () => {
|
||||
const voteType = btn.getAttribute('data-vote');
|
||||
|
||||
if (!reviewId) {
|
||||
showAuthPrompt(section);
|
||||
return;
|
||||
}
|
||||
|
||||
buttons.forEach(b => (b as HTMLButtonElement).disabled = true);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/reviews/vote', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({ review_id: reviewId, vote_type: voteType })
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
showAuthPrompt(section);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
buttons.forEach(b => b.classList.remove('active'));
|
||||
btn.classList.add('active');
|
||||
|
||||
const votesNumber = section.querySelector('.votes-number');
|
||||
if (votesNumber && typeof data.likes === 'number') {
|
||||
votesNumber.textContent = data.likes.toString();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[ReviewCard] Vote failed:', err);
|
||||
} finally {
|
||||
buttons.forEach(b => (b as HTMLButtonElement).disabled = false);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
document.addEventListener('astro:page-load', () => {
|
||||
const votingSections = document.querySelectorAll('.voting-section');
|
||||
|
||||
votingSections.forEach(section => {
|
||||
const buttons = section.querySelectorAll('.vote-btn');
|
||||
|
||||
buttons.forEach(btn => {
|
||||
btn.addEventListener('click', async () => {
|
||||
const voteType = btn.getAttribute('data-vote');
|
||||
const reviewId = section.getAttribute('data-review-id');
|
||||
|
||||
if (!reviewId) {
|
||||
showAuthPrompt(section);
|
||||
return;
|
||||
}
|
||||
|
||||
buttons.forEach(b => (b as HTMLButtonElement).disabled = true);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/reviews/vote', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({ review_id: reviewId, vote_type: voteType })
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
showAuthPrompt(section);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
buttons.forEach(b => b.classList.remove('active'));
|
||||
btn.classList.add('active');
|
||||
|
||||
const votesNumber = section.querySelector('.votes-number');
|
||||
if (votesNumber && typeof data.likes === 'number') {
|
||||
votesNumber.textContent = data.likes.toString();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[ReviewCard] Vote failed:', err);
|
||||
} finally {
|
||||
buttons.forEach(b => (b as HTMLButtonElement).disabled = false);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
146
frontend/src/components/reviews/ReviewsList.astro
Normal file
146
frontend/src/components/reviews/ReviewsList.astro
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
---
|
||||
import ReviewCard from './ReviewCard.astro';
|
||||
import VotingSummary from './VotingSummary.astro';
|
||||
|
||||
interface ReviewRecord {
|
||||
id: string;
|
||||
name: string;
|
||||
surname?: string;
|
||||
profession?: string;
|
||||
text: string;
|
||||
rating: number;
|
||||
status: string;
|
||||
votesCount: number;
|
||||
created: string;
|
||||
expand?: {
|
||||
user?: {
|
||||
id: string;
|
||||
name: string;
|
||||
firstName?: string;
|
||||
email: string;
|
||||
avatar?: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
const POCKETBASE_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
|
||||
|
||||
interface Props {
|
||||
status?: string;
|
||||
}
|
||||
|
||||
const { status = 'published' } = Astro.props;
|
||||
|
||||
// Загрузка данных с сервера
|
||||
let reviews: ReviewRecord[] = [];
|
||||
let error: string | null = null;
|
||||
let averageRating = 0;
|
||||
let totalVotes = 0;
|
||||
let totalReviews = 0;
|
||||
let ratingDistribution: Record<number, number> = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };
|
||||
|
||||
try {
|
||||
const filter = `status = "${status}"`;
|
||||
const expand = 'user';
|
||||
const sort = '-created';
|
||||
|
||||
const response = await fetch(
|
||||
`${POCKETBASE_URL}/api/collections/reviews/records?expand=${expand}&filter=${encodeURIComponent(filter)}&sort=${sort}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
reviews = data.items || [];
|
||||
|
||||
// Расчёт статистики
|
||||
totalReviews = reviews.length;
|
||||
totalVotes = reviews.reduce((sum, r) => sum + (r.votesCount || 0), 0);
|
||||
const totalRating = reviews.reduce((sum, r) => sum + r.rating, 0);
|
||||
averageRating = totalReviews > 0 ? parseFloat((totalRating / totalReviews).toFixed(1)) : 0;
|
||||
|
||||
ratingDistribution = reviews.reduce((acc, r) => {
|
||||
acc[r.rating] = (acc[r.rating] || 0) + 1;
|
||||
return acc;
|
||||
}, {} as Record<number, number>);
|
||||
} else {
|
||||
error = 'Не удалось загрузить отзывы';
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[ReviewsList] Error:', e);
|
||||
error = 'Ошибка загрузки отзывов';
|
||||
}
|
||||
|
||||
// Цвета для аватарок
|
||||
const colors = [
|
||||
'bg-blue-100 text-blue-600', 'bg-teal-100 text-teal-600', 'bg-orange-100 text-orange-600',
|
||||
'bg-pink-100 text-pink-600', 'bg-purple-100 text-purple-600', 'bg-indigo-100 text-indigo-600',
|
||||
'bg-green-100 text-green-600', 'bg-yellow-100 text-yellow-600', 'bg-red-100 text-red-600'
|
||||
];
|
||||
|
||||
const getAvatarInfo = (name: string) => {
|
||||
const initial = name.charAt(0).toUpperCase();
|
||||
const color = colors[initial.charCodeAt(0) % colors.length];
|
||||
return { initial, color };
|
||||
};
|
||||
---
|
||||
|
||||
{error && (
|
||||
<div class="text-center py-12 text-red-500">
|
||||
<p>{error}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!error && (
|
||||
<>
|
||||
{reviews.length > 0 && (
|
||||
<VotingSummary
|
||||
averageRating={averageRating}
|
||||
totalVotes={totalVotes}
|
||||
totalReviews={totalReviews}
|
||||
ratingDistribution={ratingDistribution}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div class="reviews-grid">
|
||||
{reviews.length > 0 ? (
|
||||
reviews.map((review) => {
|
||||
const avatarInfo = getAvatarInfo(review.name);
|
||||
const fullName = `${review.name} ${review.surname || ''}`.trim();
|
||||
return (
|
||||
<ReviewCard
|
||||
name={fullName}
|
||||
profession={review.profession || 'Клиент'}
|
||||
text={review.text}
|
||||
rating={review.rating}
|
||||
initial={avatarInfo.initial}
|
||||
color={avatarInfo.color}
|
||||
date={review.created}
|
||||
votesCount={review.votesCount || 0}
|
||||
reviewId={review.id}
|
||||
/>
|
||||
);
|
||||
})
|
||||
) : (
|
||||
<div class="text-center py-12 text-gray-500" style="grid-column: 1 / -1;">
|
||||
<p>Пока нет отзывов. Будьте первым!</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
<style>
|
||||
.reviews-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
|
||||
gap: 2rem;
|
||||
margin: 3rem 0;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.reviews-grid {
|
||||
grid-template-columns: 1fr;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
@ -16,8 +16,8 @@ const { averageRating, totalVotes, totalReviews, ratingDistribution } = Astro.pr
|
|||
|
||||
// Расчёт процентов для каждого рейтинга
|
||||
const getPercentage = (count: number) => {
|
||||
if (totalVotes === 0) return 0;
|
||||
return Math.round((count / totalVotes) * 100);
|
||||
if (totalReviews === 0) return 0;
|
||||
return Math.round((count / totalReviews) * 100);
|
||||
};
|
||||
---
|
||||
|
||||
|
|
@ -45,7 +45,7 @@ const getPercentage = (count: number) => {
|
|||
</div>
|
||||
</div>
|
||||
<p class="total-votes-text">
|
||||
На основе <strong>{totalVotes}</strong> голосов
|
||||
На основе <strong>{totalReviews}</strong> отзывов
|
||||
</p>
|
||||
<p class="total-reviews-text">
|
||||
<strong>{totalReviews}</strong> отзывов оставлено
|
||||
|
|
@ -54,18 +54,21 @@ const getPercentage = (count: number) => {
|
|||
|
||||
<!-- Правая колонка - Распределение оценок -->
|
||||
<div class="distribution">
|
||||
{[5, 4, 3, 2, 1].map((rating) => (
|
||||
<div class="distribution-row">
|
||||
<span class="rating-label">{rating} ★</span>
|
||||
<div class="progress-bar">
|
||||
<div
|
||||
class="progress-fill"
|
||||
style={`width: ${getPercentage(ratingDistribution[rating as keyof typeof ratingDistribution])}%`}
|
||||
></div>
|
||||
{[5, 4, 3, 2, 1].map((rating) => {
|
||||
const count = ratingDistribution[rating as keyof typeof ratingDistribution] || 0;
|
||||
return (
|
||||
<div class="distribution-row">
|
||||
<span class="rating-label">{rating} ★</span>
|
||||
<div class="progress-bar">
|
||||
<div
|
||||
class="progress-fill"
|
||||
style={`width: ${getPercentage(count)}%`}
|
||||
></div>
|
||||
</div>
|
||||
<span class="rating-count">{count}</span>
|
||||
</div>
|
||||
<span class="rating-count">{ratingDistribution[rating as keyof typeof ratingDistribution]}</span>
|
||||
</div>
|
||||
))}
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue