import { CUSTOMER_API, CLICK_TRACKING_URL, PUBLIC_PROFILE_DOMAIN, ORG_PROFILE_DOMAIN } from './ResourceConstants' import { IExtractedLink, IExtractedLinkPatch, IContentOptions } from './contentInterfaces' import { logout } from '../modules/auth/actions' import { Store } from 'react-chrome-redux' import Raven from './sentry' const store = new Store({ portName: 'DRIFT', // communication port name }) const handleErrors = (response: any) => { if (response.status === 403) { store.dispatch(logout()) } if (!response.ok && response.status <= 500) { return response.text().then((text: any) => { try { const newErrorMessage = JSON.stringify({ ...response, ...text }) throw new Error(newErrorMessage) } catch (e) { // catch error if response is not JSON throw new Error(text) } }) } return response } export const getOneShotTokens = (token: string, email: string) => { return fetch(`${CUSTOMER_API}/auth/oneshot`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `bearer ${token}`, }, body: JSON.stringify({ emailAddress: email }), }) .then(handleErrors) .then(r => r.json()) .catch((error) => { Raven.captureException(error) return { oneshotTokens: [] } }) } export const bulkGetClickTrackIds = (token: string, links: IExtractedLink[]) => { return fetch(`${CLICK_TRACKING_URL}/click`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `bearer ${token}`, }, body: JSON.stringify(links), }) .then(handleErrors) .then((r: any) => r.json()) .catch((error) => { Raven.captureException(error) return [] }) } export const patchClickTrackIds = (token: string, clickId: string, body: IExtractedLinkPatch) => { return fetch(`${CLICK_TRACKING_URL}/click/data/${clickId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', Authorization: `bearer ${token}`, }, body: JSON.stringify(body), }) .then(handleErrors) .catch((error) => { Raven.captureException(error) }) } export const getMeetingBaseUrlFromContentOptions = (contentOptions: IContentOptions, slug?: string) => { const { handle, globalHandle, orgHandle, profileVisibility } = contentOptions if (profileVisibility !== 'PRIVATE') { if (globalHandle) { return slug ? `https://${PUBLIC_PROFILE_DOMAIN}/${globalHandle}/meeting/${slug}` : `https://${PUBLIC_PROFILE_DOMAIN}/${globalHandle}/meeting/` } if (orgHandle && handle) { return slug ? `https://${orgHandle}.${ORG_PROFILE_DOMAIN}/${handle}/meeting/${slug}` : `https://${orgHandle}.${ORG_PROFILE_DOMAIN}/${handle}/meeting/` } } return '' } export const getOnboardingHyperlinkFromContentOptions = (contentOptions: IContentOptions) => { const { orgUrl, handle, globalHandle, orgHandle, profileVisibility } = contentOptions if (profileVisibility !== 'PRIVATE') { if (globalHandle) { return `https://drift.me/${globalHandle}` } if (orgHandle && handle) { return `https://${orgHandle}.drift.com/${handle}` } } if (orgUrl) { return orgUrl } return 'https://help.drift.com/integrations/using-the-drift-email-extension' } export const getLatestCalendarAvailability = (token: string, userId: number, meetingType?: string) => { const url = !!meetingType ? `${CUSTOMER_API}/scheduling/${userId}/availability?meetingType=${meetingType}` : `${CUSTOMER_API}/scheduling/${userId}/availability` return fetch(url, { method: 'GET', headers: { Authorization: `bearer ${token}`, }, }) .then(handleErrors) .then((r: any) => r.json()) .catch((error) => { Raven.captureException(error) return [] }) } export const createErrorMessage = (warningIconUrl: string) => { return `
` }