Skip to content

Commit 1762f70

Browse files
authored
Merge pull request #2809 from Vizzuality/disposable/mygfw-checkauth
Release: My GFW - Check auth
2 parents 6bd5b68 + fa54d8c commit 1762f70

9 files changed

+256
-166
lines changed

app/assets/javascripts/connect/views/StoriesListItemView.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ define([
22
'backbone',
33
'handlebars',
44
'moment',
5+
'mps',
56
'connect/views/ListItemDeleteConfirmView',
67
'text!connect/templates/storiesListItem.handlebars'
78
], function(
89
Backbone,
910
Handlebars,
1011
moment,
12+
mps,
1113
ListItemDeleteConfirmView,
1214
tpl
1315
) {
@@ -25,6 +27,7 @@ define([
2527

2628
initialize: function(options) {
2729
this.story = options.story;
30+
this.user = options.user;
2831

2932
this.render();
3033
},
@@ -45,13 +48,19 @@ define([
4548
confirmDestroy: function(event) {
4649
event.preventDefault();
4750

48-
this.story.set('from', 'Global Forest Watch');
49-
50-
var confirmView = new ListItemDeleteConfirmView({
51-
model: this.story
52-
});
53-
this.$el.append(confirmView.render().el);
54-
this.listenTo(confirmView, 'confirmed', this.destroy.bind(this));
51+
this.user.checkLogged()
52+
.then(function(response) {
53+
this.story.set('from', 'Global Forest Watch');
54+
55+
var confirmView = new ListItemDeleteConfirmView({
56+
model: this.story
57+
});
58+
this.$el.append(confirmView.render().el);
59+
this.listenTo(confirmView, 'confirmed', this.destroy.bind(this));
60+
}.bind(this))
61+
.catch(function(e) {
62+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
63+
}.bind(this));
5564
},
5665

5766
destroy: function() {

app/assets/javascripts/connect/views/StoriesListView.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ define([
3434
var StoriesListView = Backbone.View.extend({
3535
template: Handlebars.compile(tpl),
3636

37-
initialize: function() {
37+
initialize: function(router, user) {
38+
this.user = user;
3839
this.model = new StoriesListModel();
3940
this.stories = new Stories();
4041
this.listenTo(this.stories, 'sync remove', this.render);
@@ -55,10 +56,11 @@ define([
5556
if (!!paginatedStories.length) {
5657
_.each(paginatedStories, function(story) {
5758
var view = new StoriesListItemView({
58-
story: story
59+
story: story,
60+
user: this.user
5961
});
6062
$tableBody.append(view.el);
61-
});
63+
}.bind(this));
6264
if (this.stories.length/this.model.get('perpage') > 1) {
6365
this.initPaginate();
6466
}

app/assets/javascripts/connect/views/SubscriptionListItemView.js

+130-87
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ define([
4040

4141
initialize: function(options) {
4242
this.subscription = options.subscription;
43+
this.user = options.user;
4344
this.render();
4445
},
4546

@@ -97,44 +98,55 @@ define([
9798
*/
9899
// Name
99100
onClickEditName: function(e) {
100-
var $el = $(e.currentTarget);
101-
if (!$el.hasClass('-editing')) {
102-
var value = this.subscription.get('name');
103-
104-
$el.addClass('-editing').
105-
html('<input />').
106-
find('input').val(value).
107-
focus();
108-
109-
$el.on('keyup.'+this.subscription.get('id'), this.onKeyUpEditName.bind(this));
110-
}
101+
this.user.checkLogged()
102+
.then(function(response) {
103+
var $el = $(e.currentTarget);
104+
if (!$el.hasClass('-editing')) {
105+
var value = this.subscription.get('name');
106+
107+
$el.addClass('-editing').
108+
html('<input />').
109+
find('input').val(value).
110+
focus();
111+
112+
$el.on('keyup.'+this.subscription.get('id'), this.onKeyUpEditName.bind(this));
113+
}
114+
}.bind(this))
115+
.catch(function(e) {
116+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
117+
}.bind(this));
111118
},
112119

113120
onBlurEditName: function(e) {
114-
var $el = $(e.currentTarget);
115-
if ($el.hasClass('-editing')) {
116-
var old_value = this.subscription.get('name'),
117-
new_value = $el.find('input').val();
118-
119-
$el.off('keyup.'+this.subscription.get('id'));
120-
121-
// Check if the value has changed before save it
122-
if (old_value != new_value) {
123-
this.subscription.save('name', new_value, {
124-
patch: true,
125-
wait: true,
126-
silent: true,
127-
success: this.resetName.bind(this),
128-
error: function() {
129-
$el.find('input').
130-
addClass('error').
131-
val(old_value).
132-
focus();
121+
this.user.checkLogged()
122+
.then(function(response) {
123+
var $el = $(e.currentTarget);
124+
if ($el.hasClass('-editing')) {
125+
var old_value = this.subscription.get('name'),
126+
new_value = $el.find('input').val();
127+
128+
$el.off('keyup.'+this.subscription.get('id'));
129+
130+
// Check if the value has changed before save it
131+
if (old_value != new_value) {
132+
this.subscription.save('name', new_value, {
133+
patch: true,
134+
wait: true,
135+
silent: true,
136+
success: this.resetName.bind(this),
137+
error: function() {
138+
$el.find('input').
139+
addClass('error').
140+
val(old_value).
141+
focus();
142+
}
143+
});
133144
}
134-
});
135-
}
136-
137-
}
145+
}
146+
}.bind(this))
147+
.catch(function(e) {
148+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
149+
}.bind(this));
138150
},
139151

140152
onKeyUpEditName: function(e) {
@@ -149,45 +161,57 @@ define([
149161

150162
// Language
151163
onChangeLanguage: function(e) {
152-
var $el = $(e.currentTarget),
153-
old_value = this.subscription.get('language'),
154-
new_value = $el.val();
155-
156-
this.subscription.save('language', new_value, {
157-
wait: true,
158-
silent: true,
159-
patch: true,
160-
success: function() {
161-
$el.val(new_value);
162-
mps.publish('Notification/open', ['notification-my-gfw-subscription-correct']);
163-
},
164-
error: function() {
165-
$el.val(old_value);
166-
mps.publish('Notification/open', ['notification-my-gfw-subscription-incorrect']);
167-
}
168-
});
164+
this.user.checkLogged()
165+
.then(function(response) {
166+
var $el = $(e.currentTarget),
167+
old_value = this.subscription.get('language'),
168+
new_value = $el.val();
169+
170+
this.subscription.save('language', new_value, {
171+
wait: true,
172+
silent: true,
173+
patch: true,
174+
success: function() {
175+
$el.val(new_value);
176+
mps.publish('Notification/open', ['notification-my-gfw-subscription-correct']);
177+
},
178+
error: function() {
179+
$el.val(old_value);
180+
mps.publish('Notification/open', ['notification-my-gfw-subscription-incorrect']);
181+
}
182+
});
183+
}.bind(this))
184+
.catch(function(e) {
185+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
186+
}.bind(this));
169187
},
170188

171189
// Destroy
172190
onClickDestroy: function(e) {
173191
e.preventDefault();
174192

175-
this.subscription.set('from', 'your profile');
193+
this.user.checkLogged()
194+
.then(function(response) {
195+
this.subscription.set('from', 'your profile');
176196

177-
// Create and append confirm view
178-
var confirmView = new ListItemDeleteConfirmView({
179-
model: this.subscription
180-
});
181-
this.$el.append(confirmView.render().el);
182-
183-
// Listen to confirmed param of confirmView
184-
this.listenTo(confirmView, 'confirmed', function() {
185-
this.subscription.destroy({
186-
success: this.remove.bind(this)
187-
});
188-
mps.publish('Notification/open', ['notification-my-gfw-subscription-deleted']);
189-
window.ga('send', 'event', 'User Profile', 'Delete Subscription');
190-
}.bind(this));
197+
// Create and append confirm view
198+
var confirmView = new ListItemDeleteConfirmView({
199+
model: this.subscription
200+
});
201+
this.$el.append(confirmView.render().el);
202+
203+
// Listen to confirmed param of confirmView
204+
this.listenTo(confirmView, 'confirmed', function() {
205+
this.subscription.destroy({
206+
success: this.remove.bind(this)
207+
});
208+
mps.publish('Notification/open', ['notification-my-gfw-subscription-deleted']);
209+
window.ga('send', 'event', 'User Profile', 'Delete Subscription');
210+
}.bind(this));
211+
}.bind(this))
212+
.catch(function(e) {
213+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
214+
}.bind(this));
191215
},
192216

193217
// View on map
@@ -197,40 +221,59 @@ define([
197221

198222
// Datasets
199223
onClickDataset: function() {
200-
var confirmView = new ListItemDatasetsConfirmView({
201-
model: this.subscription
202-
});
224+
this.user.checkLogged()
225+
.then(function(response) {
226+
var confirmView = new ListItemDatasetsConfirmView({
227+
model: this.subscription
228+
});
203229

204-
this.$el.append(confirmView.render().el);
230+
this.$el.append(confirmView.render().el);
205231

206-
// Listen to confirmed param of confirmView
207-
this.listenTo(confirmView, 'confirmed', function(datasets) {
208-
this.saveDatasets(datasets);
209-
}.bind(this));
232+
// Listen to confirmed param of confirmView
233+
this.listenTo(confirmView, 'confirmed', function(datasets) {
234+
this.saveDatasets(datasets);
235+
}.bind(this));
236+
}.bind(this))
237+
.catch(function(e) {
238+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
239+
}.bind(this));
210240
},
211241

212242
onClickDatasetRemove: function(e) {
213243
e && e.preventDefault();
214-
var datasets = _.without(this.subscription.get('datasets'),$(e.currentTarget).data('dataset'));
215-
this.saveDatasets(datasets);
244+
245+
this.user.checkLogged()
246+
.then(function(response) {
247+
var datasets = _.without(this.subscription.get('datasets'),$(e.currentTarget).data('dataset'));
248+
this.saveDatasets(datasets);
249+
}.bind(this))
250+
.catch(function(e) {
251+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
252+
}.bind(this));
216253
},
217254

218255
/**
219256
* HELPERS
220257
* - resetName
221258
*/
222259
saveDatasets: function(datasets) {
223-
this.subscription.save('datasets', datasets, {
224-
wait: true,
225-
silent: true,
226-
patch: true,
227-
success: function() {
228-
mps.publish('Notification/open', ['notification-my-gfw-subscription-correct']);
229-
},
230-
error: function() {
231-
mps.publish('Notification/open', ['notification-my-gfw-subscription-incorrect']);
232-
}
233-
});
260+
this.user.checkLogged()
261+
.then(function(response) {
262+
this.subscription.save('datasets', datasets, {
263+
wait: true,
264+
silent: true,
265+
patch: true,
266+
success: function() {
267+
mps.publish('Notification/open', ['notification-my-gfw-subscription-correct']);
268+
},
269+
error: function() {
270+
mps.publish('Notification/open', ['notification-my-gfw-subscription-incorrect']);
271+
}
272+
});
273+
}.bind(this))
274+
.catch(function(e) {
275+
mps.publish('Notification/open', ['notification-my-gfw-not-logged']);
276+
}.bind(this));
234277
},
235278

236279
resetName: function() {

app/assets/javascripts/connect/views/SubscriptionListView.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ define([
3737
var SubscriptionListView = View.extend({
3838
template: Handlebars.compile(tpl),
3939

40-
initialize: function() {
40+
initialize: function(router, user) {
4141
View.prototype.initialize.apply(this);
4242

43+
this.user = user;
4344
this.model = new SubscriptionListModel();
4445
this.subscriptions = new Subscriptions();
4546
this.listenTo(this.subscriptions, 'sync remove', this.render);
@@ -73,10 +74,11 @@ define([
7374
if (!!paginatedSubscriptions.length) {
7475
_.each(paginatedSubscriptions, function(subscription) {
7576
var view = new SubscriptionListItemView({
76-
subscription: subscription
77+
subscription: subscription,
78+
user: this.user
7779
});
7880
$tableBody.append(view.el);
79-
});
81+
}.bind(this));
8082
if (this.subscriptions.length/this.model.get('perpage') > 1) {
8183
this.initPaginate();
8284
}

0 commit comments

Comments
 (0)