"use client"; import { FileInput, Label, Modal, useThemeMode } from "flowbite-react"; import { Spinner } from "../Spinner/Spinner"; import useSWR from "swr"; import { ENDPOINTS } from "#/api/config"; import { useEffect, useState } from "react"; import { b64toBlob, tryCatchAPI, unixToDate, useSWRfetcher } from "#/api/utils"; import { ProfileEditPrivacyModal } from "./Profile.EditPrivacyModal"; import { ProfileEditStatusModal } from "./Profile.EditStatusModal"; import { ProfileEditSocialModal } from "./Profile.EditSocialModal"; import { CropModal } from "../CropModal/CropModal"; import { useSWRConfig } from "swr"; import { useUserStore } from "#/store/auth"; import { ProfileEditLoginModal } from "./Profile.EditLoginModal"; import { toast } from "react-toastify"; import { ProfileBlockedUsersModal } from "./Profile.BlockedUsersModal"; export const ProfileEditModal = (props: { isOpen: boolean; setIsOpen: (isOpen: boolean) => void; token: string; profile_id: number; }) => { const [privacyModalOpen, setPrivacyModalOpen] = useState(false); const [statusModalOpen, setStatusModalOpen] = useState(false); const [socialModalOpen, setSocialModalOpen] = useState(false); const [loginModalOpen, setLoginModalOpen] = useState(false); const [blockedModalOpen, setBlockedModalOpen] = useState(false); const [privacyModalSetting, setPrivacyModalSetting] = useState("none"); const [privacySettings, setPrivacySettings] = useState({ privacy_stats: 9, privacy_counts: 9, privacy_social: 9, privacy_friend_requests: 9, }); const [socialBounds, setSocialBounds] = useState({ vk: false, google: false, }); const [status, setStatus] = useState(""); const [login, setLogin] = useState(""); const { mutate } = useSWRConfig(); const userStore = useUserStore(); const theme = useThemeMode(); const [avatarModalProps, setAvatarModalProps] = useState({ isOpen: false, isActionsDisabled: false, selectedImage: null, croppedImage: null, }); const privacy_stat_act_social_text = { 0: "Все пользователи", 1: "Только друзья", 2: "Только я", 9: "Неизвестно", }; const privacy_friend_req_text = { 0: "Все пользователи", 1: "Никто", 9: "Неизвестно", }; function useFetchInfo(url: string) { if (!props.token) { url = ""; } const { data, isLoading, error } = useSWR(url, useSWRfetcher); return [data, isLoading, error]; } const [prefData, prefLoading, prefError] = useFetchInfo( `${ENDPOINTS.user.settings.my}?token=${props.token}` ); const [loginData, loginLoading, loginError] = useFetchInfo( `${ENDPOINTS.user.settings.login.info}?token=${props.token}` ); const handleAvatarPreview = (e: any) => { const file = e.target.files[0]; const fileReader = new FileReader(); fileReader.onloadend = () => { const content = fileReader.result; setAvatarModalProps({ ...avatarModalProps, isOpen: true, selectedImage: content, }); e.target.value = ""; }; fileReader.readAsDataURL(file); }; useEffect(() => { if (prefData) { setPrivacySettings({ privacy_stats: prefData.privacy_stats, privacy_counts: prefData.privacy_counts, privacy_social: prefData.privacy_social, privacy_friend_requests: prefData.privacy_friend_requests, }); setSocialBounds({ vk: prefData.is_vk_bound || prefData.isVkBound || false, google: prefData.is_google_bound || prefData.isGoogleBound || false, }); setStatus(prefData.status); } }, [prefData]); useEffect(() => { if (loginData) { setLogin(loginData.login); } }, [loginData]); useEffect(() => { async function _uploadAvatar() { let block = avatarModalProps.croppedImage.split(";"); let contentType = block[0].split(":")[1]; let realData = block[1].split(",")[1]; const blob = b64toBlob(realData, contentType); const formData = new FormData(); formData.append("image", blob, "cropped.jpg"); formData.append("name", "image"); setAvatarModalProps( (state) => (state = { ...state, isActionsDisabled: true }) ); const tid = toast.loading("Обновление аватара...", { position: "bottom-center", hideProgressBar: true, closeOnClick: false, pauseOnHover: false, draggable: false, theme: theme.mode == "light" ? "light" : "dark", }); const { data, error } = await tryCatchAPI( fetch(`${ENDPOINTS.user.settings.avatar}?token=${props.token}`, { method: "POST", body: formData, }) ); if (error) { toast.update(tid, { render: "Ошибка обновления аватара", type: "error", autoClose: 2500, isLoading: false, closeOnClick: true, draggable: true, }); setAvatarModalProps( (state) => (state = { ...state, isActionsDisabled: false }) ); return; } toast.update(tid, { render: "Аватар обновлён", type: "success", autoClose: 2500, isLoading: false, closeOnClick: true, draggable: true, }); setAvatarModalProps( (state) => (state = { isOpen: false, isActionsDisabled: false, selectedImage: null, croppedImage: null, }) ); mutate( `${ENDPOINTS.user.profile}/${props.profile_id}?token=${props.token}` ); userStore.checkAuth(); } if (avatarModalProps.croppedImage) { _uploadAvatar(); } }, [avatarModalProps.croppedImage]); if (!prefData || !loginData || prefError || loginError) { return <>; } return ( <> props.setIsOpen(false)} size={"7xl"} > Редактирование профиля {prefLoading ? :

Профиль

Приватность

Привязка к сервисам

Недоступно для изменения в данном клиенте

Связанные аккаунты

{socialBounds.vk || socialBounds.google ? "Аккаунт привязан к:" : "не привязан к сервисам"}{" "} {socialBounds.vk && "ВК"} {socialBounds.vk && socialBounds.google && ", "} {socialBounds.google && "Google"}

}
{props.token ? <> : ""} ); };