1
1
import Controller from '@ember/controller' ;
2
- import { sort , filterBy , notEmpty } from '@ember/object/computed' ;
2
+ import { alias , sort , filterBy , notEmpty } from '@ember/object/computed' ;
3
3
import { inject as service } from '@ember/service' ;
4
+ import ajax from 'ember-fetch/ajax' ;
4
5
5
6
export default Controller . extend ( {
6
7
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
@@ -12,10 +13,50 @@ export default Controller.extend({
12
13
13
14
isResetting : false ,
14
15
16
+ ownedCrates : alias ( 'model.ownedCrates' ) ,
17
+
15
18
newTokens : filterBy ( 'model.api_tokens' , 'isNew' , true ) ,
16
19
disableCreate : notEmpty ( 'newTokens' ) ,
17
20
21
+ emailNotificationsError : false ,
22
+ emailNotificationsSuccess : false ,
23
+
24
+ setAllEmailNotifications ( value ) {
25
+ this . get ( 'ownedCrates' ) . forEach ( c => {
26
+ c . set ( 'email_notifications' , value ) ;
27
+ } ) ;
28
+ } ,
29
+
18
30
actions : {
31
+ async saveEmailNotifications ( ) {
32
+ try {
33
+ await ajax ( `/api/v1/me/email_notifications` , {
34
+ method : 'PUT' ,
35
+ body : JSON . stringify (
36
+ this . get ( 'ownedCrates' ) . map ( c => ( {
37
+ id : parseInt ( c . id , 10 ) ,
38
+ email_notifications : c . email_notifications ,
39
+ } ) ) ,
40
+ ) ,
41
+ } ) ;
42
+ this . setProperties ( {
43
+ emailNotificationsError : false ,
44
+ emailNotificationsSuccess : true ,
45
+ } ) ;
46
+ } catch ( err ) {
47
+ console . error ( err ) ;
48
+ this . setProperties ( {
49
+ emailNotificationsError : true ,
50
+ emailNotificationsSuccess : false ,
51
+ } ) ;
52
+ }
53
+ } ,
54
+ emailNotificationsSelectAll ( ) {
55
+ this . setAllEmailNotifications ( true ) ;
56
+ } ,
57
+ emailNotificationsSelectNone ( ) {
58
+ this . setAllEmailNotifications ( false ) ;
59
+ } ,
19
60
startNewToken ( ) {
20
61
this . store . createRecord ( 'api-token' , {
21
62
created_at : new Date ( Date . now ( ) + 2000 ) ,
0 commit comments