ํ”„๋กœ์ ํŠธ/๐Ÿ ๊ธˆ์œต ์ปค๋ฎค๋‹ˆํ‹ฐ

[๊ฐœ๋ฐœ] ๊ธˆ์œต ์ปค๋ฎค๋‹ˆํ‹ฐ - ๋ฆฌ์น˜์—์ด๋ธ” ๊ฐœ๋ฐœ์ผ์ง€ #4(์ข‹์•„์š”)

TwoIceFish 2024. 7. 4. 18:07

 

 

ํ›„๊ธฐ

์ข‹์•„์š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

UI์ƒ์˜ ํ‘œ์‹œ์™€ ๋ฐฑ์—”๋“œ์˜ ์ข‹์•„์š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ๊ฒƒ์€ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ตฌํ˜„์ด ๋˜์–ด์žˆ๋‹ค. ํ•จ๊ป˜ ์„œ๋ฒ„ ์•ก์…˜๊ณผ ์—ฐ๋™ํ•˜๋‹ˆ ์‹œ๊ฐ„๋„ ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฆ‰๊ฐ์ ์ธ ๋ฐ˜์‘์„ ์ฃผ๊ธฐ๋กœ ํ–ˆ๋‹ค.

ํ‘œํ˜„ํ•  Thread ๊ฐ์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์ข‹์•„์š” ํ˜„ํ™ฉ์„ ๋ Œ๋”๋งํ•œ๋‹ค.

 

import React, { useEffect, useState } from "react";
import { HeartIcon, MessageCircle, RepeatIcon, SendIcon } from "lucide-react";
import { threadLike } from "@/actions/thread/ThreadLike";
import { ThreadType } from "@/type/ThreadType";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";

interface ThreadFooterProps {
  thread: ThreadType;
}

const ThreadFooter = ({ thread }: ThreadFooterProps) => {
  const [likeNumber, setLikeNumber] = useState(thread?.likes | 0);
  const [iLike, setILike] = useState(thread?.iLike);

  const onClick = () => {
    const action = async () => {
      await threadLike(thread?.id);
    };
    action().then();

    if (iLike) {
      toast.error("์ข‹์•„์š” ์ทจ์†Œ");
      setILike(false);
      setLikeNumber((thread.likes -= 1));
    }

    if (!iLike) {
      toast.success("์ข‹์•„์š”");
      setILike(true);
      setLikeNumber((thread.likes += 1));
    }
  };

  useEffect(() => {
    if (thread) {
      setILike(thread?.iLike);
      setLikeNumber(thread?.likes);
    }
  }, [thread]);

  return (
    <div className={"flex select-none items-center"}>
      <Button
        variant={"ghost"}
        onClick={onClick}
        className={"flex items-center gap-2"}
      >
        <HeartIcon fill={iLike ? "red" : "none"} />
        <div>{likeNumber}</div>
      </Button>
      <Button variant={"ghost"} className={"flex items-center gap-2"}>
        <MessageCircle />
        <div>00</div>
      </Button>
      <Button variant={"ghost"} className={"flex items-center gap-2"}>
        <RepeatIcon />
      </Button>
      <Button variant={"ghost"} className={"flex items-center gap-2"}>
        <SendIcon />
      </Button>
    </div>
  );
};

export default ThreadFooter;

 

threadLike ํ•จ์ˆ˜์˜ ์ž‘๋™ ๋กœ์ง์ด๋‹ค ํ•˜๋‚˜์•ˆ์— ๊ธฐ์กด ๋‚ด๊ฐ€ ์ข‹์•„์š”๋ฅผ ํ•œ ์ƒํƒœ์ธ์ง€ ์ฒดํฌ๋ฅผ ํ•œ๋‹ค.

"use server";

import db from "@/lib/db";
import { currentUser } from "@/lib/auth";

export const threadLike = async (threadId: string) => {
  const user = await currentUser();
  // ์ด๋ฏธ ์ข‹์•„์š”๋ฅผ ๋ˆŒ๋ €๋Š”์ง€ ํ™•์ธ
  const existingLike = await db.threadLike.findFirst({
    where: {
      threadId: threadId,
      userId: user?.id,
    },
  });

  // ์ด๋ฏธ ์ข‹์•„์š”๋ฅผ ๋ˆŒ๋ €๋‹ค๋ฉด, ์ข‹์•„์š” ์‚ญ์ œ ๋ฐ ์ทจ์†Œ
  if (existingLike != null) {
    await db.threadLike.delete({
      where: {
        id: existingLike.id,
      },
    });
    await db.thread.update({
      where: {
        id: threadId,
      },
      data: {
        likes: {
          decrement: 1,
        },
      },
    });

    return { dislike: "์ข‹์•„์š” ์ทจ์†Œ" };
  }

  // ์ข‹์•„์š” ์ƒ์„ฑ ๋ฐ ์ฆ๊ฐ€
  if (existingLike == null) {
    await db.threadLike.create({
      data: {
        threadId: threadId,
        userId: user?.id as string,
      },
    });

    // ํ•ด๋‹น ์Šค๋ ˆ๋“œ์˜ ์ข‹์•„์š” ์ˆ˜๋ฅผ ์ฆ๊ฐ€
    await db.thread.update({
      where: {
        id: threadId,
      },
      data: {
        likes: {
          increment: 1,
        },
      },
    });

    const activity = await db.activity.create({
      data: {
        title: "์ข‹์•„์š”",
        content: "์ข‹์•„์š”๋ฅผ ๋ˆŒ๋ €์Šต๋‹ˆ๋‹ค.",
      },
    });

    await db.activityUser.create({
      data: {
        userId: user?.id as string,
        activityId: activity.id,
      },
    });

    return { like: "์ข‹์•„์š”" };
  }
  return { error: "์„œ๋ฒ„ ์—๋Ÿฌ" };
};

 

์‚ฌ์ดํŠธ

https://richable.cyber-luna.com

 

๋ฆฌ์น˜์—์ด๋ธ” - ๋กœ๊ทธ์ธ

๋” ๋†’์€ ์„ฑ์žฅ์„ ์œ„ํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ

richable.cyber-luna.com

์†Œ์Šค์ฝ”๋“œ

https://github.com/TwoIceFIsh/richable-web-server

 

GitHub - TwoIceFIsh/richable-web-server: richable-web-server

richable-web-server. Contribute to TwoIceFIsh/richable-web-server development by creating an account on GitHub.

github.com