|
| 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; |
0 commit comments