change status review
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { type FC } from 'react'
|
||||
import { useGetReviewById } from './hooks/useReviewData'
|
||||
import { type FC, useState } from 'react'
|
||||
import { useGetReviewById, useUpdateReviewStatus } from './hooks/useReviewData'
|
||||
import { useParams } from 'react-router-dom'
|
||||
import PageLoading from '@/components/PageLoading'
|
||||
import Input from '@/components/Input'
|
||||
import Status from '@/components/Status'
|
||||
import Select from '@/components/Select'
|
||||
import Button from '@/components/Button'
|
||||
import { Star1, User, ShoppingCart, DocumentText, Like1, Dislike } from 'iconsax-react'
|
||||
import { formatOptionalDate, formatPrice, formatFaNumber } from '@/helpers/func'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
@@ -13,6 +15,8 @@ const DetailReview: FC = () => {
|
||||
const { t } = useTranslation('global')
|
||||
const { id } = useParams()
|
||||
const { data: reviewData, isLoading } = useGetReviewById(id!)
|
||||
const updateStatusMutation = useUpdateReviewStatus()
|
||||
const [selectedStatus, setSelectedStatus] = useState<string>('')
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
@@ -49,16 +53,55 @@ const DetailReview: FC = () => {
|
||||
'rejected': 'رد شده',
|
||||
}
|
||||
|
||||
const statusOptions = [
|
||||
{ value: 'pending', label: 'در انتظار' },
|
||||
{ value: 'approved', label: 'تایید شده' },
|
||||
{ value: 'rejected', label: 'رد شده' },
|
||||
]
|
||||
|
||||
const handleStatusChange = () => {
|
||||
if (selectedStatus && selectedStatus !== review.status && id) {
|
||||
updateStatusMutation.mutate(
|
||||
{ id, status: selectedStatus },
|
||||
{
|
||||
onSuccess: () => {
|
||||
setSelectedStatus('')
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const userName = [review.user.firstName, review.user.lastName].filter(Boolean).join(' ') || '-'
|
||||
|
||||
return (
|
||||
<div className='mt-5'>
|
||||
<div className='flex justify-between items-center mb-8'>
|
||||
<h1 className='text-lg font-light'>جزئیات نظر</h1>
|
||||
<Status
|
||||
variant={getStatusVariant(review.status)}
|
||||
label={statusLabels[review.status] || review.status}
|
||||
/>
|
||||
<div className='flex items-center gap-4'>
|
||||
<Status
|
||||
variant={getStatusVariant(review.status)}
|
||||
label={statusLabels[review.status] || review.status}
|
||||
/>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div>
|
||||
<Select
|
||||
items={statusOptions}
|
||||
value={selectedStatus || review.status}
|
||||
onChange={(e) => setSelectedStatus(e.target.value)}
|
||||
className='w-40'
|
||||
/>
|
||||
</div>
|
||||
{selectedStatus && selectedStatus !== review.status && (
|
||||
<Button
|
||||
label='ذخیره'
|
||||
onClick={handleStatusChange}
|
||||
isloading={updateStatusMutation.isPending}
|
||||
className='w-24'
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='flex gap-7'>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as api from "../service/ReviewService";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
export const useGetReviews = (params?: Record<string, string | number>) => {
|
||||
return useQuery({
|
||||
@@ -14,3 +14,15 @@ export const useGetReviewById = (id: string) => {
|
||||
queryFn: () => api.getReviewById(id),
|
||||
});
|
||||
};
|
||||
|
||||
export const useUpdateReviewStatus = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, status }: { id: string; status: string }) =>
|
||||
api.updateReviewStatus(id, status),
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: ["reviews"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["review", variables.id] });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -18,3 +18,8 @@ export const getReviewById = async (
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const updateReviewStatus = async (id: string, status: string) => {
|
||||
const { data } = await axios.patch(`/admin/reviews/${id}/status`, { status });
|
||||
return data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user