Skip to content

Commit 7212bf2

Browse files
committed
initial chnages
1 parent bdfe08c commit 7212bf2

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/shared/components/Contentful/AppComponent/index.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import React from 'react';
1010
import { errors } from 'topcoder-react-lib';
1111
import Leaderboard from 'containers/tco/Leaderboard';
1212
import RecruitCRMJobs from 'containers/Gigs/RecruitCRMJobs';
13+
import EmailSubscribeForm from 'containers/EmailSubscribeForm';
14+
1315

1416
const { fireErrorMessage } = errors;
1517

@@ -34,6 +36,9 @@ export function AppComponentSwitch(appComponent) {
3436
if (appComponent.fields.type === 'RecruitCRM-Jobs') {
3537
return <RecruitCRMJobs {...appComponent.fields.props} key={appComponent.sys.id} />;
3638
}
39+
if (appComponent.fields.type === 'EmailSubscribeForm') {
40+
return <EmailSubscribeForm {...appComponent.fields.props} key={appComponent.sys.id} />;
41+
}
3742
fireErrorMessage('Unsupported app component type from contentful', '');
3843
return null;
3944
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Genetic subscribe for MailChimp tags component
3+
*/
4+
import React from 'react';
5+
import PT from 'prop-types';
6+
import { isValidEmail } from 'utils/tc';
7+
import defaulTheme from './style.scss';
8+
9+
/* Holds the base URL of Community App endpoints that proxy HTTP request to
10+
* mailchimp APIs. */
11+
const PROXY_ENDPOINT = '/api/mailchimp';
12+
13+
class SubscribeMailChimpTagContainer extends React.Component {
14+
constructor(props) {
15+
super(props);
16+
this.state = {
17+
error: '',
18+
subsribed: false,
19+
disabled: true,
20+
inputVal: '',
21+
};
22+
this.onSubscribeClick = this.onSubscribeClick.bind(this);
23+
this.onInputChange = this.onInputChange.bind(this);
24+
}
25+
26+
onSubscribeClick() {
27+
const { inputVal } = this.state;
28+
const { listId, tags } = this.props;
29+
const fetchUrl = `${PROXY_ENDPOINT}/${listId}/members/${inputVal}/tags`;
30+
const data = {
31+
email_address: inputVal,
32+
status: 'subscribed',
33+
tags: tags.map(t => ({ name: t, status: 'active' })),
34+
};
35+
const formData = JSON.stringify(data);
36+
fetch(fetchUrl, {
37+
method: 'POST',
38+
headers: {
39+
'Content-Type': 'application/json',
40+
},
41+
body: formData,
42+
}).then(result => result.json()).then((dataResponse) => {
43+
if (dataResponse.status === 204) {
44+
// regist success
45+
return this.setState({
46+
subsribed: true,
47+
error: '',
48+
});
49+
}
50+
if (dataResponse.status === 404) {
51+
// new email register it for list and add tags
52+
return fetch(`${PROXY_ENDPOINT}/${listId}/members`, {
53+
method: 'POST',
54+
headers: {
55+
'Content-Type': 'application/json',
56+
},
57+
body: JSON.stringify({
58+
email_address: inputVal,
59+
status: 'subscribed',
60+
tags,
61+
}),
62+
})
63+
.then(result => result.json()).then(() => {
64+
this.setState({
65+
subsribed: true,
66+
error: '',
67+
});
68+
});
69+
}
70+
return this.setState({
71+
subsribed: false,
72+
error: `Error ${dataResponse.status} when assign to tags`,
73+
});
74+
})
75+
.catch((e) => {
76+
this.setState({
77+
subsribed: false,
78+
error: e.message,
79+
});
80+
});
81+
}
82+
83+
onInputChange(event) {
84+
this.setState({
85+
inputVal: event.target.value,
86+
disabled: !isValidEmail(event.target.value),
87+
});
88+
}
89+
90+
render() {
91+
const {
92+
disabled, inputVal, error, subsribed,
93+
} = this.state;
94+
return (
95+
<div className={defaulTheme.wrapper}>
96+
<input type="email" placeholder="Email" value={inputVal} onChange={this.onInputChange} />
97+
<button type="button" onClick={this.onSubscribeClick} disabled={disabled} className={defaulTheme.button}>Subscribe</button>
98+
</div>
99+
);
100+
}
101+
}
102+
103+
SubscribeMailChimpTagContainer.propTypes = {
104+
listId: PT.string.isRequired,
105+
tags: PT.arrayOf(PT.string).isRequired,
106+
};
107+
108+
export default SubscribeMailChimpTagContainer;

src/shared/containers/EmailSubscribeForm/style.scss

Whitespace-only changes.

0 commit comments

Comments
 (0)