Fix user edit frontend and backend

This commit is contained in:
Grégoire Delattre 2016-11-24 20:21:02 +01:00
parent d6df379fc4
commit 85672b5242
6 changed files with 42 additions and 21 deletions

View File

@ -15,6 +15,7 @@
"react-bootstrap": "^0.30.6", "react-bootstrap": "^0.30.6",
"react-dom": "^15.3.2", "react-dom": "^15.3.2",
"react-redux": "^4.4.6", "react-redux": "^4.4.6",
"react-redux-form": "^1.2.4",
"react-router": "^3.0.0", "react-router": "^3.0.0",
"react-router-bootstrap": "^0.23.1", "react-router-bootstrap": "^0.23.1",
"react-router-redux": "^4.0.7", "react-router-redux": "^4.0.7",

View File

@ -134,7 +134,7 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error {
return err return err
} }
if data.Password == "" && data.PasswordConfirm != "" { if data.Password != "" && data.PasswordConfirm != "" {
if data.Password != data.PasswordConfirm { if data.Password != data.PasswordConfirm {
return e.RenderError(w, fmt.Errorf("Passwords empty or missmatch")) return e.RenderError(w, fmt.Errorf("Passwords empty or missmatch"))
} }
@ -145,10 +145,6 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error {
if err != nil { if err != nil {
return err return err
} }
if err := user.Update(e.Database); err != nil {
return err
}
} }
// Update the polochon config // Update the polochon config
@ -162,5 +158,10 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error {
return err return err
} }
// Save the user with the new configurations
if err := user.Update(e.Database); err != nil {
return err
}
return e.RenderOK(w, "user updated") return e.RenderOK(w, "user updated")
} }

View File

