import React from 'react';
import Repos from './Github/Repos';
import UserProfile from './Github/UserProfile';
import Notes from './Notes/Notes';
import helpers from '../utils/helpers';
import Rebase from 're-base';
var base = Rebase.createClass('https://react-git-profiler.firebaseio.com');
class Profile extends React.Component {
constructor (props) { // We have access to a constructor func in ES6 classes that is initialized with the class
super(props);
this.state = {
notes: [],
bio: {},
repos: []
};
}
init () {
this.ref = base.bindToState(this.router.getCurrentParams().username, { // When the .username endpoint changes, this will update the state for 'notes'
context: this,
asArray: true,
state: 'notes'
});
helpers.getGithubInfo(this.router.getCurrentParams().username)
.then((dataObj) => {
this.setState({
bio: dataObj.bio,
repos: dataObj.repos
});
}); // this refers to this in the upper contexts
}
componentWillMount () {
this.router = this.context.router;
}
componentDidMount () { // Where all AJAX requests will live
this.init();
}
componentWillUnmount () { // So that we don't get listeners keep adding, we will unmount them
base.removeBinding(this.ref);
}
componentWillReceiveProps () {
base.removeBinding(this.ref);
this.init();
}
handleAddNote (newNote) { // Make the function in the component where it will be evoked
base.post(this.router.getCurrentParams().username, {
data: this.state.notes.concat([newNote])
}); // in .set() we get the previous values in an array and output a new array with newNote added
}
render () {
var username = this.router.getCurrentParams().username; // "username" is what we use in the route URL in routes.js
return (
<div className="row">
<div className="col-md-4">
<UserProfile username={username} bio={this.state.bio} />
</div>
<div className="col-md-4">
<Repos username={username} repos={this.state.repos} />
</div>
<div className="col-md-4">
<Notes
username={username}
notes={this.state.notes}
addNote={this.handleAddNote.bind(this)}
/>
</div>
</div>
)
}
};
Profile.contextTypes = {
router: React.PropTypes.func.isRequired
};
export default Profile;
/* var React = require('react');
//var Router = require('react-router');
var Repos = require('./Github/Repos.js');
var UserProfile = require('./Github/UserProfile.js');
var Notes = require('./Notes/Notes.js');
//var ReactFireMixin = require('reactfire'); - Removed, can't use mixins with ES6 classes
var Firebase = require('firebase');
var helpers = require('../utils/helpers');
var Profile = React.createClass({
// Allows to query the route parameter and to get the specific username, also allows communication of react fire with firebase and the profile component
mixins: [Router.State, ReactFireMixin],
getInitialState: function () {
return {
notes: [],
bio: {},
repos: []
}
},
init: function () {
var childRef = this.ref.child(this.getParams().username);
this.bindAsArray(childRef, 'notes'); // .bindAsArray is added as a property when we inserted "ReactFireMixin" into the mixins: array above
helpers.getGithubInfo(this.getParams().username)
.then(function (dataObj) {
this.setState({
bio: dataObj.bio,
repos: dataObj.repos
});
}.bind(this)); // this refers to this in the upper contexts
},
componentDidMount: function () { // Where all AJAX requests will live
this.ref = new Firebase('https://react-git-profiler.firebaseio.com');
this.init();
},
componentWillUnmount: function () { // So that we don't get listeners keep adding, we will unmount them
this.unbind('notes');
},
componentWillReceiveProps: function () {
this.unbind('notes');
this.init();
},
handleAddNote: function (newNote) { // Make the function in the component where it will be evoked
this.ref.child(this.getParams().username).set(this.state.notes.concat([newNote])); // Get current profile and replace the data at the selected endpoint with the value in .set()
// in .set() we get the previous values in an array and output a new array with newNote added
},
render: function () {
var username = this.getParams().username; // "username" is what we use in the route URL in routes.js
return (
<div className="row">
<div className="col-md-4">
<UserProfile username={username} bio={this.state.bio} />
</div>
<div className="col-md-4">
<Repos username={username} repos={this.state.repos} />
</div>
<div className="col-md-4">
<Notes
username={username}
notes={this.state.notes}
addNote={this.handleAddNote}
/>
</div>
</div>
)
}
});
module.exports = Profile;*/