import { url } from "../../App"
import React, { useContext, useState } from 'react';
import { useHistory, useParams } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { CardContent, Typography, Stack, Avatar, Box, Container, Badge, TextField, InputAdornment } from '@mui/material';
import Button from '@mui/material-next/Button';
import { PersonRemoveRounded, PersonAddRounded, ManageAccountsRounded, BorderColorRounded, ImageSearchRounded, AccountCircle, HttpsRounded } from '@mui/icons-material';
import { actionUserPageSubscribing } from "../../redux/thunks";
import ModalWindow from '../structure/modal';
import ChangeData from "./change_data";
import { ModalForCountsContext, UpdateProfile } from "../../App";
import { EditProfile } from ".";
// Функция отображения кнопки редактирования своего профиля/отписки или подписки на другого юзера
function UptadeProfileButton({ data }) {
// console.log('UserData: ', data)
const history = useHistory()
const { userId } = useParams()
const dispatch = useDispatch()
// отслеживание нажатия кнопки редактирования профиля
const [isEditProfile, setIsEditProfile] = useContext(EditProfile)
// контекст обновления профиля
const [updateProfile, setUpdateProfile] = useContext(UpdateProfile)
// console.log('updateProfile: ', updateProfile)
// определяем мой id
const myId = updateProfile?._id
// определяем всех моих подписчиков
const myFollowingList = updateProfile?.following?.map(user => user?._id)
// проверка, является ли пользователь моим подписчиком
const isFollowing = myFollowingList && (myFollowingList)?.some(item => item === (data?.owner?._id || data?._id))
// функция подписки/отписки - это чисто кнопка
function isSubscribing() {
// при клике на посте
const newData = {
...updateProfile, following: (isFollowing
? updateProfile?.following?.filter(item => item?._id !== (data?.owner?._id || data?._id)) // отписка
// : [...updateProfile?.following, { _id: (data?.owner?._id || data?._id) }] // подписка
: updateProfile?.following
? [...updateProfile?.following, { _id: (data?.owner?._id || data?._id) }] // подписка если уже есть кто-то в подписках
: [{ _id: (data?.owner?._id || data?._id) }] // подписка, если это первая подписка
)
}
dispatch(actionUserPageSubscribing(newData, userId))
}
// функция перехода на редактирование профиля
function upsertProfile() {
// history.push('/updateprofile')
setIsEditProfile(true)
}
return (
//
// {userId === myId &&
//
// }
// disableRipple
// >
//
// Редактировать аккаунт
//
//
// }
// {myFollowingList?.includes(userId) &&
//
// }
// disableRipple
// >
//
// Отписаться
//
//
// }
// {(userId !== myId && !myFollowingList?.includes(userId)) &&
//
// }
// disableRipple
// >
//
// Подписаться
//
//
// }
//
) ||
((myFollowingList?.includes(userId)) && ) ||
((userId !== myId && !myFollowingList?.includes(userId)) && )
}
disableRipple
>
{(userId === myId) && 'Редактировать аккаунт' ||
(myFollowingList?.includes(userId)) && 'Отписаться' ||
(userId !== myId && !myFollowingList?.includes(userId)) && 'Подписаться'
}
)
}
// блок пользовательских данных
export default function BasicCard({ userData }) {
// console.log('userData: ', userData)
// определяем количество постов пользоваеля
const userPostsCount = useSelector(state => state?.promise?.UserPostsCount?.payload)
// контекст обновления профиля
const [updateProfile, setUpdateProfile] = useContext(UpdateProfile)
// console.log('updateProfile: ', updateProfile)
// отслеживание нажатия кнопки редактирования профиля
const [isEditProfile, setIsEditProfile] = useContext(EditProfile)
// контекст модального окна
const [modalName, setModalName, modalArray, setModalArray, openModal, setOpenModal, handleOpenModal, handleCloseModal] = useContext(ModalForCountsContext)
// отслеживание статуса изменения пароля
const [changePassword, setChangePassword] = useState(false)
// открытие поля редактирвоания пароля
function changePasswordButton() {
setChangePassword(true)
}
function userFollowers() {
handleOpenModal({
arr: userData?.followers, name: `Подписчики ${userData?.login}`
})
}
function userFollowing() {
handleOpenModal({
arr: userData?.following, name: `Подписки ${userData?.login}`
})
}
return (
{/*
// }
>
*/}
{isEditProfile &&
}
{!isEditProfile &&
{userData?.login}
{userData?.nick || ''}
{userPostsCount || '0'} публикаций
{userData?.followers?.length || '0'} подписчиков
{userData?.following?.length || '0'} подписок
}
{isEditProfile &&
//
// {/* */}
//
//
//
//
//
//
//
//
//
//
//
//
//
//
// {changePassword &&
//
//
//
//
//
//
//
//
//
// }
//
//
//
}
{modalArray && }
)
}