"use client"; import { Button, Modal, Label, TextInput } from "flowbite-react"; import { Spinner } from "../Spinner/Spinner"; import { ENDPOINTS } from "#/api/config"; import { useEffect, useState } from "react"; import { useSWRConfig } from "swr"; export const ProfileEditSocialModal = (props: { isOpen: boolean; setIsOpen: (isOpen: boolean) => void; token: string; profile_id: number; }) => { const [loading, setLoading] = useState(false); const [updating, setUpdating] = useState(false); const [socials, setSocials] = useState({ vkPage: "", tgPage: "", discordPage: "", instPage: "", ttPage: "", }); const { mutate } = useSWRConfig(); function _addUrl(username: string, social: string) { if (!username) { return ""; } if (username.startsWith("h")) { return username; } switch (social) { case "vk": return `https://vk.com/${username}`; case "tg": return `https://t.me/${username}`; case "inst": return `https://instagram.com/${username}`; case "tt": return `https://tiktok.com/@${username}`; } } function _removeUrl(link: string) { if (link.startsWith("https://")) { const split = link.split("/"); return split[split.length - 1]; } else { return link; } } useEffect(() => { setLoading(true); fetch(`${ENDPOINTS.user.settings.socials.info}?token=${props.token}`) .then((res) => { if (res.ok) { return res.json(); } }) .then((data) => { setSocials({ vkPage: data.vk_page, tgPage: data.tg_page, discordPage: data.discord_page, instPage: data.inst_page, ttPage: data.tt_page, }); setLoading(false); }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [props.isOpen]); function handleInput(e: any) { const social = { ...socials, [e.target.name]: e.target.value } setSocials(social); } function _setSocialSetting() { const data = { vkPage: _removeUrl(socials.vkPage), tgPage: _removeUrl(socials.tgPage), discordPage: _removeUrl(socials.discordPage), instPage: _removeUrl(socials.instPage), ttPage: _removeUrl(socials.ttPage), }; setUpdating(true); fetch(`${ENDPOINTS.user.settings.socials.edit}?token=${props.token}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }) .then((res) => { if (res.ok) { mutate( `${ENDPOINTS.user.profile}/${props.profile_id}?token=${props.token}` ); setUpdating(false); props.setIsOpen(false); } else { new Error("failed to send data"); } }) .catch((err) => { console.log(err); setUpdating(false); }); } return ( props.setIsOpen(false)} size={"4xl"} > Соц. сети

Укажите ссылки на свои социальные сети, чтобы другие пользователи могли с вами связаться

{loading ? (
) : (
handleInput(e)} value={_addUrl(socials.vkPage, "vk")} placeholder="Ссылка или никнейм" />
handleInput(e)} value={_addUrl(socials.tgPage, "tg")} placeholder="Ссылка или никнейм" />
handleInput(e)} value={socials.discordPage} placeholder="Никнейм" />
handleInput(e)} value={_addUrl(socials.instPage, "inst")} placeholder="Ссылка или никнейм" />
handleInput(e)} value={_addUrl(socials.ttPage, "tt")} placeholder="Ссылка или никнейм" />
)}
); };