import Button from 'react-bootstrap/Button'; import {connect} from 'react-redux'; import React, {useState} from 'react'; import { store } from '../store/store'; import image from '../images/card.png'; import { actionAuthLogin } from '../store/authReducer'; import {Link} from 'react-router-dom'; import Form from 'react-bootstrap/Form'; export async function sendForm (url, data) { let error = await fetch(`http://player-api/api/${url}`, { method: 'POST', headers: { ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {}) }, body: data }).then(res => res.json()) .then(data => { if(data.user) { store.dispatch(actionAuthLogin(store.getState().auth?.token, data.user)); return data } else if(data.login){ return data.login[0]; } else{ return data; } }) return error; } export function EditProfile (props) { const [login, setLogin] = useState(props.user?.login); const [name, setName] = useState(props.user?.name); const [avatar, setAvatar] = useState((props.user?.avatar || image)); const [passwordConfirm, setPasswordConfirm] = useState(''); const [password, setPassword] = useState(''); const [textModal, setTextModal] = useState(''); const postForm = async (event) =>{ event.preventDefault(); const data = new FormData(); login && login!==props.user?.login && data.append("login", login); name && name!==props.user?.name && data.append("name", name); avatar.name && data.append("avatar", avatar, avatar.name); setTextModal(( typeof(await sendForm('profile/edit', data))==='string')? (await sendForm('profile/edit', data)) : ''); } const postEditPassword = async (event) =>{ event.preventDefault(); const data = new FormData(); password && data.append("password", password); setTextModal(( typeof(await sendForm('profile/edit', data))==='string')? (await sendForm('profile/edit', data)) : ''); } return (<>
Login: {store.getState().auth?.user?.login}