|
| 1 | +/** |
| 2 | + * Actions for the Newsletter preference container. |
| 3 | + */ |
| 4 | + |
| 5 | +/* global fetch */ |
| 6 | +import _ from 'lodash'; |
| 7 | +import { createActions } from 'redux-actions'; |
| 8 | +import { config } from 'topcoder-react-utils'; |
| 9 | + |
| 10 | +const PROXY_ENDPOINT = `${config.URL.COMMUNITY_APP}/api/mailchimp`; |
| 11 | + |
| 12 | +// Fetching member's newsletter preferences init |
| 13 | +function fetchDataInit(email) { |
| 14 | + return email; |
| 15 | +} |
| 16 | + |
| 17 | +// Fetching member's newsletter preferences |
| 18 | +async function fetchDataDone(emailHash, listId = config.NEWSLETTER_SIGNUP.DEFAUL_LIST_ID) { |
| 19 | + /* NOTE: In the real life in most cases you don't want to use fetch() directly |
| 20 | + * in an action. You want to create a service for your calls and use it here. |
| 21 | + * However, in this example, to keep it a bit more compact, we use fetch() |
| 22 | + * directly here. */ |
| 23 | + try { |
| 24 | + let error = false; |
| 25 | + const subs = await fetch(`${PROXY_ENDPOINT}/${listId}/members/${emailHash}`, { |
| 26 | + method: 'GET', |
| 27 | + headers: { |
| 28 | + 'Content-Type': 'application/json', |
| 29 | + }, |
| 30 | + }) |
| 31 | + .then((result) => { |
| 32 | + if (result.status !== 200) error = true; |
| 33 | + return result.json(); |
| 34 | + }); |
| 35 | + |
| 36 | + return { |
| 37 | + email: emailHash, |
| 38 | + preferences: subs.tags, |
| 39 | + error, |
| 40 | + }; |
| 41 | + } catch (error) { |
| 42 | + return { |
| 43 | + email: emailHash, |
| 44 | + error, |
| 45 | + }; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Updates member newsletter subscription |
| 50 | +async function updateSubscriptionsDone( |
| 51 | + emailHash, tagId, status, listId = config.NEWSLETTER_SIGNUP.DEFAUL_LIST_ID, |
| 52 | +) { |
| 53 | + /* NOTE: In the real life in most cases you don't want to use fetch() directly |
| 54 | + * in an action. You want to create a service for your calls and use it here. |
| 55 | + * However, in this example, to keep it a bit more compact, we use fetch() |
| 56 | + * directly here. */ |
| 57 | + try { |
| 58 | + let error = false; |
| 59 | + const fetchUrl = `${PROXY_ENDPOINT}/${listId}/members/${emailHash}/tags`; |
| 60 | + |
| 61 | + const data = { |
| 62 | + tags: [ |
| 63 | + { name: tagId, status: status ? 'active' : 'inactive' }, |
| 64 | + ], |
| 65 | + }; |
| 66 | + |
| 67 | + const formData = JSON.stringify(data); |
| 68 | + // use proxy for avoid 'Access-Control-Allow-Origin' bug |
| 69 | + await fetch(fetchUrl, { |
| 70 | + method: 'POST', |
| 71 | + headers: { |
| 72 | + 'Content-Type': 'application/json', |
| 73 | + }, |
| 74 | + body: formData, |
| 75 | + }) |
| 76 | + .then((result) => { |
| 77 | + if (!result.ok) error = true; |
| 78 | + return result.json(); |
| 79 | + }); |
| 80 | + |
| 81 | + return { |
| 82 | + id: tagId, |
| 83 | + checked: status, |
| 84 | + email: emailHash, |
| 85 | + error, |
| 86 | + }; |
| 87 | + } catch (error) { |
| 88 | + return { |
| 89 | + id: tagId, |
| 90 | + checked: status, |
| 91 | + email: emailHash, |
| 92 | + error, |
| 93 | + }; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export default createActions({ |
| 98 | + NEWSLETTER_PREFERENCES: { |
| 99 | + FETCH_DATA_INIT: fetchDataInit, |
| 100 | + FETCH_DATA_DONE: fetchDataDone, |
| 101 | + UPDATE_TAG_INIT: _.identity, |
| 102 | + UPDATE_TAG_DONE: updateSubscriptionsDone, |
| 103 | + }, |
| 104 | +}); |
0 commit comments