import React, {ChangeEvent, FormEvent} from "react"; import {connect} from "react-redux"; import {fetchUser, updateUser} from "../../../api/users"; import Loading from "../../../ui/Loading"; import './APUser.sass'; import {addNotification} from "../../../store/actions"; import NotificationMessage from "../../../ui/Notifications/NotificationMessage"; import Button from "../../../ui/Button/Button"; type Props = { userId: number addNotification: (notification: React.ReactNode) => void }; type State = { user: UserType | null username: string }; class ModifyUser extends React.Component { state: State = { user: null, username: '' } componentDidMount() { const { userId } = this.props; fetchUser(userId).then((user: UserType) => { this.setState({ user, username: user.username }); document.title = `${user.username} / Админ-панель`; }) } onChange = (event: ChangeEvent) => { const user = Object.assign({}, this.state.user); const target = event.target; // @ts-ignore user[target.name] = target.type === 'checkbox' ? target.checked : target.value; this.setState({ user }) } onSubmit = (event: FormEvent) => { event.preventDefault(); const { user } = this.state; const { addNotification } = this.props; if(user === null) { console.error('Пользователь - null!'); return; } updateUser(user).then(() => { const notification = ( Пользователь успешно изменен! ) addNotification(notification) }); } render() { const { user, username } = this.state; if(!user) return ; return (
{username}
) } } export default connect( (state: StoreState) => ({ user: state.currentUser }), (dispatch) => ({ addNotification: (notification: React.ReactNode) => { dispatch(addNotification(notification)) } }) )(ModifyUser);