From 0527e0184e32d6c98b5042629608e8743fea4256 Mon Sep 17 00:00:00 2001 From: vrde Date: Wed, 20 May 2015 16:19:40 +0200 Subject: [PATCH] Work on UserStuff --- js/actions/user_actions.js | 22 ++++++++++++++++++++++ js/components/ascribe_app.js | 3 ++- js/components/header.js | 29 +++++++++++++++++++++++++++++ js/fetchers/user_fetcher.js | 21 +++++++++++++++++++++ js/stores/user_store.js | 15 +++++++++++++++ 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 js/actions/user_actions.js create mode 100644 js/fetchers/user_fetcher.js create mode 100644 js/stores/user_store.js diff --git a/js/actions/user_actions.js b/js/actions/user_actions.js new file mode 100644 index 00000000..e2a83adf --- /dev/null +++ b/js/actions/user_actions.js @@ -0,0 +1,22 @@ +import alt from '../alt'; +import UserFetcher from '../fetchers/user_fetcher'; + +class UserActions { + constructor() { + this.generateActions( + 'updateCurrentUser' + ); + } + + fetchCurrentUser() { + UserFetcher.fetchOne() + .then((res) => { + this.actions.updateCurrentUser(res['users'][0]); + }) + .catch((err) => { + console.log(err); + }); + } +}; + +export default alt.createActions(UserActions); diff --git a/js/components/ascribe_app.js b/js/components/ascribe_app.js index a1f84157..5f548ca3 100644 --- a/js/components/ascribe_app.js +++ b/js/components/ascribe_app.js @@ -1,5 +1,6 @@ import React from 'react'; import Router from 'react-router'; +import Header from '../components/header'; let Link = Router.Link; @@ -10,7 +11,7 @@ let AscribeApp = React.createClass({ render() { return (
-
ascribe
+
pieces diff --git a/js/components/header.js b/js/components/header.js index e69de29b..71d67197 100644 --- a/js/components/header.js +++ b/js/components/header.js @@ -0,0 +1,29 @@ +import React from 'react'; +import AltContainer from 'alt/AltContainer'; +import UserActions from '../actions/user_actions'; +import UserStore from '../stores/user_store'; + + +let Header = React.createClass({ + + getInitialState() { + return UserStore.getState(); + }, + + componentDidMount() { + UserStore.listen(this.onChange) + UserActions.fetchCurrentUser(); + }, + + onChange(state) { + this.setState(state); + }, + + render() { + return ( +
hello {this.state.currentUser.username}
+ ) + } +}); + +export default Header; diff --git a/js/fetchers/user_fetcher.js b/js/fetchers/user_fetcher.js new file mode 100644 index 00000000..5bf33529 --- /dev/null +++ b/js/fetchers/user_fetcher.js @@ -0,0 +1,21 @@ +import fetch from 'isomorphic-fetch'; + +import AppConstants from '../constants/application_constants'; +import FetchApiUtils from '../utils/fetch_api_utils'; + +let UserFetcher = { + /** + * Fetch one user from the API. + * If no arg is supplied, load the current user + * + */ + fetchOne() { + return fetch(AppConstants.baseUrl + 'users/', { + headers: { + 'Authorization': 'Basic ' + AppConstants.debugCredentialBase64 + } + }).then((res) => res.json()); + } +}; + +export default UserFetcher; diff --git a/js/stores/user_store.js b/js/stores/user_store.js new file mode 100644 index 00000000..00e4bfbf --- /dev/null +++ b/js/stores/user_store.js @@ -0,0 +1,15 @@ +import alt from '../alt'; +import UserAction from '../actions/user_actions'; + +class UserStore{ + constructor() { + this.currentUser = {} + this.bindActions(UserAction); + } + + onUpdateCurrentUser(user) { + this.currentUser = user; + } +}; + +export default alt.createStore(UserStore);