diff --git a/src/pages/account/account.js b/src/pages/account/account.js new file mode 100644 index 0000000..d3401a5 --- /dev/null +++ b/src/pages/account/account.js @@ -0,0 +1,113 @@ +/** + * This file is part of VILLASweb. + * + * VILLASweb is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VILLASweb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VILLASweb. If not, see . + ******************************************************************************/ + +import { useState } from "react"; +import IconButton from "../../common/buttons/icon-button"; +import { Form, Row, Col } from 'react-bootstrap'; +import EditOwnUserDialog from "./edit-own-user"; +import { currentUser } from "../../localStorage"; +import NotificationsFactory from "../../common/data-managers/notifications-factory"; +import notificationsDataManager from "../../common/data-managers/notifications-data-manager"; +import { useUpdateUserMutation } from "../../store/apiSlice"; + +const Account = () => { + + const [isEditModalOpened, setIsEditModalOpened] = useState(false); + const [updateUser] = useUpdateUserMutation(); + + const buttonStyle = { + marginLeft: '10px', + } + + const iconStyle = { + height: '30px', + width: '30px' + } + + const handleEdit = async (data) => { + if(data){ + try { + await updateUser(data); + } catch (err) { + if(err.data){ + notificationsDataManager.addNotification(NotificationsFactory.UPDATE_ERROR(err.data.message)); + } else { + console.log('Error', err); + } + } + } + setIsEditModalOpened(false); + } + + return ( +
+

Account + + setIsEditModalOpened(true)} + icon='edit' + buttonStyle={buttonStyle} + iconStyle={iconStyle} + /> + +

+ + {currentUser ? + <> +
+ + Username + + + + + + E-mail + + + + + + Role + + + + + + Created at + + + + + +
+ + handleEdit(data)} + user={currentUser} + /> + + :
+ } +
+ ); +} + +export default Account; diff --git a/src/pages/account/edit-own-user.js b/src/pages/account/edit-own-user.js new file mode 100644 index 0000000..fd9351c --- /dev/null +++ b/src/pages/account/edit-own-user.js @@ -0,0 +1,113 @@ +/** + * This file is part of VILLASweb. + * + * VILLASweb is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VILLASweb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VILLASweb. If not, see . + ******************************************************************************/ + +import React from 'react'; +import { Form, Col } from 'react-bootstrap'; +import Dialog from '../../common/dialogs/dialog'; +import NotificationsDataManager from "../../common/data-managers/notifications-data-manager"; +import NotificationsFactory from "../../common/data-managers/notifications-factory"; + + +class EditOwnUserDialog extends React.Component { + constructor(props) { + super(props); + + this.state = { + username: "", + mail: this.props.user.mail, + password: '', + oldPassword: '', + confirmPassword: '' + } + } + + onClose(canceled) { + if (canceled === false) { + + let user = {}; + user.id = this.props.user.id; + user.password = this.state.password; + user.oldPassword = this.state.oldPassword; + user.confirmPassword = this.state.confirmPassword + + if (this.state.username != null && this.state.username !== this.props.user.username){ + user.username = this.state.username; + } + + if (this.state.mail != null && this.state.mail !== this.props.user.mail){ + user.mail = this.state.mail; + } + + if (this.state.password !== '' && this.state.oldPassword !== '' && this.state.password === this.state.confirmPassword ) { + user.password = this.state.password; + user.oldPassword = this.state.oldPassword; + } else if (this.state.password !== '' && this.state.password !== this.state.confirmPassword) { + NotificationsDataManager.addNotification(NotificationsFactory.UPDATE_ERROR('New password not correctly confirmed')); + } + + this.props.onClose(user); + } else { + this.props.onClose(); + } + } + + handleChange(e) { + this.setState({ [e.target.id]: e.target.value }); + } + + resetState() { + this.setState({ + username: this.props.user.username, + mail: this.props.user.mail, + oldPassword: '', + confirmPassword: '', + password: '', + }); + } + + render() { + return ( + this.onClose(c)} onReset={() => this.resetState()} valid={true}> +
+ + Username + this.handleChange(e)} autoComplete="username" /> + + + + E-mail + this.handleChange(e)} autoComplete="email" /> + + + Old Password + this.handleChange(e)} autoComplete="current-password" /> + + + New Password + this.handleChange(e)} autoComplete="new-password" /> + + + Confirm New Password + this.handleChange(e)} autoComplete="new-password" /> + +
+
+ ); + } +} + +export default EditOwnUserDialog;