mirror of
https://github.com/trafficlunar/tomodachi-share.git
synced 2026-06-28 14:44:15 +00:00
feat: working like button
This commit is contained in:
parent
bc7460bd71
commit
e458d4459f
4 changed files with 105 additions and 13 deletions
|
|
@ -6,26 +6,32 @@ import { Icon } from "@iconify/react";
|
|||
|
||||
interface Props {
|
||||
likes: number;
|
||||
miiId: number | undefined;
|
||||
isLiked: boolean;
|
||||
isLoggedIn: boolean;
|
||||
big?: boolean;
|
||||
}
|
||||
|
||||
export default function LikeButton({ likes, isLoggedIn, big }: Props) {
|
||||
const [isLiked, setIsLiked] = useState(false);
|
||||
export default function LikeButton({ likes, isLiked, miiId, isLoggedIn, big }: Props) {
|
||||
const [isLikedState, setIsLikedState] = useState(isLiked);
|
||||
const [likesState, setLikesState] = useState(likes);
|
||||
|
||||
const onClick = () => {
|
||||
const onClick = async () => {
|
||||
if (!isLoggedIn) redirect("/login");
|
||||
|
||||
setIsLiked((prev) => !prev);
|
||||
setIsLikedState((prev) => !prev);
|
||||
setLikesState((prev) => (isLiked ? prev - 1 : prev + 1));
|
||||
|
||||
// todo: update database
|
||||
const response = await fetch("/api/like", { method: "PATCH", body: JSON.stringify({ miiId }) });
|
||||
const { liked, count } = await response.json();
|
||||
|
||||
setIsLikedState(liked);
|
||||
setLikesState(count);
|
||||
};
|
||||
|
||||
return (
|
||||
<button onClick={onClick} className={`flex items-center gap-2 text-red-400 cursor-pointer ${big ? "text-3xl" : "text-xl"}`}>
|
||||
<Icon icon={isLiked ? "icon-park-solid:like" : "icon-park-outline:like"} />
|
||||
<Icon icon={isLikedState ? "icon-park-solid:like" : "icon-park-outline:like"} />
|
||||
<span>{likesState}</span>
|
||||
</button>
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue