Skip to content

Commit 41d7faa

Browse files
committed
final fixes for email pref
1 parent b577781 commit 41d7faa

File tree

1 file changed

+86
-42
lines changed

1 file changed

+86
-42
lines changed

src/shared/containers/NewsletterSignupForMembers.jsx

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ class NewsletterSignupForMembersContainer extends React.Component {
3131

3232
// Get interestIds and interest request object for mailchimp api
3333
// to use in checkSubscription and subscribe function
34-
const { groups } = props;
34+
const { groups, tags } = props;
3535
this.groupsIds = null;
36+
this.tagNames = null;
3637
if (groups !== '') {
3738
this.groupsIds = groups.split(/ *, */);
3839
this.groupsIds[this.groupsIds.length - 1] = this.groupsIds[this.groupsIds.length - 1].replace(/^\s+|\s+$/g, '');
3940
}
41+
if (tags) {
42+
this.tagNames = tags.split(',');
43+
}
4044
this.isSubscribed = false;
4145

4246
this.state = {
@@ -89,13 +93,25 @@ class NewsletterSignupForMembersContainer extends React.Component {
8993
.then((dataResponse) => {
9094
if (dataResponse.status === 'subscribed') {
9195
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 });
96114
}
97-
} else {
98-
this.setState({ signupState: SIGNUP_NEWSLETTER.DEFAULT });
99115
}
100116
} else {
101117
this.setState({ signupState: SIGNUP_NEWSLETTER.DEFAULT });
@@ -107,49 +123,75 @@ class NewsletterSignupForMembersContainer extends React.Component {
107123
const {
108124
listId, user,
109125
} = 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' : ''}`;
124128

125129
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+
}
126141
data.interests = {};
127142
// eslint-disable-next-line array-callback-return
128143
this.groupsIds.map((group) => {
129144
data.interests[group] = true;
130145
});
131-
}
132146

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+
}
153195
}
154196

155197
showSignupConfirmModal() {
@@ -194,6 +236,7 @@ NewsletterSignupForMembersContainer.defaultProps = {
194236
buttonTheme: 'primary-green-md',
195237
title: 'Sign up for the Topcoder Newsletter',
196238
desc: 'Do you want to subscribe to this newsletter?',
239+
tags: null,
197240
};
198241

199242
NewsletterSignupForMembersContainer.propTypes = {
@@ -206,6 +249,7 @@ NewsletterSignupForMembersContainer.propTypes = {
206249
buttonTheme: PT.string,
207250
title: PT.string,
208251
desc: PT.string,
252+
tags: PT.string,
209253
};
210254

211255
function mapStateToProps(state, ownProps) {

0 commit comments

Comments
 (0)