From 85672b52429b08e93c14b58ec9c3397d733d7700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Thu, 24 Nov 2016 20:21:02 +0100 Subject: [PATCH] Fix user edit frontend and backend --- package.json | 1 + src/internal/users/handlers.go | 11 ++++++----- src/internal/users/users.go | 7 +++++-- src/public/js/components/users/edit.js | 19 ++++++++----------- src/public/js/reducers/index.js | 7 +++++-- yarn.lock | 18 +++++++++++++++++- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index f8f40e3..db8bd19 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "react-bootstrap": "^0.30.6", "react-dom": "^15.3.2", "react-redux": "^4.4.6", + "react-redux-form": "^1.2.4", "react-router": "^3.0.0", "react-router-bootstrap": "^0.23.1", "react-router-redux": "^4.0.7", diff --git a/src/internal/users/handlers.go b/src/internal/users/handlers.go index 7f95366..eda6af9 100644 --- a/src/internal/users/handlers.go +++ b/src/internal/users/handlers.go @@ -134,7 +134,7 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error { return err } - 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")) } @@ -145,10 +145,6 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error { if err != nil { return err } - - if err := user.Update(e.Database); err != nil { - return err - } } // Update the polochon config @@ -162,5 +158,10 @@ func EditHandler(e *web.Env, w http.ResponseWriter, r *http.Request) error { 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") } diff --git a/src/internal/users/users.go b/src/internal/users/users.go index ad1b224..30b7d25 100644 --- a/src/internal/users/users.go +++ b/src/internal/users/users.go @@ -55,8 +55,7 @@ func (u *User) GetConfig(key string, v interface{}) error { // SetConfig marshal v into json for specified config key func (u *User) SetConfig(key string, v interface{}) error { var configMap map[string]*json.RawMessage - err := u.RawConfig.Unmarshal(&configMap) - if err != nil { + if err := u.RawConfig.Unmarshal(&configMap); err != nil { return err } @@ -65,6 +64,10 @@ func (u *User) SetConfig(key string, v interface{}) error { return err } + if configMap == nil { + configMap = map[string]*json.RawMessage{} + } + r := json.RawMessage(b) configMap[key] = &r b, err = json.Marshal(configMap) diff --git a/src/public/js/components/users/edit.js b/src/public/js/components/users/edit.js index ca34331..4e28e8b 100644 --- a/src/public/js/components/users/edit.js +++ b/src/public/js/components/users/edit.js @@ -1,4 +1,5 @@ import React from 'react' +import { Control, Form } from 'react-redux-form'; export default class UserEdit extends React.Component { componentWillMount() { @@ -8,19 +9,15 @@ export default class UserEdit extends React.Component { super(props); this.handleSubmit = this.handleSubmit.bind(this); } - handleSubmit(e) { - e.preventDefault(); + handleSubmit() { this.props.updateUser({ - 'polochon_url': this.refs.polochonUrl.value, - 'polochon_token': this.refs.polochonToken.value, + 'polochon_url': this.props.userStore.polochonUrl, + 'polochon_token': this.props.userStore.polochonToken, 'password': this.refs.newPassword.value, 'password_confirm': this.refs.newPasswordConfirm.value, }); } render() { - // TODO make this fields editable - const polochonUrl = this.props.userStore.polochonUrl; - const polochonToken = this.props.userStore.polochonToken; return (
@@ -28,15 +25,15 @@ export default class UserEdit extends React.Component {

Edit user


-
this.handleSubmit(e)}> + this.handleSubmit(val)}>
- +
- +

@@ -54,7 +51,7 @@ export default class UserEdit extends React.Component {
-
+
diff --git a/src/public/js/reducers/index.js b/src/public/js/reducers/index.js index 8fb7d7c..9b8c802 100644 --- a/src/public/js/reducers/index.js +++ b/src/public/js/reducers/index.js @@ -1,11 +1,14 @@ -import { combineReducers } from 'redux'; +import { combineForms } from 'react-redux-form' import { routerReducer } from 'react-router-redux' import movieStore from './movies' import userStore from './users' 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, movieStore, userStore, diff --git a/yarn.lock b/yarn.lock index 59f4ae6..09d5596 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1795,6 +1795,10 @@ https-browserify@0.0.0: version "0.0.0" 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: version "0.4.13" 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: loose-envify "^1.0.0" -invariant@^2.1.0: +invariant@^2.1.0, invariant@~2.2.1: version "2.2.2" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360" dependencies: @@ -2306,6 +2310,10 @@ lodash.templatesettings@^3.0.0: lodash._reinterpolate "^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: version "4.16.6" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.16.6.tgz#d22c9ac660288f3843e16ba7d2b5d06cca27d777" @@ -3093,6 +3101,14 @@ react-redux: lodash "^4.2.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: version "3.0.0" resolved "https://registry.yarnpkg.com/react-router/-/react-router-3.0.0.tgz#3f313e4dbaf57048c48dd0a8c3cac24d93667dff"