From 6e34a72c222c1cd3de0a99e8cdde89249aeda671 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sun, 8 Jan 2017 20:51:58 -0500 Subject: [PATCH] Convert NewUserPanel to a Modal, and to use redux --- js/actions/UserActions.js | 43 ++++++++++- js/components/LunchApp.js | 75 ++++++++----------- .../{NewUserForm.js => NewUserModal.js} | 49 +++++------- js/constants/UserConstants.js | 2 + js/containers/NewUserModalContainer.js | 20 +++++ 5 files changed, 111 insertions(+), 78 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 e7d9f6e..c37b643 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 @@ -97,8 +111,34 @@ function initializeSession(dispatch, session) { dispatch(SuggestionActions.fetchPopular()); } +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({ @@ -202,6 +242,7 @@ function update(user) { } module.exports = { + create: create, fetch: fetch, login: login, logout: logout, diff --git a/js/components/LunchApp.js b/js/components/LunchApp.js index 49d46dc..f4cc2e9 100644 --- a/js/components/LunchApp.js +++ b/js/components/LunchApp.js @@ -10,75 +10,56 @@ var TopBarContainer = require('../containers/TopBarContainer'); var RecordLunchContainer = require('../containers/RecordLunchContainer'); var AccountSettingsModalContainer = require('../containers/AccountSettingsModalContainer'); var LunchStatsContainer = require('../containers/LunchStatsContainer'); -var NewUserForm = require('./NewUserForm'); +var NewUserModalContainer = require('../containers//NewUserModalContainer'); module.exports = React.createClass({ displayName: "LunchApp", 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 = ( - - - - - - - - ); - else - mainContent = ( - -
-

Lunch App

-
-
); - } + if (this.props.user.isUser()) + mainContent = ( + + + + + + + + ); + else + mainContent = ( + +
+

Lunch App

+
+
); return (
@@ -86,6 +67,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)