Skip to content

Instantly share code, notes, and snippets.

@benjaminhoffman
Created January 14, 2017 01:46
Show Gist options
  • Save benjaminhoffman/db1ad6f34431522aed4e47fbf944256e to your computer and use it in GitHub Desktop.
Save benjaminhoffman/db1ad6f34431522aed4e47fbf944256e to your computer and use it in GitHub Desktop.
import { createAction } from 'redux-actions'
import request from 'superagent'
import { enabledIntegration } from '../analytics/enable'
export const ENABLE_INTEGRATION_REQUESTING = 'ENABLE_INTEGRATION_REQUESTING'
export const ENABLE_INTEGRATION_ERROR = 'ENABLE_INTEGRATION_ERROR'
export const ENABLE_INTEGRATION_REQUESTED = 'ENABLE_INTEGRATION_REQUESTED'
export const enableIntegrationRequesting = createAction(ENABLE_INTEGRATION_REQUESTING)
export const enableIntegrationError = createAction(ENABLE_INTEGRATION_ERROR)
export const enableIntegrationRequested = createAction(ENABLE_INTEGRATION_REQUESTED)
export const enableIntegration = (slugs, enable, project) => dispatch => {
const { workspaceSlug, projectSlug } = slugs
const { integration, settings } = enable
dispatch(enableIntegrationRequesting())
request
.patch(`/api/workspaces/${workspaceSlug}/projects/${projectSlug}/integrations/${integration.name}`)
.send({ settings, enabled: true })
.set('x-csrf-token', window.segment.csrf)
.end(function (err, res) {
if (err || !res.ok) {
dispatch(enableIntegrationError({ error: 'Unable to enable this integration, please try again later.' }))
} else {
dispatch(enableIntegrationRequested())
const fallback = `/${workspaceSlug}/sources/${projectSlug}/integrations/${integration.slug}`
// delete ".enable" from our session
request
.del('/api/session/enable')
.set('x-csrf-token', window.segment.csrf)
.end(function () {
enabledIntegration(project, enable.integration)
window.location = enable.redirect || fallback
})
}
})
}
import fetch from 'lib/fetch'
export const ENABLE_INTEGRATION_REQUESTING = 'ENABLE_INTEGRATION_REQUESTING'
export const ENABLE_INTEGRATION_ERROR = 'ENABLE_INTEGRATION_ERROR'
export const ENABLE_INTEGRATION_REQUESTED = 'ENABLE_INTEGRATION_REQUESTED'
export const enableIntegration = (slugs, enable, project) => dispatch => {
const { workspaceSlug, projectSlug } = slugs
const { integration, settings } = enable
return {
request: ENABLE_INTEGRATION_REQUESTING,
receive: ENABLE_INTEGRATION_ERROR,
failure: ENABLE_INTEGRATION_ERROR
},
callAPI: function () {
return fetch(`/api/workspaces/${workspaceSlug}/projects/${projectSlug}/integrations/${integration.name}`, {
method: 'PATCH',
body: JSON.stringify({ settings, enabled: true })
}).then(res => {
if (!res.ok) {
throw new Error('Unable to enable this integration, please try again later.')
}
// delete ".enable" from our session
return fetch('/api/session/enable', {
method: 'DELETE'
}).then(res => {
if (!res.ok) {
throw new Error('Unable to enable this integration, please try again later.')
}
enabledIntegration(project, enable.integration)
window.location = enable.redirect || fallback
})
}).catch(err => {
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment