summaryrefslogtreecommitdiff
path: root/src/components/Flashcard/BookmarkButton.tsx
blob: 5128b91070147005a118d435d9ab0c3f117e42c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use client";

import { toggleBookmark } from "@/actions/deck";
import { CardResponse } from "@/lib/types/cards";
import { BookMarkedIcon, BookmarkIcon } from "lucide-react";
import { useEffect, useState, useTransition } from "react";

export const BookmarkIconito: React.FC<{ card: CardResponse }> = ({ card }) => {
  const [notes, setNotes] = useState();
  const [isBookmarked, setBookmarked] = useState(false);
  useEffect(() => {
    setBookmarked(card.expression.isBookmarked);
  }, [card]);

  const [isPending, startTransition] = useTransition();
  const toggle = (e: React.MouseEvent) => {
    console.log("toggling on fe");
    e.stopPropagation();
    startTransition(async () => {
      const res = await toggleBookmark(
        2,
        card.expression.id,
        isBookmarked,
        notes,
      );
      if ("ok" in res) setBookmarked(true);
    });
  };

  return isBookmarked ? (
    <BookMarkedIcon
      onClick={toggle}
      className="absolute top-5 right-3 hover:bg-red"
    />
  ) : (
    <BookmarkIcon
      onClick={toggle}
      className="absolute top-5 right-3 hover:bg-red"
    />
  );
};