Work on UserStuff

This commit is contained in:
vrde 2015-05-20 16:19:40 +02:00
parent f575f2b4ca
commit 0527e0184e
5 changed files with 89 additions and 1 deletions

View File

@ -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);

View File

@ -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 (
<div>
<header>ascribe</header>
<Header />
<navigation>
<Link to="pieces">pieces</Link>
</navigation>

View File

@ -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 (
<div>hello {this.state.currentUser.username}</div>
)
}
});
export default Header;

View File

@ -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;

15
js/stores/user_store.js Normal file
View File

@ -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);