import {Invite, InviteWithUsers} from "@/interfaces/invite"; import {User} from "@/interfaces/user"; import {getUserName} from "@/utils/users"; import axios from "axios"; import {useMemo, useState} from "react"; import {BsArrowRepeat} from "react-icons/bs"; import {toast} from "react-toastify"; interface Props { invite: InviteWithUsers; reload: () => void; } export default function InviteWithUserCard({invite, reload}: Props) { const [isLoading, setIsLoading] = useState(false); const name = useMemo(() => (!invite.from ? null : getUserName(invite.from)), [invite.from]); const decide = (decision: "accept" | "decline") => { if (!confirm(`Are you sure you want to ${decision} this invite?`)) return; setIsLoading(true); axios .get(`/api/invites/${decision}/${invite.id}`) .then(() => { toast.success(`Successfully ${decision === "accept" ? "accepted" : "declined"} the invite!`, {toastId: "success"}); reload(); }) .catch((e) => { toast.success(`Something went wrong, please try again later!`, { toastId: "error", }); reload(); }) .finally(() => setIsLoading(false)); }; return (