Skip to content

Commit

Permalink
use events to indicate when clubs have changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Mar 19, 2015
1 parent 4869505 commit 6d61ad2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
21 changes: 8 additions & 13 deletions components/clubs-page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,21 @@ var ModalLearnMore = React.createClass({
var ClubsPage = React.createClass({
mixins: [ModalManagerMixin, TeachAPIClientMixin],
statics: {
teachAPIEvents: {
'clubs:change': 'handleClubsChange'
},
pageClassName: "clubs"
},
getInitialState: function() {
return {
clubs: []
clubs: this.getTeachAPI().getClubs()
};
},
componentDidMount: function() {
this.getTeachAPI().getAllClubsData(function(err, data) {
if (!this.isMounted()) {
// We were unmounted before the API request completed,
// so do nothing.
return;
}
if (err) {
console.log(err)
return;
}
this.setState({clubs: data});
}.bind(this));
this.getTeachAPI().updateClubs();
},
handleClubsChange: function(clubs) {
this.setState({clubs: clubs});
},
showAddYourClubModal: function() {
this.showModal(ModalAddYourClub);
Expand Down
10 changes: 8 additions & 2 deletions lib/teach-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function TeachAPI(options) {
this.storage = options.storage || (
process.browser ? window.sessionStorage : {}
);
this._clubs = [];
}

util.inherits(TeachAPI, EventEmitter);
Expand Down Expand Up @@ -74,15 +75,20 @@ _.extend(TeachAPI.prototype, {

return req;
},
getAllClubsData: function(callback) {
getClubs: function() {
return this._clubs;
},
updateClubs: function(callback) {
callback = callback || function () {};
return this.request('get', '/api/clubs')
.accept('json')
.end(function(err, res) {
if (err) {
return callback(err);
}
this.emit('clubs:change', res.body);
callback(null, res.body);
});
}.bind(this));
}
});

Expand Down
8 changes: 4 additions & 4 deletions test/browser/teach-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('TeachAPI', function() {
should(api.getUsername()).equal(null);
});

describe('getAllClubsData()', function() {
describe('updateClubs()', function() {
var api;

beforeEach(function() {
Expand All @@ -102,14 +102,14 @@ describe('TeachAPI', function() {
});

it('accesses /api/clubs', function() {
api.getAllClubsData(function() {});
api.updateClubs();
requests.length.should.equal(1);
requests[0].method.should.eql('get');
requests[0].url.should.eql('http://example.org/api/clubs');
});

it('returns parsed JSON on success', function(done) {
api.getAllClubsData(function(err, data) {
api.updateClubs(function(err, data) {
should(err).equal(null);
data.should.eql([{name: "blah"}]);
done();
Expand All @@ -120,7 +120,7 @@ describe('TeachAPI', function() {
});

it('returns an error on failure', function(done) {
api.getAllClubsData(function(err, data) {
api.updateClubs(function(err, data) {
err.message.should.eql("Internal Server Error");
done();
});
Expand Down

0 comments on commit 6d61ad2

Please sign in to comment.