2017-11-22 03:52:32 +00:00
|
|
|
import axios from 'axios'
|
|
|
|
|
|
|
|
import {
|
2017-11-26 23:32:00 +00:00
|
|
|
getDocumentList,
|
|
|
|
getDocumentText,
|
|
|
|
deleteDocument,
|
|
|
|
saveDocument,
|
2017-11-27 03:21:05 +00:00
|
|
|
setCurrentChapter,
|
2017-11-26 23:32:00 +00:00
|
|
|
createNewChapter,
|
2017-11-22 03:52:32 +00:00
|
|
|
} from '../actions'
|
|
|
|
|
2017-11-26 23:32:00 +00:00
|
|
|
let apiRoute = '/api/'
|
2017-11-22 03:52:32 +00:00
|
|
|
|
2017-11-26 23:32:00 +00:00
|
|
|
// Axios HTTP Functions
|
|
|
|
const HTTPGetDocumentList = docType =>
|
|
|
|
axios.get(apiRoute + docType).then(res => {
|
|
|
|
return {status: 'success', docType: docType, data: res.data}
|
|
|
|
}).catch(error => {
|
|
|
|
return {status: 'error', docType: docType, data: error}
|
|
|
|
})
|
2017-11-22 03:52:32 +00:00
|
|
|
|
2017-11-26 23:32:00 +00:00
|
|
|
const HTTPGetDocumentText = (id, docType) =>
|
|
|
|
axios.get(apiRoute + docType + '/' + id).then(res => {
|
|
|
|
return {id: id, status: 'success', docType: docType, data: res.data}
|
|
|
|
}).catch(error => {
|
|
|
|
return {id: id, status: 'error', docType: docType, data: error}
|
|
|
|
})
|
2017-11-22 03:52:32 +00:00
|
|
|
|
2017-11-26 23:32:00 +00:00
|
|
|
const HTTPDeleteDocument = (id, docType) =>
|
|
|
|
axios.delete(apiRoute + docType + '/' + id).then(res => {
|
|
|
|
return {id: id, status: 'success', docType: docType, data: res.data}
|
|
|
|
}).catch(error => {
|
|
|
|
return {id: id, status: 'error', docType: docType, data: error}
|
|
|
|
})
|
2017-11-22 03:52:32 +00:00
|
|
|
|
2017-11-26 23:32:00 +00:00
|
|
|
const HTTPPostWriteDocument = (id, docType, data) =>
|
|
|
|
axios.post(apiRoute + docType + '/' + id, data).then(res => {
|
|
|
|
return {id: id, status: 'success', docType: docType, data: res.data}
|
|
|
|
}).catch(error => {
|
|
|
|
return {id: id, status: 'error', docType: docType, data: error}
|
|
|
|
})
|
2017-11-22 03:52:32 +00:00
|
|
|
|
2017-11-26 23:32:00 +00:00
|
|
|
// API Middleware
|
|
|
|
export const joyceAPI = store => next => action => {
|
|
|
|
next(action)
|
2017-11-22 03:52:32 +00:00
|
|
|
switch(action.type) {
|
2017-11-26 23:32:00 +00:00
|
|
|
// API Calls
|
|
|
|
case 'GET_DOCUMENT_LIST':
|
|
|
|
if (action.status === 'request') {
|
|
|
|
HTTPGetDocumentList(action.docType).then(response =>
|
|
|
|
store.dispatch(getDocumentList(response))
|
|
|
|
)
|
|
|
|
}
|
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') {
|
|
|
|
HTTPGetDocumentText(action.id, action.docType).then(response =>
|
|
|
|
store.dispatch(getDocumentText(response))
|
|
|
|
)
|
|
|
|
}
|
2017-11-22 03:52:32 +00:00
|
|
|
break
|
2017-11-26 23:32:00 +00:00
|
|
|
case 'SAVE_DOCUMENT':
|
|
|
|
if (action.status === 'request') {
|
|
|
|
HTTPPostWriteDocument(action.id, action.docType, action.data).then(response =>
|
|
|
|
store.dispatch(saveDocument(response))
|
|
|
|
)
|
|
|
|
} else if (action.status === 'success') {
|
2017-11-27 03:21:05 +00:00
|
|
|
store.dispatch(setCurrentChapter(action.id))
|
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') {
|
|
|
|
HTTPDeleteDocument(action.id, action.docType).then(response =>
|
|
|
|
store.dispatch(deleteDocument(response))
|
|
|
|
)
|
|
|
|
} else if (action.status === 'success') {
|
2017-11-27 03:21:05 +00:00
|
|
|
store.dispatch(setCurrentChapter(action.data[0].number))
|
2017-11-26 23:32:00 +00:00
|
|
|
}
|
2017-11-22 03:52:32 +00:00
|
|
|
break
|
2017-11-27 03:21:05 +00:00
|
|
|
// Chapter Action Middleware
|
|
|
|
case 'SET_CURRENT_CHAPTER':
|
2017-11-26 23:32:00 +00:00
|
|
|
store.dispatch(getDocumentText({id: action.id, docType: 'chapters'}))
|
2017-11-22 03:52:32 +00:00
|
|
|
break
|
2017-11-26 23:32:00 +00:00
|
|
|
case 'SUBMIT_CHAPTER_EDIT':
|
|
|
|
store.dispatch(saveDocument({id: action.document.number, docType: 'chapters', data: action.document}))
|
|
|
|
break
|
|
|
|
case 'DELETE_CURRENT_CHAPTER':
|
|
|
|
store.dispatch(deleteDocument({id: action.id, docType: 'chapters'}))
|
|
|
|
break
|
2017-11-22 03:52:32 +00:00
|
|
|
case 'CREATE_CHAPTER':
|
2017-11-26 23:32:00 +00:00
|
|
|
if (!action.chapterNumber) {
|
|
|
|
const chapterNumber = store.getState().chapters.length + 1
|
|
|
|
store.dispatch(createNewChapter(chapterNumber))
|
2017-11-22 03:52:32 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|