Skip to content

Commit

Permalink
add rudimentary club deletion.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Mar 19, 2015
1 parent f46a91c commit e93f9be
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
18 changes: 15 additions & 3 deletions components/clubs-page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,21 @@ var ClubsPage = React.createClass({
showLearnMoreModal: function() {
this.showModal(ModalLearnMore);
},
handleClubDelete: function(url) {
console.log(url);
window.alert("Sorry, club deletion has not yet been implemented.");
handleClubDelete: function(url, clubName) {
var confirmed = window.confirm(
"Are you sure you want to delete the club \"" + clubName + "\"? " +
"This action cannot be undone!"
);
if (!confirmed) {
return;
}
this.getTeachAPI().deleteClub(url, function(err) {
if (err) {
console.log(err);
window.alert("An error occurred! Please try again later.");
}
window.alert("Your club has been removed.");
});
},
handleClubEdit: function(url) {
console.log(url);
Expand Down
5 changes: 3 additions & 2 deletions components/map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ var MarkerPopup = React.createClass({
</button>
&nbsp;
<button className="btn btn-default btn-xs"
data-club-action="delete" data-club-url={this.props.url}>
data-club-action="delete" data-club-url={this.props.url}
data-club-name={this.props.title}>
<span className="glyphicon glyphicon-trash"></span> Remove
</button>
</div>
Expand Down Expand Up @@ -145,7 +146,7 @@ var Map = React.createClass({
}

if (action == 'delete') {
this.props.onDelete(url);
this.props.onDelete(url, targetEl.getAttribute('data-club-name'));
} else if (action == 'edit') {
this.props.onEdit(url);
} else {
Expand Down
15 changes: 14 additions & 1 deletion lib/teach-api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var urlResolve = require('url').resolve;
var _ = require('underscore');
var request = require('superagent');

Expand Down Expand Up @@ -82,7 +83,7 @@ _.extend(TeachAPI.prototype, {
},
request: function(method, path) {
var info = this.getLoginInfo();
var req = request(method, this.baseURL + path);
var req = request(method, urlResolve(this.baseURL, path));

if (info && info.token) {
req.set('Authorization', 'Token ' + info.token);
Expand Down Expand Up @@ -118,6 +119,18 @@ _.extend(TeachAPI.prototype, {
this.updateClubs();
callback(null, res.body);
}.bind(this));
},
deleteClub: function(url, callback) {
callback = callback || function () {};
return this.request('delete', url)
.accept('json')
.end(function(err, res) {
if (err) {
return callback(err);
}
this.updateClubs();
callback(null);
}.bind(this));
}
});

Expand Down
33 changes: 33 additions & 0 deletions test/browser/teach-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,39 @@ describe('TeachAPI', function() {
});
});

describe('deleteClub()', function() {
var api;

beforeEach(function() {
api = new TeachAPI({storage: storage});
});

it('accesses the given URL', function() {
api.deleteClub('http://myserver/clubs/1');
requests.length.should.equal(1);
requests[0].method.should.eql('delete');
requests[0].url.should.eql('http://myserver/clubs/1');
});

it('returns no error on success', function(done) {
api.updateClubs = sinon.spy();
api.deleteClub('http://foo', function(err) {
should(err).equal(null);
done();
});
requests[0].respond(204);
api.updateClubs.callCount.should.equal(1);
});

it('returns an error on failure', function(done) {
api.deleteClub('http://foo', function(err, data) {
err.message.should.eql("Internal Server Error");
done();
});
requests[0].respond(500);
});
});

describe('startLogin()', function() {
var personaCb;

Expand Down

0 comments on commit e93f9be

Please sign in to comment.