-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.js
90 lines (78 loc) · 2.28 KB
/
manage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Ember from 'ember';
import extractError from 'teamplaybook-ember/lib/extract-error';
import ajax from 'ic-ajax';
import ENV from 'teamplaybook-ember/config/environment';
export default Ember.Controller.extend({
showError: false,
errorMessage: null,
cardToken: null,
currentPlan: Ember.computed('model.planSlug', function (){
var plans = this.store.all('plan');
return plans.findBy('slug', this.get('model.planSlug'));
}),
currentPlanIsPaid: Ember.computed.alias('currentPlan.isPaid'),
plans: Ember.computed(function(){
return this.store.find('plan');
}),
actions: {
delete: function() {
var team = this.get('model');
var controller = this;
if (window.confirm('Are you sure you wish to disband this team?') === true) {
team.deleteRecord();
team.save().then(function () {
controller.setProperties({
showError: false,
errorMessage: null
});
}).catch(function(response) {
controller.setProperties({
showError: true,
errorMessage: extractError(response)
});
});
}
},
changePlan: function(){
var controller = this;
if(this.get('currentPlanIsPaid')){
this.createStripeToken().then(function(){
controller.requestPlanChange();
});
}else{
this.requestPlanChange();
}
}
},
_buildURL: function(path) {
var apiUrl = this.get('urlInfo.apiUrl');
return apiUrl + '/' + path;
},
requestPlanChange: function(){
var team = this.get('model');
ajax({
type: 'POST',
url: this._buildURL('team/change_plan'),
data: {
plan_slug: team.get('planSlug'),
card_token: this.get('cardToken')
}
}).then(function(){
alert("You have changed your plan");
}, function(response){
alert(extractError(response));
});
},
createStripeToken: function(){
var Stripe = window.Stripe;
Stripe.setPublishableKey(ENV.STRIPE_PUBLIC_KEY);
var controller = this;
var $form = Ember.$('#payment-form');
return new Ember.RSVP.Promise(function(resolve) {
Stripe.card.createToken($form, function(status, response) {
controller.set('cardToken', response.id);
resolve();
});
});
}
});