From 871aa88ea6880f1ebf36886572646f1174a3c126 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sun, 8 Jan 2017 21:04:01 -0500 Subject: [PATCH] Convert NewUserPanel to a Modal, and to use redux --- js/actions/UserActions.js | 43 ++++++++- js/components/MoneyGoApp.js | 87 ++++++++----------- .../{NewUserForm.js => NewUserModal.js} | 49 ++++------- js/constants/UserConstants.js | 2 + js/containers/NewUserModalContainer.js | 20 +++++ 5 files changed, 117 insertions(+), 84 deletions(-) rename js/components/{NewUserForm.js => NewUserModal.js} (83%) create mode 100644 js/containers/NewUserModalContainer.js diff --git a/js/actions/UserActions.js b/js/actions/UserActions.js index 1efedab..33ec638 100644 --- a/js/actions/UserActions.js +++ b/js/actions/UserActions.js @@ -9,6 +9,20 @@ var User = models.User; var Session = models.Session; var Error = models.Error; +function createUser(user) { + return { + type: UserConstants.CREATE_USER, + user: user + } +} + +function userCreated(user) { + return { + type: UserConstants.USER_CREATED, + user: user + } +} + function loginUser() { return { type: UserConstants.LOGIN_USER @@ -95,8 +109,34 @@ function initializeSession(dispatch, session) { dispatch(SecurityActions.fetchAll()); } +function create(user) { + return function(dispatch) { + dispatch(createUser(user)); + $.ajax({ + type: "POST", + dataType: "json", + url: "user/", + data: {user: user.toJSON()}, + success: function(data, status, jqXHR) { + var e = new Error(); + e.fromJSON(data); + if (e.isError()) { + dispatch(ErrorActions.serverError(e)); + } else { + var u = new User(); + u.fromJSON(data); + dispatch(userCreated(u)); + } + }, + error: function(jqXHR, status, error) { + dispatch(ErrorActions.ajaxError(e)); + } + }); + }; +} + function login(user) { - return function (dispatch) { + return function(dispatch) { dispatch(loginUser()); $.ajax({ @@ -200,6 +240,7 @@ function update(user) { } module.exports = { + create: create, fetch: fetch, login: login, logout: logout, diff --git a/js/components/MoneyGoApp.js b/js/components/MoneyGoApp.js index 98d27de..dd72da8 100644 --- a/js/components/MoneyGoApp.js +++ b/js/components/MoneyGoApp.js @@ -7,7 +7,7 @@ var Tab = ReactBootstrap.Tab; var Modal = ReactBootstrap.Modal; var TopBarContainer = require('../containers/TopBarContainer'); -var NewUserForm = require('./NewUserForm'); +var NewUserModalContainer = require('../containers/NewUserModalContainer'); var AccountSettingsModalContainer = require('../containers/AccountSettingsModalContainer'); var AccountsTabContainer = require('../containers/AccountsTabContainer'); var SecuritiesTabContainer = require('../containers/SecuritiesTabContainer'); @@ -16,75 +16,56 @@ module.exports = React.createClass({ displayName: "MoneyGoApp", getInitialState: function() { return { - hash: "home", + showNewUserModal: false, showAccountSettingsModal: false }; }, componentDidMount: function() { this.props.tryResumingSession(); - this.handleHashChange(); - if ("onhashchange" in window) { - window.onhashchange = this.handleHashChange; - } - }, - handleHashChange: function() { - var hash = location.hash.replace(/^#/, ''); - if (hash.length == 0) - hash = "home"; - if (hash != this.state.hash) - this.setHash(hash); - }, - setHash: function(hash) { - location.hash = hash; - if (this.state.hash != hash) - this.setState({hash: hash}); }, handleAccountSettings: function() { this.setState({showAccountSettingsModal: true}); }, handleSettingsSubmitted: function(user) { - this.setState({ - showAccountSettingsModal: false - }); + this.setState({showAccountSettingsModal: false}); }, handleSettingsCanceled: function() { this.setState({showAccountSettingsModal: false}); }, handleCreateNewUser: function() { - this.setHash("new_user"); + this.setState({showNewUserModal: true}); }, - handleGoHome: function() { - this.setHash("home"); + handleNewUserCreated: function() { + this.setState({showNewUserModal: false}); + }, + handleNewUserCanceled: function() { + this.setState({showNewUserModal: false}); }, render: function() { var mainContent; - if (this.state.hash == "new_user") { - mainContent = - } else { - if (this.props.user.isUser()) - mainContent = ( - - - - - - - - Scheduled transactions go here... - Budgets go here... - Reports go here... - ); - else - mainContent = ( - -
-

MoneyGo

-

Go manage your money.

-
-
); - } + if (this.props.user.isUser()) + mainContent = ( + + + + + + + + Scheduled transactions go here... + Budgets go here... + Reports go here... + ); + else + mainContent = ( + +
+

MoneyGo

+

Go manage your money.

+
+
); return (
@@ -92,6 +73,10 @@ module.exports = React.createClass({ onCreateNewUser={this.handleCreateNewUser} onAccountSettings={this.handleAccountSettings} /> {mainContent} + Create New User; return ( - + + + Create New user + + {this.state.error}
@@ -151,15 +134,17 @@ module.exports = React.createClass({ - - + +
+ + - - -
+ + ); } }); diff --git a/js/constants/UserConstants.js b/js/constants/UserConstants.js index f090be4..c88216e 100644 --- a/js/constants/UserConstants.js +++ b/js/constants/UserConstants.js @@ -1,6 +1,8 @@ var keyMirror = require('keymirror'); module.exports = keyMirror({ + CREATE_USER: null, + USER_CREATED: null, LOGIN_USER: null, USER_LOGGEDIN: null, LOGOUT_USER: null, diff --git a/js/containers/NewUserModalContainer.js b/js/containers/NewUserModalContainer.js new file mode 100644 index 0000000..4b7f8d9 --- /dev/null +++ b/js/containers/NewUserModalContainer.js @@ -0,0 +1,20 @@ +var connect = require('react-redux').connect; + +var UserActions = require('../actions/UserActions'); + +var NewUserModal = require('../components/NewUserModal'); + +function mapStateToProps(state) { + return {} +} + +function mapDispatchToProps(dispatch) { + return { + createNewUser: function(user) {dispatch(UserActions.create(user))} + } +} + +module.exports = connect( + mapStateToProps, + mapDispatchToProps +)(NewUserModal)