API & Client: Profile update

This commit is contained in:
SamR1 2018-01-01 17:50:12 +01:00
parent 2c4f3c4462
commit 8e21fc4112
6 changed files with 68 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import datetime import datetime
from flask import Blueprint, jsonify, request from flask import Blueprint, current_app, jsonify, request
from sqlalchemy import exc, or_ from sqlalchemy import exc, or_
from mpwo_api import appLog, bcrypt, db from mpwo_api import appLog, bcrypt, db
@ -182,6 +182,21 @@ def edit_user(user_id):
bio = post_data.get('bio') bio = post_data.get('bio')
birth_date = post_data.get('birth_date') birth_date = post_data.get('birth_date')
location = post_data.get('location') location = post_data.get('location')
password = post_data.get('password')
password_conf = post_data.get('password_conf')
if password is not None and password != '':
if password_conf != password:
response_object = {
'status': 'error',
'message': 'Password and password confirmation don\'t match.\n'
}
return jsonify(response_object), 400
else:
password = bcrypt.generate_password_hash(
password, current_app.config.get('BCRYPT_LOG_ROUNDS')
).decode()
try: try:
user = User.query.filter_by(id=user_id).first() user = User.query.filter_by(id=user_id).first()
user.first_name = first_name user.first_name = first_name
@ -193,6 +208,8 @@ def edit_user(user_id):
if birth_date if birth_date
else None else None
) )
if password is not None and password != '':
user.password = password
db.session.commit() db.session.commit()
response_object = { response_object = {

View File

@ -19,6 +19,14 @@ function ProfileError(message) {
return { type: 'PROFILE_ERROR', message } return { type: 'PROFILE_ERROR', message }
} }
function PwdError(message) {
return { type: 'PWD_ERROR', message }
}
function initProfileFormData(user) {
return { type: 'INIT_PROFILE_FORM', user }
}
export const handleFormChange = (target, value) => ({ export const handleFormChange = (target, value) => ({
type: 'UPDATE_USER_FORMDATA', type: 'UPDATE_USER_FORMDATA',
target, target,
@ -31,10 +39,6 @@ export const updateProfileFormData = (target, value) => ({
value, value,
}) })
function initProfileFormData(user) {
return { type: 'INIT_PROFILE_FORM', user }
}
export function getProfile(dispatch) { export function getProfile(dispatch) {
return mpwoApi return mpwoApi
.getProfile() .getProfile()
@ -147,6 +151,10 @@ export function handleProfileFormSubmit(event) {
event.preventDefault() event.preventDefault()
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState() const state = getState()
if (state.formProfile.formProfile.password !==
state.formProfile.formProfile.passwordConf) {
dispatch(PwdError('Password and password confirmation don\'t match.'))
} else {
return mpwoApi return mpwoApi
.updateProfile(state.formProfile.formProfile) .updateProfile(state.formProfile.formProfile)
.then(ret => { .then(ret => {
@ -161,4 +169,6 @@ export function handleProfileFormSubmit(event) {
throw error throw error
}) })
} }
}
} }

View File

@ -17,6 +17,7 @@ class ProfileEdit extends React.Component {
const { formProfile, const { formProfile,
onHandleFormChange, onHandleFormChange,
onHandleProfileFormSubmit, onHandleProfileFormSubmit,
message,
user user
} = this.props } = this.props
return ( return (
@ -24,6 +25,9 @@ class ProfileEdit extends React.Component {
<Helmet> <Helmet>
<title>mpwo - {user.username} - Edit Profile</title> <title>mpwo - {user.username} - Edit Profile</title>
</Helmet> </Helmet>
{ message !== '' && (
<code>{message}</code>
)}
<div className="container"> <div className="container">
<h1 className="page-title">Profile Edition</h1> <h1 className="page-title">Profile Edition</h1>
<div className="row"> <div className="row">
@ -62,6 +66,29 @@ class ProfileEdit extends React.Component {
/> />
</label> </label>
</div> </div>
<div className="form-group">
<label>
Password:
<input
name="password"
className="form-control input-lg"
type="password"
onChange={onHandleFormChange}
/>
</label>
</div>
<div className="form-group">
<label>
Password Confirmation:
<input
name="passwordConf"
className="form-control input-lg"
type="password"
onChange={onHandleFormChange}
/>
</label>
</div>
<hr />
<div className="form-group"> <div className="form-group">
<label> <label>
First Name: First Name:
@ -146,6 +173,7 @@ class ProfileEdit extends React.Component {
export default connect( export default connect(
state => ({ state => ({
formProfile: state.formProfile.formProfile, formProfile: state.formProfile.formProfile,
message: state.message,
user: state.user, user: state.user,
}), }),
dispatch => ({ dispatch => ({

View File

@ -58,7 +58,9 @@ export default class MpwoApi {
last_name: form.lastName, last_name: form.lastName,
bio: form.bio, bio: form.bio,
location: form.location, location: form.location,
birth_date: form.birthdate, birth_date: form.birthDate,
password: form.password,
password_conf: form.passwordConf,
}), }),
}) })
return fetch(request) return fetch(request)

View File

@ -49,6 +49,7 @@ const message = (state = initial.message, action) => {
switch (action.type) { switch (action.type) {
case 'AUTH_ERROR': case 'AUTH_ERROR':
case 'PROFILE_ERROR': case 'PROFILE_ERROR':
case 'PWD_ERROR':
return action.message return action.message
case 'LOGOUT': case 'LOGOUT':
case 'PROFILE_SUCCESS': case 'PROFILE_SUCCESS':

View File

@ -28,7 +28,9 @@ export default {
lastName: '', lastName: '',
bio: '', bio: '',
location: '', location: '',
birthDate: '' birthDate: '',
password: '',
passwordConf: ''
} }
}, },
} }