|
1 | 1 | import fetch from 'isomorphic-fetch'
|
2 | 2 | import qs from 'qs'
|
3 | 3 | import S3 from 'aws-sdk/clients/s3'
|
| 4 | +import {createAction} from 'redux-actions' |
4 | 5 |
|
5 |
| -import { secureFetch } from '../../common/actions' |
6 |
| -import { getConfigProperty } from '../../common/util/config' |
| 6 | +import {secureFetch} from '../../common/actions' |
7 | 7 | import {GTFS_GRAPHQL_PREFIX, SECURE_API_PREFIX} from '../../common/constants'
|
| 8 | +import {getConfigProperty} from '../../common/util/config' |
| 9 | +import {uploadFile} from '../../common/util/upload-file' |
8 | 10 | import fileDownload from '../../common/util/file-download'
|
9 |
| -import { setErrorMessage, startJobMonitor } from './status' |
10 |
| -import { fetchFeedSource } from './feeds' |
| 11 | +import {setErrorMessage, startJobMonitor} from './status' |
| 12 | +import {fetchFeedSource} from './feeds' |
11 | 13 |
|
12 |
| -export function requestingFeedVersions () { |
13 |
| - return { |
14 |
| - type: 'REQUESTING_FEEDVERSIONS' |
15 |
| - } |
16 |
| -} |
| 14 | +export const requestingFeedVersions = createAction('REQUESTING_FEEDVERSIONS') |
17 | 15 |
|
18 | 16 | export function receiveFeedVersions (feedSource, feedVersions) {
|
19 | 17 | return {
|
@@ -135,29 +133,27 @@ export function feedNotModified (feedSource, message) {
|
135 | 133 | export function uploadFeed (feedSource, file) {
|
136 | 134 | return function (dispatch, getState) {
|
137 | 135 | dispatch(uploadingFeed(feedSource, file))
|
138 |
| - const url = `/api/manager/secure/feedversion?feedSourceId=${feedSource.id}&lastModified=${file.lastModified}` |
| 136 | + const url = `${SECURE_API_PREFIX}feedversion?feedSourceId=${feedSource.id}&lastModified=${file.lastModified}` |
139 | 137 |
|
140 |
| - var data = new window.FormData() |
141 |
| - data.append('file', file) |
142 |
| - |
143 |
| - return fetch(url, { |
144 |
| - method: 'post', |
145 |
| - headers: { 'Authorization': 'Bearer ' + getState().user.token }, |
146 |
| - body: data |
147 |
| - }).then(res => { |
148 |
| - if (res.status === 304) { |
149 |
| - dispatch(feedNotModified(feedSource, 'Feed upload cancelled because it matches latest feed version.')) |
150 |
| - } else if (res.status >= 400) { |
151 |
| - dispatch(setErrorMessage('Error uploading feed source')) |
152 |
| - } else { |
153 |
| - dispatch(uploadedFeed(feedSource)) |
154 |
| - dispatch(startJobMonitor()) |
155 |
| - } |
156 |
| - console.log('uploadFeed result', res) |
| 138 | + return uploadFile({file, url, token: getState().user.token}) |
| 139 | + .then(res => { |
| 140 | + // 304 halt thrown by server if uploaded feed matches the hash of the |
| 141 | + // latest version |
| 142 | + if (res.status === 304) { |
| 143 | + // Do not start job monitor if feed matches latest version. Display the |
| 144 | + // status modal with a message about the cancelled upload. |
| 145 | + dispatch(feedNotModified(feedSource, 'Feed upload cancelled because it matches latest feed version.')) |
| 146 | + } else if (res.status >= 400) { |
| 147 | + dispatch(setErrorMessage('Error uploading feed source')) |
| 148 | + } else { |
| 149 | + dispatch(uploadedFeed(feedSource)) |
| 150 | + dispatch(startJobMonitor()) |
| 151 | + } |
| 152 | + console.log('uploadFeed result', res) |
157 | 153 |
|
158 |
| - // fetch feed source with versions |
159 |
| - return dispatch(fetchFeedSource(feedSource.id, true)) |
160 |
| - }) |
| 154 | + // fetch feed source with versions |
| 155 | + return dispatch(fetchFeedSource(feedSource.id, true)) |
| 156 | + }) |
161 | 157 | }
|
162 | 158 | }
|
163 | 159 |
|
|
0 commit comments