-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b873d0
commit ecb31bb
Showing
12 changed files
with
261 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use server"; | ||
|
||
import { apiConfig } from "@/lib/apiConfig"; | ||
import { apiHandler } from "@/lib/apiHandler"; | ||
import { getErrorMessage } from "@/utils/helpers"; | ||
import { Tags } from "@/utils/tags"; | ||
import { revalidateTag } from "next/cache"; | ||
|
||
export async function deleteRating(ratingId: string) { | ||
try { | ||
const endpoint = apiConfig.ratings.get(ratingId); | ||
await apiHandler({ | ||
endpoint, | ||
method: "DELETE", | ||
}); | ||
revalidateTag(Tags.reviews); | ||
} catch (error) { | ||
console.log(error); | ||
return { error: getErrorMessage(error) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/app/(dashboard)/places/[placeId]/_sections/placeContent/PlaceReviews.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { getPlaceRatings } from "@/actions/place"; | ||
import WithServerError from "@/components/hoc/WithServerError"; | ||
import EmptyContent from "@/components/shared/EmptyContent"; | ||
import ReviewCard from "@/components/shared/cards/ReviewCard"; | ||
import HStack from "@/components/shared/layout/HStack"; | ||
import { Place } from "@/types/place"; | ||
import { Pagination, Skeleton } from "@nextui-org/react"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import React, { useState } from "react"; | ||
|
||
interface Props { | ||
place: Place; | ||
} | ||
export default function PlaceReviews({ place }: Props) { | ||
const [page, setPage] = useState(1); | ||
|
||
const { data, error, isPending } = useQuery({ | ||
queryKey: ["placeReviews", place?.id, page], | ||
queryFn: () => getPlaceRatings(place?.id, { page }), | ||
}); | ||
|
||
if (isPending) { | ||
return ( | ||
<div className='grid grid-cols-2 xl:grid-cols-3 gap-3'> | ||
{Array.from("loadin").map((l) => ( | ||
<Skeleton className='rounded-lg mb-3' key={l}> | ||
<div className='h-40 rounded-lg bg-default-300'></div> | ||
</Skeleton> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
if (data?.results?.items?.length === 0 && !isPending) { | ||
return <EmptyContent title='No reviews yet' />; | ||
} | ||
|
||
const totalPages = data?.results?.meta?.totalPages || 1; | ||
|
||
return ( | ||
<WithServerError error={error?.message}> | ||
<div className='grid grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 gap-4'> | ||
{data?.results?.items?.map((rating) => ( | ||
<ReviewCard key={rating?.id} rating={rating} /> | ||
))} | ||
</div> | ||
{totalPages > 1 && ( | ||
<HStack className='justify-center mt-3'> | ||
<Pagination | ||
total={totalPages} | ||
initialPage={1} | ||
variant='bordered' | ||
showControls | ||
onChange={(page) => setPage(page)} | ||
/> | ||
</HStack> | ||
)} | ||
</WithServerError> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
"use client"; | ||
import React from "react"; | ||
import { | ||
Card, | ||
CardHeader, | ||
CardBody, | ||
CardFooter, | ||
Avatar, | ||
Button, | ||
useDisclosure, | ||
} from "@nextui-org/react"; | ||
import { Ratings } from "@/types/ratings"; | ||
import { getErrorMessage, getInitials, pluralize } from "@/utils/helpers"; | ||
import Modal from "../modal"; | ||
import { useServerAction } from "@/hooks/useServerAction"; | ||
import { deleteRating } from "@/actions/ratings"; | ||
import { toast } from "sonner"; | ||
import { useQueryClient } from "@tanstack/react-query"; | ||
|
||
interface Props { | ||
rating: Ratings; | ||
} | ||
export default function ReviewCard({ rating }: Props) { | ||
const { isOpen, onOpen, onClose } = useDisclosure(); | ||
|
||
const [runDeleteReview, { loading }] = useServerAction< | ||
any, | ||
typeof deleteRating | ||
>(deleteRating); | ||
|
||
const queryClient = useQueryClient(); | ||
|
||
const onConfirmDelete = async () => { | ||
try { | ||
const response = await runDeleteReview(rating?.id); | ||
if (response?.error) { | ||
toast.error(response.error); | ||
return; | ||
} | ||
toast.success("Review deleted"); | ||
queryClient.invalidateQueries({ | ||
queryKey: ["getReviews", rating?.booking?.place?.id], | ||
}); | ||
onClose(); | ||
} catch (error) { | ||
toast.error(getErrorMessage(error)); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Card className='max-w-[340px] h-fit' shadow='sm'> | ||
<CardHeader className='justify-between'> | ||
<div className='flex gap-3'> | ||
<Avatar | ||
isBordered | ||
radius='full' | ||
size='sm' | ||
fallback={getInitials(rating?.user?.fullName)} | ||
/> | ||
<div className='flex flex-col gap-1 items-start justify-center'> | ||
<h4 className='text-small font-semibold line-clamp-1 leading-none text-default-600'> | ||
{rating?.user?.fullName} | ||
</h4> | ||
</div> | ||
</div> | ||
<Button | ||
onClick={onOpen} | ||
disableRipple | ||
color='primary' | ||
radius='full' | ||
size='sm' | ||
> | ||
Delete | ||
</Button> | ||
</CardHeader> | ||
<CardBody className='px-3 py-0 text-small text-default-400'> | ||
<p>{rating?.comment}</p> | ||
</CardBody> | ||
<CardFooter className='gap-3 justify-between'> | ||
<div className='flex gap-1'> | ||
<p className='font-semibold text-default-400 text-small'>Date:</p> | ||
<p className='text-default-400 text-small'> | ||
{new Date(rating?.createdAt).toLocaleDateString()} | ||
</p> | ||
</div> | ||
<div className='flex gap-1'> | ||
<p className=' text-default-400 text-small'> | ||
{rating?.rating} {pluralize("star", rating?.rating)} | ||
</p> | ||
</div> | ||
</CardFooter> | ||
</Card> | ||
<Modal | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
title='Delete review' | ||
description='Are you sure you want to delete this review?' | ||
content={ | ||
<div className='flex flex-col gap-3'> | ||
<div className='flex gap-3'> | ||
<Button | ||
disableRipple | ||
color='secondary' | ||
onClick={onClose} | ||
isDisabled={loading} | ||
> | ||
Cancel | ||
</Button> | ||
<Button | ||
color='primary' | ||
onClick={onConfirmDelete} | ||
isLoading={loading} | ||
> | ||
Continue | ||
</Button> | ||
</div> | ||
</div> | ||
} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { User } from "./auth"; | ||
import { Booking } from "./booking"; | ||
|
||
export interface Ratings { | ||
id: string; | ||
createdAt: string; | ||
updatedAt: string; | ||
deletedAt: string | null; | ||
rating: number; | ||
comment: string; | ||
date: string; | ||
user: User; | ||
booking: Booking; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters