@@ -31,12 +31,16 @@ class NewsletterSignupForMembersContainer extends React.Component {
31
31
32
32
// Get interestIds and interest request object for mailchimp api
33
33
// to use in checkSubscription and subscribe function
34
- const { groups } = props ;
34
+ const { groups, tags } = props ;
35
35
this . groupsIds = null ;
36
+ this . tagNames = null ;
36
37
if ( groups !== '' ) {
37
38
this . groupsIds = groups . split ( / * , * / ) ;
38
39
this . groupsIds [ this . groupsIds . length - 1 ] = this . groupsIds [ this . groupsIds . length - 1 ] . replace ( / ^ \s + | \s + $ / g, '' ) ;
39
40
}
41
+ if ( tags ) {
42
+ this . tagNames = tags . split ( ',' ) ;
43
+ }
40
44
this . isSubscribed = false ;
41
45
42
46
this . state = {
@@ -89,13 +93,25 @@ class NewsletterSignupForMembersContainer extends React.Component {
89
93
. then ( ( dataResponse ) => {
90
94
if ( dataResponse . status === 'subscribed' ) {
91
95
this . isSubscribed = true ;
92
- const subscribedTags = _ . keys ( _ . pickBy ( dataResponse . interests , v => v ) ) ;
93
- if ( subscribedTags . length ) {
94
- if ( _ . intersection ( subscribedTags , this . groupsIds ) . length ) {
95
- this . setState ( { signupState : SIGNUP_NEWSLETTER . HIDDEN } ) ;
96
+ if ( this . groupsIds ) {
97
+ const subscribedGroups = _ . keys ( _ . pickBy ( dataResponse . interests , v => v ) ) ;
98
+ if ( subscribedGroups . length ) {
99
+ if ( _ . intersection ( subscribedGroups , this . groupsIds ) . length ) {
100
+ this . setState ( { signupState : SIGNUP_NEWSLETTER . HIDDEN } ) ;
101
+ }
102
+ } else {
103
+ this . setState ( { signupState : SIGNUP_NEWSLETTER . DEFAULT } ) ;
104
+ }
105
+ }
106
+ if ( ! this . groupsIds && this . tagNames ) {
107
+ const subscribedTags = _ . map ( dataResponse . tags , t => t . name ) ;
108
+ if ( subscribedTags . length ) {
109
+ if ( _ . intersection ( subscribedTags , this . tagNames ) . length ) {
110
+ this . setState ( { signupState : SIGNUP_NEWSLETTER . HIDDEN } ) ;
111
+ }
112
+ } else {
113
+ this . setState ( { signupState : SIGNUP_NEWSLETTER . DEFAULT } ) ;
96
114
}
97
- } else {
98
- this . setState ( { signupState : SIGNUP_NEWSLETTER . DEFAULT } ) ;
99
115
}
100
116
} else {
101
117
this . setState ( { signupState : SIGNUP_NEWSLETTER . DEFAULT } ) ;
@@ -107,49 +123,75 @@ class NewsletterSignupForMembersContainer extends React.Component {
107
123
const {
108
124
listId, user,
109
125
} = this . props ;
110
-
111
- const fetchUrl = `${ PROXY_ENDPOINT } /${ listId } /members/${ this . emailHash } ` ;
112
-
113
- let data = { } ;
114
- if ( ! this . isSubscribed ) {
115
- data = {
116
- email_address : user . email ,
117
- status : 'subscribed' ,
118
- merge_fields : {
119
- FNAME : user . FNAME ,
120
- LNAME : user . LNAME ,
121
- } ,
122
- } ;
123
- }
126
+ const isTagsUpdate = ! ! this . tagNames ;
127
+ const fetchUrl = `${ PROXY_ENDPOINT } /${ listId } /members/${ this . emailHash } ${ isTagsUpdate ? '/tags' : '' } ` ;
124
128
125
129
if ( this . groupsIds ) {
130
+ let data = { } ;
131
+ if ( ! this . isSubscribed ) {
132
+ data = {
133
+ email_address : user . email ,
134
+ status : 'subscribed' ,
135
+ merge_fields : {
136
+ FNAME : user . FNAME ,
137
+ LNAME : user . LNAME ,
138
+ } ,
139
+ } ;
140
+ }
126
141
data . interests = { } ;
127
142
// eslint-disable-next-line array-callback-return
128
143
this . groupsIds . map ( ( group ) => {
129
144
data . interests [ group ] = true ;
130
145
} ) ;
131
- }
132
146
133
- const formData = JSON . stringify ( data ) ;
134
- // use proxy for avoid 'Access-Control-Allow-Origin' bug
135
- await fetch ( fetchUrl , {
136
- method : 'PUT' ,
137
- headers : {
138
- 'Content-Type' : 'application/json' ,
139
- } ,
140
- body : formData ,
141
- } ) . then ( result => result . json ( ) ) . then ( ( dataResponse ) => {
142
- if ( dataResponse . status === 'subscribed' ) {
143
- // regist success
144
- this . setState ( { signupState : SIGNUP_NEWSLETTER . SIGNEDUP } ) ;
145
- } else {
146
- // regist fail
147
- this . setState ( {
148
- signupState : SIGNUP_NEWSLETTER . ERROR ,
149
- message : dataResponse . detail ,
150
- } ) ;
151
- }
152
- } ) ;
147
+ const formData = JSON . stringify ( data ) ;
148
+ // use proxy for avoid 'Access-Control-Allow-Origin' bug
149
+ await fetch ( fetchUrl , {
150
+ method : 'PUT' ,
151
+ headers : {
152
+ 'Content-Type' : 'application/json' ,
153
+ } ,
154
+ body : formData ,
155
+ } ) . then ( result => result . json ( ) ) . then ( ( dataResponse ) => {
156
+ if ( dataResponse . status === 'subscribed' ) {
157
+ // regist success
158
+ this . setState ( { signupState : SIGNUP_NEWSLETTER . SIGNEDUP } ) ;
159
+ } else {
160
+ // regist fail
161
+ this . setState ( {
162
+ signupState : SIGNUP_NEWSLETTER . ERROR ,
163
+ message : dataResponse . detail ,
164
+ } ) ;
165
+ }
166
+ } ) ;
167
+ }
168
+ if ( ! this . groupsIds && this . tagNames ) {
169
+ const formData = JSON . stringify ( {
170
+ tags : this . tagNames . map ( tName => ( {
171
+ name : tName ,
172
+ status : 'active' ,
173
+ } ) ) ,
174
+ } ) ;
175
+ // use proxy for avoid 'Access-Control-Allow-Origin' bug
176
+ await fetch ( fetchUrl , {
177
+ method : 'POST' ,
178
+ headers : {
179
+ 'Content-Type' : 'application/json' ,
180
+ } ,
181
+ body : formData ,
182
+ } ) . then ( result => result . json ( ) ) . then ( ( dataResponse ) => {
183
+ if ( dataResponse . status === 204 ) {
184
+ // regist success
185
+ this . setState ( { signupState : SIGNUP_NEWSLETTER . SIGNEDUP } ) ;
186
+ } else {
187
+ // regist fail
188
+ this . setState ( {
189
+ signupState : SIGNUP_NEWSLETTER . ERROR ,
190
+ message : dataResponse . detail ,
191
+ } ) ;
192
+ }
193
+ } ) ;
194
+ }
153
195
}
154
196
155
197
showSignupConfirmModal ( ) {
@@ -194,6 +236,7 @@ NewsletterSignupForMembersContainer.defaultProps = {
194
236
buttonTheme : 'primary-green-md' ,
195
237
title : 'Sign up for the Topcoder Newsletter' ,
196
238
desc : 'Do you want to subscribe to this newsletter?' ,
239
+ tags : null ,
197
240
} ;
198
241
199
242
NewsletterSignupForMembersContainer . propTypes = {
@@ -206,6 +249,7 @@ NewsletterSignupForMembersContainer.propTypes = {
206
249
buttonTheme : PT . string ,
207
250
title : PT . string ,
208
251
desc : PT . string ,
252
+ tags : PT . string ,
209
253
} ;
210
254
211
255
function mapStateToProps ( state , ownProps ) {
0 commit comments