index.js
109 lines
| 1 | import {donorDashboardApi} from '../../../utils'; |
| 2 | import {store} from '../../../store'; |
| 3 | import {setProfile} from '../../../store/actions'; |
| 4 | import {getCleanParentHref} from "../../../components/logout-modal/utils"; |
| 5 | |
| 6 | export const updateProfileWithAPI = async ({ |
| 7 | titlePrefix, |
| 8 | firstName, |
| 9 | lastName, |
| 10 | company, |
| 11 | primaryEmail, |
| 12 | additionalEmails, |
| 13 | primaryAddress, |
| 14 | additionalAddresses, |
| 15 | avatarFile, |
| 16 | isAnonymous, |
| 17 | id, |
| 18 | }) => { |
| 19 | /** |
| 20 | * If a new avatar file is defined, upload it and use the returned |
| 21 | * media ID to be stored as donor meta |
| 22 | */ |
| 23 | const {profile} = store.getState(); |
| 24 | let {avatarId} = profile; |
| 25 | if (avatarFile) { |
| 26 | avatarId = await uploadAvatarWithAPI(avatarFile); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Pass new profile data to the Profile REST endpoint |
| 31 | */ |
| 32 | const {dispatch} = store; |
| 33 | return donorDashboardApi |
| 34 | .post( |
| 35 | 'profile', |
| 36 | { |
| 37 | data: JSON.stringify({ |
| 38 | titlePrefix, |
| 39 | firstName, |
| 40 | lastName, |
| 41 | company, |
| 42 | primaryEmail, |
| 43 | additionalEmails, |
| 44 | primaryAddress, |
| 45 | additionalAddresses, |
| 46 | avatarId, |
| 47 | isAnonymous, |
| 48 | }), |
| 49 | id, |
| 50 | }, |
| 51 | {} |
| 52 | ) |
| 53 | .then((response) => response.data) |
| 54 | .then((responseData) => { |
| 55 | /** |
| 56 | * Once updated, update the store's representation of |
| 57 | * the donor's profile data |
| 58 | */ |
| 59 | dispatch(setProfile(responseData.profile)); |
| 60 | return responseData; |
| 61 | }); |
| 62 | }; |
| 63 | |
| 64 | export const uploadAvatarWithAPI = (file) => { |
| 65 | // Prepare a FormData object with the file to be past to the 'avatar' REST endpoint |
| 66 | const formData = new window.FormData(); |
| 67 | formData.append('file', file); |
| 68 | |
| 69 | // Upload the new file, and return the resolved Promise with new media ID |
| 70 | return donorDashboardApi |
| 71 | .post('avatar', formData) |
| 72 | .then((response) => { |
| 73 | return response.data; |
| 74 | }) |
| 75 | .then((responseData) => responseData.id); |
| 76 | }; |
| 77 | |
| 78 | export const fetchStatesWithAPI = (country) => { |
| 79 | return donorDashboardApi |
| 80 | .post( |
| 81 | 'location', |
| 82 | { |
| 83 | countryCode: country, |
| 84 | }, |
| 85 | {} |
| 86 | ) |
| 87 | .then((response) => response.data) |
| 88 | .then((data) => { |
| 89 | return data.states.map((state) => { |
| 90 | return { |
| 91 | value: state.value, |
| 92 | label: decodeHTMLEntity(state.label), |
| 93 | }; |
| 94 | }); |
| 95 | }); |
| 96 | }; |
| 97 | |
| 98 | export const decodeHTMLEntity = (entity) => { |
| 99 | const div = document.createElement('div'); |
| 100 | div.innerHTML = entity; |
| 101 | return div.innerText; |
| 102 | }; |
| 103 | |
| 104 | export const updatePasswordWithAPI = async (newPassword) => { |
| 105 | return donorDashboardApi |
| 106 | .post('password', {newPassword}) |
| 107 | .then(() => window.parent.location.href = getCleanParentHref()) // After password update the cookie is invalidated, so we need to refresh the page. |
| 108 | }; |
| 109 |