-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(reservation): api and hook (#68)
- Loading branch information
1 parent
c7bf848
commit a1ed1b9
Showing
7 changed files
with
276 additions
and
177 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,40 +1,73 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
|
||
import { MinusCircle } from "lucide-react"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { Card } from "@/components/ui/card"; | ||
|
||
import ResStateDialog from "./resstate-dialog"; | ||
|
||
type ReservationCard = { | ||
productId: number; | ||
id: number; | ||
name: string; | ||
note: string; | ||
price: number; | ||
unit: number; | ||
time: string; | ||
quantity: number; | ||
status: string; | ||
}; | ||
|
||
export function ReservationCard({ | ||
id, | ||
name, | ||
note, | ||
price, | ||
unit, | ||
time, | ||
quantity, | ||
status, | ||
}: ReservationCard) { | ||
const [finishDialogOpen, setFinishDialogOpen] = useState(false); | ||
const [cancelDialogOpen, setCancelDialogOpen] = useState(false); | ||
|
||
return ( | ||
<Card className="w-full cursor-pointer p-4"> | ||
<div className="flex flex-col gap-x-4"> | ||
<div className="flex flex-row items-center justify-between"> | ||
<Card | ||
className="w-full cursor-pointer p-4" | ||
onClick={() => setFinishDialogOpen(!finishDialogOpen)} | ||
> | ||
<div className="flex flex-row justify-between"> | ||
<div className="flex flex-col gap-x-4"> | ||
<div className="flex items-center gap-x-2 lg:gap-x-8"> | ||
<p className="text-lg font-bold lg:text-2xl">{name}</p> | ||
<p className="text-md lg:text-lg">$ {price}</p> | ||
</div> | ||
<p className="text-md font-semibold lg:text-lg">{unit} 個</p> | ||
</div> | ||
<div className="text-md pt-2 font-normal text-muted-foreground lg:pt-4 lg:text-lg"> | ||
{note.slice(0, 20)}... | ||
<div className="text-md pt-2 font-normal text-muted-foreground lg:pt-4 lg:text-lg"> | ||
{status}... | ||
</div> | ||
</div> | ||
<div className="text-md pt-2 text-end font-normal lg:pt-4 lg:text-lg"> | ||
{time} | ||
<div className="flex flex-row items-center gap-x-4"> | ||
<p className="text-md font-semibold lg:text-lg">{quantity} 個</p> | ||
<Button | ||
variant="ghost" | ||
size="icon" | ||
className="flex flex-col items-center justify-center" | ||
onClick={() => setCancelDialogOpen(!cancelDialogOpen)} | ||
> | ||
<MinusCircle /> | ||
</Button> | ||
</div> | ||
</div> | ||
<ResStateDialog | ||
open={finishDialogOpen} | ||
onOpenChange={setFinishDialogOpen} | ||
type="finish" | ||
id={id} | ||
quantity={quantity} | ||
/> | ||
<ResStateDialog | ||
open={cancelDialogOpen} | ||
onOpenChange={setCancelDialogOpen} | ||
type="cancel" | ||
id={id} | ||
quantity={quantity} | ||
/> | ||
</Card> | ||
); | ||
} |
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,51 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { useState } from "react"; | ||
|
||
import { PlusCircle, MinusCircle } from "lucide-react"; | ||
|
||
import usePost from "@/hooks/use-post"; | ||
|
||
type ReservationUpdate = { | ||
id: number; | ||
postId: number; | ||
name: string; | ||
quantity: number; | ||
description: string; | ||
image: string; | ||
}; | ||
|
||
export function ReservationUpdate({ | ||
id, | ||
postId, | ||
name, | ||
quantity, | ||
description, | ||
image, | ||
}: ReservationUpdate) { | ||
const { updatePost } = usePost(); | ||
const [dishQuantity, setDishQuantity] = useState(quantity); | ||
|
||
const handleDecreaseQuantity = () => { | ||
if (dishQuantity !== undefined && dishQuantity > 0) { | ||
updatePost(id, postId, name, dishQuantity - 1, description, image); | ||
setDishQuantity(dishQuantity - 1); | ||
} | ||
}; | ||
|
||
const handleIncreaseQuantity = () => { | ||
if (dishQuantity !== undefined) { | ||
updatePost(id, postId, name, dishQuantity + 1, description, image); | ||
setDishQuantity(dishQuantity + 1); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex flex-row items-center gap-x-2"> | ||
<MinusCircle onClick={handleDecreaseQuantity} /> | ||
<p className="text-xs lg:text-lg">{dishQuantity}</p> | ||
<PlusCircle onClick={handleIncreaseQuantity} /> | ||
</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
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,66 @@ | ||
import { useState } from "react"; | ||
|
||
import { useRouter } from "next/navigation"; | ||
|
||
import { toast } from "sonner"; | ||
|
||
import handleFetch from "./utils"; | ||
|
||
export default function useReservation() { | ||
const [loading, setLoading] = useState(false); | ||
const router = useRouter(); | ||
|
||
const finishRservation = async (id: number, quantity: number) => { | ||
setLoading(true); | ||
|
||
try { | ||
await handleFetch({ | ||
data: { | ||
id, | ||
quantity, | ||
status: "finished", | ||
}, | ||
method: "PUT", | ||
url: "/api/posts/post-reservations", | ||
}); | ||
|
||
setLoading(false); | ||
router.refresh(); | ||
} catch (error) { | ||
console.error("Error updating reservation:", error); | ||
} | ||
|
||
toast("Reservation has finished."); | ||
router.refresh(); | ||
setLoading(false); | ||
}; | ||
|
||
const cancelRservation = async (id: number) => { | ||
setLoading(true); | ||
|
||
try { | ||
await handleFetch({ | ||
data: { | ||
id, | ||
}, | ||
method: "DELETE", | ||
url: "/api/posts/post-reservations", | ||
}); | ||
|
||
setLoading(false); | ||
router.refresh(); | ||
} catch (error) { | ||
console.error("Error delete reservation:", error); | ||
} | ||
|
||
toast("Reservation has been deleted."); | ||
router.refresh(); | ||
setLoading(false); | ||
}; | ||
|
||
return { | ||
finishRservation, | ||
cancelRservation, | ||
loading, | ||
}; | ||
} |