This commit is contained in:
@@ -475,6 +475,7 @@ export interface OrderDetail {
|
|||||||
subTotal: number;
|
subTotal: number;
|
||||||
tax: number;
|
tax: number;
|
||||||
deliveryFee: number;
|
deliveryFee: number;
|
||||||
|
prepareTime: number | null;
|
||||||
total: number;
|
total: number;
|
||||||
totalItems: number;
|
totalItems: number;
|
||||||
description: string;
|
description: string;
|
||||||
|
|||||||
@@ -133,6 +133,14 @@ function OrderTrackingPage() {
|
|||||||
{/* <span className='text-xs text-muted-foreground'>{formatTime(orderDetail.data.createdAt)}</span> */}
|
{/* <span className='text-xs text-muted-foreground'>{formatTime(orderDetail.data.createdAt)}</span> */}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex justify-between items-center h-16 border-b border-border">
|
||||||
|
<span className="text-sm text-muted-foreground">زمان آمادهسازی</span>
|
||||||
|
<span className="text-sm font-semibold">
|
||||||
|
{orderDetail.data.prepareTime
|
||||||
|
? `${orderDetail.data.prepareTime.toLocaleString("fa-IR")} دقیقه`
|
||||||
|
: "-"}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
<div className="flex justify-between items-center h-16 border-b border-border">
|
<div className="flex justify-between items-center h-16 border-b border-border">
|
||||||
<span className="text-sm text-muted-foreground">نوع ارسال</span>
|
<span className="text-sm text-muted-foreground">نوع ارسال</span>
|
||||||
<span className="text-sm font-semibold">{orderDetail.data?.deliveryMethod?.description}</span>
|
<span className="text-sm font-semibold">{orderDetail.data?.deliveryMethod?.description}</span>
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ import ToggleButton, { ToggleButtonClassNames } from "@/components/button/Toggle
|
|||||||
import TabContainer, { TabContainerClassNames, TabContainerRenderType } from "@/components/tab/TabContainer";
|
import TabContainer, { TabContainerClassNames, TabContainerRenderType } from "@/components/tab/TabContainer";
|
||||||
import { TabHeader } from "@/components/tab/TabHeader";
|
import { TabHeader } from "@/components/tab/TabHeader";
|
||||||
import { toast, toastLoginRequired } from "@/components/Toast";
|
import { toast, toastLoginRequired } from "@/components/Toast";
|
||||||
import Comment from "@/components/utils/Comment";
|
|
||||||
import RateSelectionBar from "@/components/utils/RateSelectionBar";
|
import RateSelectionBar from "@/components/utils/RateSelectionBar";
|
||||||
|
import ReviewChatThread from "./ReviewChatThread";
|
||||||
|
import { buildReviewThreads } from "../utils/reviewHelpers";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { getToken } from "@/lib/api/func";
|
import { getToken } from "@/lib/api/func";
|
||||||
import { extractErrorMessage } from "@/lib/func";
|
import { extractErrorMessage } from "@/lib/func";
|
||||||
@@ -18,7 +19,6 @@ import { useGetProfile } from "../../../(Profile)/profile/hooks/userProfileData"
|
|||||||
import { NegativePointEnum, PositivePointEnum } from "../enum/ReviewPoints";
|
import { NegativePointEnum, PositivePointEnum } from "../enum/ReviewPoints";
|
||||||
import { useCreateReview, useGetFoodReviews } from "../hooks/useFoodData";
|
import { useCreateReview, useGetFoodReviews } from "../hooks/useFoodData";
|
||||||
import { CreateReviewType } from "../types/ReviewTypes";
|
import { CreateReviewType } from "../types/ReviewTypes";
|
||||||
import { isReviewApproved } from "../../about/types/Types";
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
foodId: string;
|
foodId: string;
|
||||||
@@ -54,8 +54,7 @@ function FoodReviewsSection({ foodId }: Props) {
|
|||||||
const [negativePoints, setNegativePoints] = useState<string[]>([]);
|
const [negativePoints, setNegativePoints] = useState<string[]>([]);
|
||||||
const [formKey, setFormKey] = useState(0);
|
const [formKey, setFormKey] = useState(0);
|
||||||
|
|
||||||
const reviews = (reviewsData?.data || []).filter(isReviewApproved);
|
const reviewThreads = buildReviewThreads(reviewsData?.data || []);
|
||||||
const sortedReviews = [...reviews].sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
||||||
|
|
||||||
const getTranslationKey = (point: string, isPositive: boolean): string => {
|
const getTranslationKey = (point: string, isPositive: boolean): string => {
|
||||||
const basePath = isPositive ? "Tabs.Strengths.Options" : "Tabs.Weeknesses.Options";
|
const basePath = isPositive ? "Tabs.Strengths.Options" : "Tabs.Weeknesses.Options";
|
||||||
@@ -84,15 +83,6 @@ function FoodReviewsSection({ foodId }: Props) {
|
|||||||
setFormKey((prev) => prev + 1);
|
setFormKey((prev) => prev + 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatDate = (dateString: string) => {
|
|
||||||
const date = new Date(dateString);
|
|
||||||
return date.toLocaleDateString("fa-IR", {
|
|
||||||
year: "numeric",
|
|
||||||
month: "long",
|
|
||||||
day: "numeric",
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@@ -217,21 +207,8 @@ function FoodReviewsSection({ foodId }: Props) {
|
|||||||
<div>
|
<div>
|
||||||
{reviewsLoading ? (
|
{reviewsLoading ? (
|
||||||
<p className="text-sm2 text-center py-8 text-disabled-text">در حال بارگذاری نظرات...</p>
|
<p className="text-sm2 text-center py-8 text-disabled-text">در حال بارگذاری نظرات...</p>
|
||||||
) : sortedReviews.length > 0 ? (
|
) : reviewThreads.length > 0 ? (
|
||||||
sortedReviews.map((review) => (
|
reviewThreads.map((thread) => <ReviewChatThread key={thread.root.id} thread={thread} />)
|
||||||
<article key={review.id}>
|
|
||||||
<Comment
|
|
||||||
className="pt-6"
|
|
||||||
user={`${review.user?.firstName || ""} ${review.user?.lastName || ""}`.trim() || "کاربر"}
|
|
||||||
rating={review.rating}
|
|
||||||
date={formatDate(review.createdAt)}
|
|
||||||
text={review.comment}
|
|
||||||
tags={[]}
|
|
||||||
positivePoints={review.positivePoints}
|
|
||||||
negativePoints={review.negativePoints}
|
|
||||||
/>
|
|
||||||
</article>
|
|
||||||
))
|
|
||||||
) : (
|
) : (
|
||||||
<p className="text-sm2 text-center py-8 text-disabled-text">هنوز نظری ثبت نشده است</p>
|
<p className="text-sm2 text-center py-8 text-disabled-text">هنوز نظری ثبت نشده است</p>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Star1 } from "iconsax-react";
|
||||||
|
import { useMemo } from "react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { ReviewThread, buildThreadMessages, formatReviewMessageTime } from "../utils/reviewHelpers";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
thread: ReviewThread;
|
||||||
|
};
|
||||||
|
|
||||||
|
function ReviewChatThread({ thread }: Props) {
|
||||||
|
const { t } = useTranslation("rating");
|
||||||
|
|
||||||
|
const messages = useMemo(() => buildThreadMessages(thread), [thread]);
|
||||||
|
|
||||||
|
const getTranslationKey = (point: string, isPositive: boolean): string => {
|
||||||
|
const basePath = isPositive ? "Tabs.Strengths.Options" : "Tabs.Weeknesses.Options";
|
||||||
|
return `${basePath}.${point}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<article className="py-4 border-b border-border/60 last:border-b-0">
|
||||||
|
<ul className="flex flex-col gap-4">
|
||||||
|
{messages.map((message) => {
|
||||||
|
const isAdmin = message.type === "admin";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<li key={message.id} className={`flex w-full ${isAdmin ? "justify-start" : "justify-end"}`}>
|
||||||
|
<div
|
||||||
|
className={`w-full max-w-[min(420px,85%)] rounded-[24px] px-5 py-4 ${
|
||||||
|
isAdmin ? "bg-[#F6F7FA] dark:bg-gray-700 rounded-tr-none" : "bg-[#EBEDF5] dark:bg-gray-600 rounded-tl-none"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<h6 className="text-xs font-medium text-gray-700 dark:text-gray-200 mb-2">{message.senderName}</h6>
|
||||||
|
|
||||||
|
{message.parentMessage && (
|
||||||
|
<div className="mb-3 rounded-xl border-r-2 border-primary/50 bg-white/70 dark:bg-black/10 px-3 py-2">
|
||||||
|
<p className="text-[11px] font-medium text-gray-600 dark:text-gray-300">{message.parentMessage.senderName}</p>
|
||||||
|
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1 line-clamp-3 whitespace-pre-wrap break-words">
|
||||||
|
{message.parentMessage.content}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!isAdmin && message.rating !== undefined && message.rating > 0 && (
|
||||||
|
<div className="flex items-center gap-1.5 mb-2">
|
||||||
|
<Star1 size={16} color="#FFB800" variant="Bold" />
|
||||||
|
<span className="text-sm font-medium">{message.rating}</span>
|
||||||
|
<span className="text-xs text-gray-500 dark:text-gray-400">از ۵</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<p className="text-sm leading-7 text-gray-800 dark:text-gray-100 whitespace-pre-wrap break-words">{message.content}</p>
|
||||||
|
|
||||||
|
{!isAdmin && message.positivePoints && message.positivePoints.length > 0 && (
|
||||||
|
<div className="flex flex-wrap gap-2 mt-3">
|
||||||
|
{message.positivePoints.map((point, index) => (
|
||||||
|
<span
|
||||||
|
key={index}
|
||||||
|
className="px-3 py-1 border border-green-500 text-green-600 dark:text-green-400 rounded-xl text-xs bg-white dark:bg-transparent"
|
||||||
|
>
|
||||||
|
{t(getTranslationKey(point, true))}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!isAdmin && message.negativePoints && message.negativePoints.length > 0 && (
|
||||||
|
<div className="flex flex-wrap gap-2 mt-2">
|
||||||
|
{message.negativePoints.map((point, index) => (
|
||||||
|
<span
|
||||||
|
key={index}
|
||||||
|
className="px-3 py-1 border border-red-500 text-red-600 dark:text-red-400 rounded-xl text-xs bg-white dark:bg-transparent"
|
||||||
|
>
|
||||||
|
{t(getTranslationKey(point, false))}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{message.createdAt && (
|
||||||
|
<div className="mt-3 flex justify-end">
|
||||||
|
<span dir="ltr" className="text-[11px] text-gray-400 dark:text-gray-500">
|
||||||
|
{formatReviewMessageTime(message.createdAt)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
</article>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ReviewChatThread;
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
import { isReviewApproved, Review } from "../../about/types/Types";
|
||||||
|
|
||||||
|
export type ReviewThread = {
|
||||||
|
root: Review;
|
||||||
|
replies: Review[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export function getParentId(parent: Review["parent"]): string | null {
|
||||||
|
if (!parent) return null;
|
||||||
|
if (typeof parent === "string") return parent;
|
||||||
|
if (typeof parent === "object" && "id" in parent) return parent.id;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isPopulatedReview(value: Review["parent"]): value is Review {
|
||||||
|
return Boolean(value && typeof value === "object" && "comment" in value);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getDisplayName(firstName?: string | null, lastName?: string | null, fallback = "کاربر"): string {
|
||||||
|
return [firstName, lastName].filter(Boolean).join(" ") || fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildReviewThreads(reviews: Review[]): ReviewThread[] {
|
||||||
|
const approved = reviews.filter(isReviewApproved);
|
||||||
|
const topLevel = approved.filter((review) => !getParentId(review.parent));
|
||||||
|
const topLevelById = new Map(topLevel.map((review) => [review.id, review]));
|
||||||
|
|
||||||
|
const repliesByParentId = new Map<string, Review[]>();
|
||||||
|
const orphanReplies: Review[] = [];
|
||||||
|
|
||||||
|
for (const review of approved) {
|
||||||
|
const parentId = getParentId(review.parent);
|
||||||
|
if (!parentId) continue;
|
||||||
|
|
||||||
|
if (topLevelById.has(parentId)) {
|
||||||
|
const list = repliesByParentId.get(parentId) ?? [];
|
||||||
|
list.push(review);
|
||||||
|
repliesByParentId.set(parentId, list);
|
||||||
|
} else {
|
||||||
|
orphanReplies.push(review);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const threads: ReviewThread[] = topLevel.map((root) => ({
|
||||||
|
root,
|
||||||
|
replies: (repliesByParentId.get(root.id) ?? []).sort(
|
||||||
|
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(),
|
||||||
|
),
|
||||||
|
}));
|
||||||
|
|
||||||
|
for (const reply of orphanReplies) {
|
||||||
|
if (isPopulatedReview(reply.parent)) {
|
||||||
|
threads.push({
|
||||||
|
root: reply.parent,
|
||||||
|
replies: [reply],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return threads.sort((a, b) => new Date(b.root.createdAt).getTime() - new Date(a.root.createdAt).getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveParentReview(reply: Review, threadRoot: Review): Review | null {
|
||||||
|
if (isPopulatedReview(reply.parent)) {
|
||||||
|
return reply.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parentId = getParentId(reply.parent);
|
||||||
|
if (parentId && parentId === threadRoot.id) {
|
||||||
|
return threadRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function reviewToUserMessage(review: Review): ChatMessage {
|
||||||
|
return {
|
||||||
|
id: review.id,
|
||||||
|
type: "user",
|
||||||
|
senderName: getDisplayName(review.user?.firstName, review.user?.lastName),
|
||||||
|
content: review.comment || "نظری ثبت نشده است",
|
||||||
|
createdAt: review.createdAt,
|
||||||
|
rating: review.rating,
|
||||||
|
positivePoints: review.positivePoints,
|
||||||
|
negativePoints: review.negativePoints,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ChatMessage = {
|
||||||
|
id: string;
|
||||||
|
type: "user" | "admin";
|
||||||
|
senderName: string;
|
||||||
|
content: string;
|
||||||
|
createdAt?: string;
|
||||||
|
rating?: number;
|
||||||
|
positivePoints?: string[];
|
||||||
|
negativePoints?: string[];
|
||||||
|
parentMessage?: {
|
||||||
|
senderName: string;
|
||||||
|
content: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export function buildThreadMessages(thread: ReviewThread): ChatMessage[] {
|
||||||
|
const rootMessage = reviewToUserMessage(thread.root);
|
||||||
|
const items: ChatMessage[] = [rootMessage];
|
||||||
|
|
||||||
|
thread.replies.forEach((reply) => {
|
||||||
|
const parentReview = resolveParentReview(reply, thread.root);
|
||||||
|
const parentMessage = parentReview
|
||||||
|
? {
|
||||||
|
senderName: getDisplayName(parentReview.user?.firstName, parentReview.user?.lastName),
|
||||||
|
content: parentReview.comment || "-",
|
||||||
|
}
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
items.push({
|
||||||
|
id: reply.id,
|
||||||
|
type: "admin",
|
||||||
|
senderName: getDisplayName(reply.admin?.firstName, reply.admin?.lastName, "رستوران"),
|
||||||
|
content: reply.comment || "-",
|
||||||
|
createdAt: reply.createdAt,
|
||||||
|
parentMessage,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatReviewMessageTime(date?: string) {
|
||||||
|
if (!date) return "";
|
||||||
|
|
||||||
|
return new Date(date).toLocaleString("fa-IR", {
|
||||||
|
year: "numeric",
|
||||||
|
month: "2-digit",
|
||||||
|
day: "2-digit",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -107,6 +107,12 @@ export interface ReviewUser {
|
|||||||
|
|
||||||
export type ReviewStatus = "approved" | "pending" | "rejected";
|
export type ReviewStatus = "approved" | "pending" | "rejected";
|
||||||
|
|
||||||
|
export interface ReviewAdmin {
|
||||||
|
id: string;
|
||||||
|
firstName?: string | null;
|
||||||
|
lastName?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Review {
|
export interface Review {
|
||||||
id: string;
|
id: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
@@ -115,12 +121,14 @@ export interface Review {
|
|||||||
isBuyer?: boolean;
|
isBuyer?: boolean;
|
||||||
order?: string;
|
order?: string;
|
||||||
food: ReviewFood;
|
food: ReviewFood;
|
||||||
user: ReviewUser;
|
user?: ReviewUser | null;
|
||||||
|
admin?: ReviewAdmin | null;
|
||||||
comment: string;
|
comment: string;
|
||||||
rating: number;
|
rating: number;
|
||||||
positivePoints: string[];
|
positivePoints: string[];
|
||||||
negativePoints: string[];
|
negativePoints: string[];
|
||||||
status?: ReviewStatus;
|
status?: ReviewStatus;
|
||||||
|
parent?: string | { id: string } | Review | null;
|
||||||
/** @deprecated use status */
|
/** @deprecated use status */
|
||||||
isApproved?: boolean;
|
isApproved?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user