Skip to content

Commit

Permalink
refactor(snapshots): simplify snapshot actions
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Feb 22, 2018
1 parent 6015f9d commit 87dc6ab
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions lib/editor/actions/snapshots.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {createAction} from 'redux-actions'

import {clearGtfsContent} from './active'
import {secureFetch} from '../../common/actions'
import {getConfigProperty} from '../../common/util/config'
import {fetchBaseGtfs} from './editor'
import {receiveFeedSource} from '../../manager/actions/feeds'
import {handleJobResponse} from '../../manager/actions/status'
import {downloadS3Key} from '../../manager/actions/versions'

const requestingSnapshots = createAction('REQUESTING_GTFSEDITOR_SNAPSHOTS')
const receiveSnapshots = createAction('RECEIVE_GTFSEDITOR_SNAPSHOTS')
const restoringSnapshot = createAction('RESTORING_SNAPSHOT')
const restoredSnapshot = createAction('RESTORED_SNAPSHOT')
const creatingSnapshot = createAction('CREATING_SNAPSHOT')
const createdSnapshot = createAction('CREATED_SNAPSHOT')
const downloadingSnapshot = createAction('DOWNLOADING_SNAPSHOT')
const loadingFeedVersionForEditing = createAction('LOADING_FEEDVERSION_FOR_EDITING')
const deletingSnapshot = createAction('DELETING_SNAPSHOT')
Expand All @@ -22,27 +17,23 @@ export function fetchSnapshots (feedSource) {
return function (dispatch, getState) {
dispatch(requestingSnapshots(feedSource))
const url = `/api/editor/secure/snapshot?feedId=${feedSource.id}`
return dispatch(secureFetch(url, 'get'))
return dispatch(secureFetch(url))
.then(res => res.json())
.then(snapshots => dispatch(receiveSnapshots({feedSource, snapshots})))
}
}

/**
* Restores a snapshot into the active editor buffer. On the backend, this is
* creating a copy of the snapshotted tables and then setting the feed source's
* active buffer to those new tables.
*/
export function restoreSnapshot (feedSource, snapshot) {
return function (dispatch, getState) {
dispatch(restoringSnapshot({feedSource, snapshot}))
const url = `/api/editor/secure/snapshot/${snapshot.id}/restore?feedId=${feedSource.id}`
return dispatch(secureFetch(url, 'post'))
.then(response => response.json())
.then(feedSource => {
// Feed source has been updated with new editor namespace
dispatch(receiveFeedSource(feedSource))
dispatch(restoredSnapshot(snapshot.name))
dispatch(fetchSnapshots(feedSource))
// Clear old GTFS and fetch new GTFS with updated editor namespace field
dispatch(clearGtfsContent())
dispatch(fetchBaseGtfs({namespace: feedSource.editorNamespace}))
})
.then(res => dispatch(handleJobResponse(res, 'Error restoring snapshot')))
}
}

Expand All @@ -66,17 +57,17 @@ export function downloadSnapshot (feedSource, snapshot) {
export function downloadSnapshotViaCredentials (snapshot, isPublic, prefix) {
return function (dispatch, getState) {
const route = isPublic ? 'public' : 'secure'
const url = `/api/editor/${route}/snapshot/${snapshot.id}/downloadtoken?feedId=${snapshot.feedId}`
dispatch(secureFetch(url))
.then(response => response.json())
.then(credentials => {
if (getConfigProperty('application.data.use_s3_storage')) {
dispatch(downloadS3Key(credentials, `${snapshot.id}.zip`, 'snapshot'))
} else {
// use token to download feed
window.location.assign(`/api/editor/downloadsnapshot/${credentials.id}`)
}
})
const url = `/api/editor/${route}/snapshot/${snapshot.id}/downloadtoken?feedId=${snapshot.feedSourceId}`
return dispatch(secureFetch(url))
.then(response => response.json())
.then(credentials => {
if (getConfigProperty('application.data.use_s3_storage')) {
dispatch(downloadS3Key(credentials, `${snapshot.id}.zip`, 'snapshots'))
} else {
// use token to download feed
window.location.assign(`/api/editor/downloadsnapshot/${credentials.id}`)
}
})
}
}

Expand All @@ -96,10 +87,6 @@ export function createSnapshot (feedSource, name, comment) {
dispatch(creatingSnapshot({feedSource, snapshot}))
return dispatch(secureFetch(url, 'post', snapshot))
.then(res => dispatch(handleJobResponse(res, 'Error creating snapshot')))
// .then(() => {
// dispatch(createdSnapshot(name))
// return dispatch(fetchSnapshots(feedSource))
// })
}
}

Expand Down

0 comments on commit 87dc6ab

Please sign in to comment.