1
- lbry . emailSettingsForm = function ( formSelector , emailState , userAuthToken ) {
1
+ lbry . emailSettingsForm = function ( formSelector , tags , userAuthToken ) {
2
2
var
3
3
form = $ ( formSelector ) ,
4
- state = JSON . parse ( emailState ) ,
5
- emails = state . emails ,
6
- tags = state . tags ,
7
4
emailSection = form . find ( '.email-section' ) ,
8
5
tagSection = form . find ( '.tag-section' ) ,
9
- emailTable = emailSection . find ( 'table' ) ,
10
- tagTable = tagSection . find ( 'table' ) ,
11
6
hasError = false ,
12
7
isEmailSubmitPending = false ,
13
8
tagMap = new Map ( ) ,
14
9
isTagSubmitPending = false ;
15
10
16
-
17
- $ . each ( emails , function ( email , enabled = false ) {
18
- //console.log('email: ',email, ' enabled: ',enabled);
19
- $labelCell = $ ( '<td style="padding: 5px 10px 5px 5px;" ><label>' + email + '</label></td>' ) ;
20
- var checked = enabled ? 'checked' :''
21
- $checkbox = $ (
22
- '<section class="slider-checkbox">' +
23
- '<input id="' + email + '" type="checkbox" ' + checked + '>' +
24
- '<label class="label"></label>' +
25
- '</section>'
26
- ) ;
27
- $checkBoxCell = $ ( '<td style="padding: 5px 10px 5px 5px;">' + $checkbox [ 0 ] . outerHTML + '</td>' ) ;
28
- $rowEmail = $ ( '<tr>' + $labelCell [ 0 ] . outerHTML + $checkBoxCell [ 0 ] . outerHTML + '</tr>' ) ;
29
- emailTable . append ( $rowEmail )
30
- } ) ;
31
11
$ . each ( tags , function ( tag , enabled ) {
32
12
tagMap [ tag ] = enabled ;
33
- //console.log('tagName: ',tag,' enabled: ',enabled)
34
- $labelCell = $ ( '<td style="padding: 5px 10px 5px 5px;"><label>' + tag + '</label></td>' ) ;
35
- var checked = enabled ? 'checked' :''
36
- $checkbox = $ (
37
- '<section class="slider-checkbox">' +
38
- '<input id="' + tag + '" type="checkbox" ' + checked + '>' +
39
- '<label class="label"></label>' +
40
- '</section>'
41
- ) ;
42
- $checkBoxCell = $ ( '<td style="padding: 5px 10px 5px 5px;">' + $checkbox [ 0 ] . outerHTML + '</td>' ) ;
43
- $rowTag = $ ( '<tr>' + $labelCell [ 0 ] . outerHTML + $checkBoxCell [ 0 ] . outerHTML + '</tr>' ) ;
44
- tagTable . append ( $rowTag )
45
13
} ) ;
46
14
47
- //cleverness could eliminate some mild DRY violations below
48
15
form . submit ( function ( e ) {
49
- //remove below obv
50
- // return false;
51
16
52
17
e . preventDefault ( ) ;
53
18
54
19
form . find ( '.notice' ) . hide ( ) ;
55
20
hasError = false ;
56
21
isEmailSubmitPending = true ;
57
22
isTagSubmitPending = true ;
23
+
58
24
console . log ( "Run Email Edit" ) ;
59
- //do email edit
60
25
var url = 'https://api.lbry.io/user/email/edit?auth_token=' + userAuthToken
61
26
$ . param ( $ . map ( emailSection . find ( "input" ) , function ( element ) {
62
- console . log ( "email: " , element . id , " is_enabled: " , element . checked ) ;
63
- url = url + "&email=" + element . id + "&enabled=" + element . checked . toString ( ) ;
27
+ console . log ( "email: " , element . value , " is_enabled: " , element . checked ) ;
28
+ url = url + "&email=" + element . value + "&enabled=" + element . checked . toString ( ) ;
64
29
fetch ( url ) . then ( function ( value ) { return value . json ( ) } ) . then ( jsonResponse => {
65
30
isEmailSubmitPending = false ;
66
31
if ( ! jsonResponse . success ) {
@@ -75,22 +40,23 @@ lbry.emailSettingsForm = function (formSelector, emailState, userAuthToken) {
75
40
emailSection . find ( '.notice-error' ) . html ( error ) . show ( ) ;
76
41
} ) ;
77
42
} ) ) ;
78
- console . log ( "Run Tag Edit" ) ;
43
+
79
44
//do tag edit
45
+ console . log ( "Run Tag Edit" ) ;
80
46
var url = 'https://api.lbry.io/user/tag/edit?auth_token=' + userAuthToken
81
- var addTags = new Array ( ) , removeTags = new Array ( ) ;
82
- $ ( '#tag_table tr' ) . each ( function ( ) {
83
- $trow = $ ( this ) ;
84
- $trow . find ( 'input' ) . each ( function ( ) {
85
- var tagName = $ ( this ) [ 0 ] . id
86
- var enabled = $ ( this ) [ 0 ] . checked
87
- if ( enabled && ! tagMap [ $ ( this ) [ 0 ] . id ] ) {
88
- addTags . push ( $ ( this ) [ 0 ] . id )
89
- } else if ( ! enabled && tagMap [ $ ( this ) [ 0 ] . id ] ) {
90
- removeTags . push ( $ ( this ) [ 0 ] . id )
91
- }
92
- } )
47
+ var addTags = new Array ( ) ,
48
+ removeTags = new Array ( ) ;
49
+
50
+ tagSection . find ( 'input' ) . each ( function ( ) {
51
+ var tagName = this . value
52
+ var enabled = this . checked
53
+ if ( enabled && ! tagMap [ tagName ] ) {
54
+ addTags . push ( tagName )
55
+ } else if ( ! enabled && tagMap [ tagName ] ) {
56
+ removeTags . push ( tagName )
57
+ }
93
58
} ) ;
59
+
94
60
var hasChanges = addTags [ 0 ] || removeTags [ 0 ]
95
61
console . log ( "AddTags: " , addTags , "RemoveTags: " , removeTags )
96
62
var addTagsParam = addTags [ 0 ]
@@ -109,6 +75,7 @@ lbry.emailSettingsForm = function (formSelector, emailState, userAuthToken) {
109
75
if ( removeTagsParam && removeTagsParam . length > 0 ) {
110
76
url = url + "&remove=" + removeTagsParam
111
77
}
78
+
112
79
if ( hasChanges ) {
113
80
fetch ( url ) . then ( response => { return response . json ( ) } ) . then ( jsonResponse => {
114
81
isTagSubmitPending = false ;
@@ -127,15 +94,14 @@ lbry.emailSettingsForm = function (formSelector, emailState, userAuthToken) {
127
94
} else {
128
95
isTagSubmitPending = false ;
129
96
}
130
-
131
97
} ) ;
132
98
133
99
form . show ( ) ;
134
100
135
101
function showSuccess ( ) {
136
102
if ( ! isEmailSubmitPending && ! isTagSubmitPending && ! hasError )
137
103
{
138
- form . find ( '.notice-success' ) . show ( ) ;
104
+ form . find ( '.notice-success' ) . show ( ) . get ( 0 ) . scrollIntoView ( ) ;
139
105
}
140
106
}
141
107
}
0 commit comments