@ -55,8 +55,7 @@ func (u *User) GetConfig(key string, v interface{}) error {
// SetConfig marshal v into json for specified config key // SetConfig marshal v into json for specified config key
func (u *User) SetConfig(key string, v interface{}) error { func (u *User) SetConfig(key string, v interface{}) error {
var configMap map[string]*json.RawMessage var configMap map[string]*json.RawMessage
err := u.RawConfig.Unmarshal(&configMap) if err := u.RawConfig.Unmarshal(&configMap); err != nil {
if err != nil {
return err return err
} }
@ -65,6 +64,10 @@ func (u *User) SetConfig(key string, v interface{}) error {
return err return err
} }
if configMap == nil {
configMap = map[string]*json.RawMessage{}
}
r := json.RawMessage(b) r := json.RawMessage(b)
configMap[key] = &r configMap[key] = &r
b, err = json.Marshal(configMap) b, err = json.Marshal(configMap)

View File

@ -1,4 +1,5 @@
import React from 'react' import React from 'react'
import { Control, Form } from 'react-redux-form';
export default class UserEdit extends React.Component { export default class UserEdit extends React.Component {
componentWillMount() { componentWillMount() {
@ -8,19 +9,15 @@ export default class UserEdit extends React.Component {
super(props); super(props);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
} }
handleSubmit(e) { handleSubmit() {
e.preventDefault();
this.props.updateUser({ this.props.updateUser({
'polochon_url': this.refs.polochonUrl.value, 'polochon_url': this.props.userStore.polochonUrl,
'polochon_token': this.refs.polochonToken.value, 'polochon_token': this.props.userStore.polochonToken,
'password': this.refs.newPassword.value, 'password': this.refs.newPassword.value,
'password_confirm': this.refs.newPasswordConfirm.value, 'password_confirm': this.refs.newPasswordConfirm.value,
}); });
} }
render() { render() {
// TODO make this fields editable
const polochonUrl = this.props.userStore.polochonUrl;
const polochonToken = this.props.userStore.polochonToken;
return ( return (
<div className="container"> <div className="container">
<div className="content-fluid"> <div className="content-fluid">
@ -28,15 +25,15 @@ export default class UserEdit extends React.Component {
<h2>Edit user</h2> <h2>Edit user</h2>
<hr /> <hr />
<form ref="userEditForm" className="form-horizontal" onSubmit={(e) => this.handleSubmit(e)}> <Form model="userStore" className="form-horizontal" onSubmit={(val) => this.handleSubmit(val)}>
<div className="form-group"> <div className="form-group">
<label className="control-label">Polochon URL</label> <label className="control-label">Polochon URL</label>
<input autoFocus="autofocus" className="form-control" type="text" ref="polochonUrl" value={polochonUrl} /> <Control.text model="userStore.polochonUrl" className="form-control" />
</div> </div>
<div className="form-group"> <div className="form-group">
<label className="control-label">Polochon token</label> <label className="control-label">Polochon token</label>
<input className="form-control" ref="polochonToken" type="text" value={polochonToken} /> <Control.text model="userStore.polochonToken" className="form-control"/>
</div> </div>
<hr /> <hr />
@ -54,7 +51,7 @@ export default class UserEdit extends React.Component {
<div> <div>
<input className="btn btn-primary pull-right" type="submit" value="Update"/> <input className="btn btn-primary pull-right" type="submit" value="Update"/>
</div> </div>
</form> </Form>
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,11 +1,14 @@
import { combineReducers } from 'redux'; import { combineForms } from 'react-redux-form'
import { routerReducer } from 'react-router-redux' import { routerReducer } from 'react-router-redux'
import movieStore from './movies' import movieStore from './movies'
import userStore from './users' import userStore from './users'
import errors from './errors' import errors from './errors'
const rootReducer = combineReducers({ // Use combine form form react-redux-form, it's a thin wrapper arround the
// default combinedReducers provided with React. It allows the forms to be
// linked directly to the store.
const rootReducer = combineForms({
routing: routerReducer, routing: routerReducer,
movieStore, movieStore,
userStore, userStore,

View File

@ -1795,6 +1795,10 @@ https-browserify@0.0.0:
version "0.0.0" version "0.0.0"
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-0.0.0.tgz#b3ffdfe734b2a3d4a9efd58e8654c91fce86eafd" resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-0.0.0.tgz#b3ffdfe734b2a3d4a9efd58e8654c91fce86eafd"
icepick@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/icepick/-/icepick-1.3.0.tgz#e4942842ed8f9ee778d7dd78f7e36627f49fdaef"
iconv-lite@~0.4.13: iconv-lite@~0.4.13:
version "0.4.13" version "0.4.13"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2"
@ -1866,7 +1870,7 @@ invariant@^2.0.0, invariant@^2.2.0, invariant@^2.2.1:
dependencies: dependencies:
loose-envify "^1.0.0" loose-envify "^1.0.0"
invariant@^2.1.0: invariant@^2.1.0, invariant@~2.2.1:
version "2.2.2" version "2.2.2"
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360"
dependencies: dependencies:
@ -2306,6 +2310,10 @@ lodash.templatesettings@^3.0.0:
lodash._reinterpolate "^3.0.0" lodash._reinterpolate "^3.0.0"
lodash.escape "^3.0.0" lodash.escape "^3.0.0"
lodash@^4.10.0:
version "4.17.2"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
lodash@^4.2.0: lodash@^4.2.0:
version "4.16.6" version "4.16.6"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.16.6.tgz#d22c9ac660288f3843e16ba7d2b5d06cca27d777" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.16.6.tgz#d22c9ac660288f3843e16ba7d2b5d06cca27d777"
@ -3093,6 +3101,14 @@ react-redux:
lodash "^4.2.0" lodash "^4.2.0"
loose-envify "^1.1.0" loose-envify "^1.1.0"
react-redux-form:
version "1.2.4"
resolved "https://registry.yarnpkg.com/react-redux-form/-/react-redux-form-1.2.4.tgz#924c5ac06dd52f21efc961df6aed9c10bfe27de9"
dependencies:
icepick "^1.1.0"
invariant "~2.2.1"
lodash "^4.10.0"
react-router: react-router:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/react-router/-/react-router-3.0.0.tgz#3f313e4dbaf57048c48dd0a8c3cac24d93667dff" resolved "https://registry.yarnpkg.com/react-router/-/react-router-3.0.0.tgz#3f313e4dbaf57048c48dd0a8c3cac24d93667dff"