mirror of
https://github.com/Ategon/Jamjar.git
synced 2025-02-12 06:16:21 +00:00
Add likes
This commit is contained in:
parent
a3870224fa
commit
54e5cb6495
4 changed files with 62 additions and 5 deletions
src
|
@ -1,6 +1,6 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { Button, Form, Input } from "@nextui-org/react";
|
import { Button, Form, Input, Link } from "@nextui-org/react";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { toast } from "react-toastify";
|
import { toast } from "react-toastify";
|
||||||
|
@ -105,6 +105,9 @@ export default function UserPage() {
|
||||||
Reset
|
Reset
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
<p>
|
||||||
|
Don't have an account? <Link href="/signup">Sign up</Link>
|
||||||
|
</p>
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { Button, Form, Input } from "@nextui-org/react";
|
import { Button, Form, Input, Link } from "@nextui-org/react";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
export default function UserPage() {
|
export default function UserPage() {
|
||||||
|
@ -129,6 +129,9 @@ export default function UserPage() {
|
||||||
Reset
|
Reset
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
<p>
|
||||||
|
Already have an account? <Link href="/login">Log In</Link>
|
||||||
|
</p>
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
52
src/components/posts/LikeButton.tsx
Normal file
52
src/components/posts/LikeButton.tsx
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Button } from "@nextui-org/react";
|
||||||
|
import { PostType } from "@/types/PostType";
|
||||||
|
import { Heart } from "lucide-react";
|
||||||
|
import { toast } from "react-toastify";
|
||||||
|
import { getCookie } from "@/helpers/cookie";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
|
export default function LikeButton({ post }: { post: PostType }) {
|
||||||
|
const [likes, setLikes] = useState(post.likes.length);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="bordered"
|
||||||
|
onPress={async () => {
|
||||||
|
const response = await fetch(
|
||||||
|
process.env.NEXT_PUBLIC_MODE === "PROD"
|
||||||
|
? "https://d2jam.com/api/v1/like"
|
||||||
|
: "http://localhost:3005/api/v1/like",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
authorization: `Bearer ${getCookie("token")}`,
|
||||||
|
},
|
||||||
|
credentials: "include",
|
||||||
|
body: JSON.stringify({
|
||||||
|
username: getCookie("user"),
|
||||||
|
postId: post.id,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
if (response.status == 401) {
|
||||||
|
redirect("/login");
|
||||||
|
} else {
|
||||||
|
toast.error("An error occured");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setLikes(await response.text());
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Heart size={16} /> {likes}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ import { formatDistance } from "date-fns";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { PostType } from "@/types/PostType";
|
import { PostType } from "@/types/PostType";
|
||||||
import { Heart, MessageCircle } from "lucide-react";
|
import { Heart, MessageCircle } from "lucide-react";
|
||||||
|
import LikeButton from "./LikeButton";
|
||||||
|
|
||||||
export default function PostCard({ post }: { post: PostType }) {
|
export default function PostCard({ post }: { post: PostType }) {
|
||||||
return (
|
return (
|
||||||
|
@ -37,9 +38,7 @@ export default function PostCard({ post }: { post: PostType }) {
|
||||||
<Spacer y={4} />
|
<Spacer y={4} />
|
||||||
|
|
||||||
<div className="flex gap-3">
|
<div className="flex gap-3">
|
||||||
<Button size="sm" variant="bordered">
|
<LikeButton post={post} />
|
||||||
<Heart size={16} /> {post.likers.length}
|
|
||||||
</Button>
|
|
||||||
<Button size="sm" variant="bordered">
|
<Button size="sm" variant="bordered">
|
||||||
<MessageCircle size={16} /> {0}
|
<MessageCircle size={16} /> {0}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
Loading…
Reference in a new issue