2018-05-09 00:36:04 +00:00
|
|
|
import actions from '../actions'
|
2018-05-08 23:54:40 +00:00
|
|
|
import api from '../modules/api'
|
|
|
|
import regex from '../modules/regex'
|
2018-05-02 03:52:59 +00:00
|
|
|
|
2017-11-26 23:32:00 +00:00
|
|
|
// API Middleware
|
2018-05-08 23:54:40 +00:00
|
|
|
const joyceAPI = store => next => action => {
|
2017-11-26 23:32:00 +00:00
|
|
|
next(action)
|
2017-11-22 03:52:32 +00:00
|
|
|
switch(action.type) {
|
2017-11-26 23:32:00 +00:00
|
|
|
case 'GET_DOCUMENT_LIST':
|
|
|
|
if (action.status === 'request') {
|
2018-05-18 23:37:03 +00:00
|
|
|
api.HTTPGetDocumentList(action.docType).then(response =>
|
2018-05-09 00:36:04 +00:00
|
|
|
store.dispatch(actions.getDocumentList(response))
|
2017-11-26 23:32:00 +00:00
|
|
|
)
|
|
|
|
}
|
2017-11-22 03:52:32 +00:00
|
|
|
break
|
2017-11-26 23:32:00 +00:00
|
|
|
case 'GET_DOCUMENT_TEXT':
|
|
|
|
if (action.status === 'request') {
|
2018-05-08 23:54:40 +00:00
|
|
|
api.HTTPGetDocumentText(action.id, action.docType, action.state).then(response =>
|
2018-05-09 00:36:04 +00:00
|
|
|
store.dispatch(actions.getDocumentText(response))
|
2017-11-26 23:32:00 +00:00
|
|
|
)
|
2018-05-03 00:29:45 +00:00
|
|
|
}
|
2017-11-22 03:52:32 +00:00
|
|
|
break
|
2017-11-26 23:32:00 +00:00
|
|
|
case 'SAVE_DOCUMENT':
|
|
|
|
if (action.status === 'request') {
|
2017-11-29 04:44:15 +00:00
|
|
|
if (action.id) {
|
2018-05-08 23:54:40 +00:00
|
|
|
api.HTTPPostWriteDocument(action.id, action.docType, action.data).then(response =>
|
2019-08-03 02:24:24 +00:00
|
|
|
store.dispatch(actions.saveDocument(response))
|
2017-11-29 04:44:15 +00:00
|
|
|
)} else {
|
2018-05-08 23:54:40 +00:00
|
|
|
api.HTTPPutCreateDocument(action.docType, action.data).then(response =>
|
2018-05-09 00:36:04 +00:00
|
|
|
store.dispatch(actions.saveDocument(response))
|
2018-05-08 23:54:40 +00:00
|
|
|
)
|
2017-11-29 04:44:15 +00:00
|
|
|
}
|
2017-11-26 23:32:00 +00:00
|
|
|
}
|
2017-11-22 03:52:32 +00:00
|
|
|
break
|
2017-11-26 23:32:00 +00:00
|
|
|
case 'DELETE_DOCUMENT':
|
|
|
|
if (action.status === 'request') {
|
2018-05-08 23:54:40 +00:00
|
|
|
api.HTTPDeleteDocument(action.id, action.docType).then(response =>
|
2018-05-09 00:36:04 +00:00
|
|
|
store.dispatch(actions.deleteDocument(response))
|
2017-11-26 23:32:00 +00:00
|
|
|
)
|
|
|
|
}
|
2017-11-22 03:52:32 +00:00
|
|
|
break
|
2018-05-08 23:54:40 +00:00
|
|
|
case 'GET_SEARCH_RESULTS':
|
|
|
|
if (action.status === 'request') {
|
|
|
|
api.HTTPPostSearchResults(action.data).then(response =>
|
2018-05-09 00:36:04 +00:00
|
|
|
store.dispatch(actions.getSearchResults(response))
|
2018-05-08 23:54:40 +00:00
|
|
|
)
|
|
|
|
}
|
2019-08-03 02:24:24 +00:00
|
|
|
break
|
|
|
|
case 'UPLOAD_MEDIA_SUBMIT':
|
|
|
|
api.HTTPGetSignedPost().then(response =>
|
|
|
|
store.dispatch(actions.uploadMediaToS3Request(response, action.data[0])
|
|
|
|
))
|
|
|
|
break
|
|
|
|
case 'UPLOAD_TO_S3_REQUEST':
|
|
|
|
const formData = new FormData()
|
|
|
|
const url = action.signed_post.url
|
|
|
|
formData.append('key', action.signed_post.fields.key)
|
2019-08-10 15:28:32 +00:00
|
|
|
formData.append('AWSAccessKeyId', action.signed_post.fields.AWSAccessKeyId)
|
|
|
|
formData.append('acl', 'public-read')
|
2019-08-03 02:24:24 +00:00
|
|
|
formData.append('policy', action.signed_post.fields.policy)
|
|
|
|
formData.append('signature', action.signed_post.fields.signature)
|
|
|
|
formData.append('file', action.file)
|
|
|
|
api.HTTPPostMedia(url, formData).then(response =>
|
|
|
|
store.dispatch(actions.uploadMediaToS3Response(response))
|
|
|
|
)
|
2017-11-22 03:52:32 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2018-05-08 23:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default joyceAPI
|