diff --git a/src/components/Project/index.js b/src/components/Project/index.js index 357dd92e68ceb54c4092de3a18ed46c86fb63ca3..3b8f00ca46de34484be7fb0076d76e1f6af4bad8 100644 --- a/src/components/Project/index.js +++ b/src/components/Project/index.js @@ -130,6 +130,12 @@ export default class Project extends Component { fileReader.readAsDataURL(file); } + // deleteSlot makes calls to delete the slot from projectservice and documentservice + deleteSlot = (doc) => { + this.props.deleteDocumentSlot(doc.key, this.state.projectId); + this.props.deleteDocument(doc.id); + } + renderUploadButton = (slot) => { let disabled = false; let img = ( @@ -173,6 +179,13 @@ export default class Project extends Component { docs = documentList.map(item => (
+ {/* Button to delete a document from projectservice and documentservice */} +
)); } @@ -238,4 +251,6 @@ Project.propTypes = { getDocuments: PropTypes.func, uploadDocument: PropTypes.func, getFolderUrl: PropTypes.func, + deleteDocumentSlot: PropTypes.func, + deleteDocument: PropTypes.func, }; diff --git a/src/containers/Building/index.js b/src/containers/Building/index.js index 4d3c14e362173cb775773966ecea5f481880def1..c8b0411d8a1323ed6fea75edc8a796b8dce5ef04 100644 --- a/src/containers/Building/index.js +++ b/src/containers/Building/index.js @@ -6,7 +6,7 @@ import { completeOverviewPropTypes, completeProjectPropTypes } from './propTypes import { loadBuildingDetail, loadProjects } from './actions'; import documentsPropType from '../Documents/propTypes'; -import { loadDocuments, uploadDocument, loadFolderUrl } from '../Documents/actions'; +import { loadDocuments, uploadDocument, loadFolderUrl, deleteDocumentSlot, deleteDocument } from '../Documents/actions'; import SideBarDetail from '../../components/SideBarDetail'; import ErrorAlert from '../../components/ErrorAlert'; @@ -36,6 +36,8 @@ class Building extends Component { getDocuments: this.props.loadDocuments, uploadDocument: this.props.uploadDocument, getFolderUrl: this.props.loadFolderUrl, + deleteDocumentSlot: this.props.deleteDocumentSlot, + deleteDocument: this.props.deleteDocument, }); } @@ -65,6 +67,8 @@ Building.propTypes = { loadDocuments: PropTypes.func, uploadDocument: PropTypes.func, loadFolderUrl: PropTypes.func, + deleteDocumentSlot: PropTypes.func, + deleteDocument: PropTypes.func, }; function mapDispatchToProps(dispatch) { @@ -74,6 +78,8 @@ function mapDispatchToProps(dispatch) { loadDocuments, uploadDocument, loadFolderUrl, + deleteDocumentSlot, + deleteDocument, }, dispatch); } diff --git a/src/containers/Documents/actions.js b/src/containers/Documents/actions.js index b7eb9320cf109ec68a424674739b64aa7c0c5612..52a86b1c4736720cc8aad08709ef4f684bfb5440 100644 --- a/src/containers/Documents/actions.js +++ b/src/containers/Documents/actions.js @@ -8,6 +8,12 @@ import { LOAD_FOLDER_URL, LOAD_FOLDER_URL_SUCCESS, POST_TO_SERVICE, + DELETE_DOCUMENT_SLOT, + DOCUMENT_SLOT_DELETE_SUCCESS, + DOCUMENT_SLOT_DELETE_ERROR, + DELETE_DOCUMENT, + DOCUMENT_DELETE_SUCCESS, + DOCUMENT_DELETE_ERROR, } from './constants'; export function loadDocuments(documentPaths, documentKeys, fileKey) { @@ -87,3 +93,46 @@ export function folderUrlLoaded(url) { payload: url, }; } + +export function deleteDocumentSlot(documentKey, projectId) { + return { + type: DELETE_DOCUMENT_SLOT, + documentKey, + projectId, + }; +} + +export function documentSlotDeleteSuccess(res, documentKey) { + return { + type: DOCUMENT_SLOT_DELETE_SUCCESS, + res, + documentKey, + }; +} + +export function documentSlotDeleteError(errorMsg) { + return { + type: DOCUMENT_SLOT_DELETE_ERROR, + errorMsg, + }; +} + +export function deleteDocument(documentId) { + return { + type: DELETE_DOCUMENT, + documentId, + }; +} + +export function documentDeleteSuccess() { + return { + type: DOCUMENT_DELETE_SUCCESS, + }; +} + +export function documentDeleteError(errorMsg) { + return { + type: DOCUMENT_DELETE_ERROR, + errorMsg, + }; +} diff --git a/src/containers/Documents/constants.js b/src/containers/Documents/constants.js index 4efbcad50d9c42a88c8977eec34c3f782565e6da..430a6a12b2b5f6780360aafe00f7f6c9e94ab1e7 100644 --- a/src/containers/Documents/constants.js +++ b/src/containers/Documents/constants.js @@ -7,3 +7,9 @@ export const UPLOAD_DOCUMENT_ERROR = 'UPLOAD_DOCUMENT_ERROR'; export const LOAD_FOLDER_URL = 'LOAD_FOLDER_URL'; export const LOAD_FOLDER_URL_SUCCESS = 'LOAD_FOLDER_URL_SUCCESS'; export const POST_TO_SERVICE = 'POST_TO_SERVICE'; +export const DELETE_DOCUMENT_SLOT = 'DELETE_DOCUMENT_SLOT'; +export const DOCUMENT_SLOT_DELETE_SUCCESS = 'DOCUMENT_SLOT_DELETE_SUCCESS'; +export const DOCUMENT_SLOT_DELETE_ERROR = 'DOCUMENT_SLOT_DELETE_ERROR'; +export const DELETE_DOCUMENT = 'DELETE_DOCUMENT'; +export const DOCUMENT_DELETE_SUCCESS = 'DOCUMENT_DELETE_SUCCESS'; +export const DOCUMENT_DELETE_ERROR = 'DOCUMENT_DELETE_ERROR'; diff --git a/src/containers/Documents/reducer.js b/src/containers/Documents/reducer.js index 4f35c347e87b56025c94bf4d25ead5fe4940791c..1b15a636ec07e2f7b920531292c1ecd89e0af4c9 100644 --- a/src/containers/Documents/reducer.js +++ b/src/containers/Documents/reducer.js @@ -7,6 +7,12 @@ import { UPLOAD_DOCUMENT_ERROR, LOAD_FOLDER_URL, LOAD_FOLDER_URL_SUCCESS, + DELETE_DOCUMENT_SLOT, + DOCUMENT_SLOT_DELETE_SUCCESS, + DOCUMENT_SLOT_DELETE_ERROR, + DELETE_DOCUMENT, + DOCUMENT_DELETE_SUCCESS, + DOCUMENT_DELETE_ERROR, } from './constants'; import { @@ -19,6 +25,9 @@ const initState = { uploading: false, uploadError: false, documentUrl: '#', + deletingDocumentSlot: false, + deletingDocument: false, + deleteError: null, files: { building: [], // Root Directory project: [], @@ -27,6 +36,9 @@ const initState = { }, }; +let indexOfProjectDocumentSlot = -1; +let currentState = []; + export default function (state = initState, action) { switch (action.type) { case LOAD_DOCUMENTS: @@ -95,6 +107,56 @@ export default function (state = initState, action) { case LOAD_BUILDING_DETAIL: return initState; + case DELETE_DOCUMENT_SLOT: + return { + ...state, + deletingDocumentSlot: true, + }; + + case DOCUMENT_SLOT_DELETE_SUCCESS: + currentState = [...state.files.project]; + indexOfProjectDocumentSlot = currentState.reduce((acc, item) => { + if (item.key === action.documentKey) { + return state.files.project.indexOf(item); + } + return acc; + }, -1); + currentState.splice(indexOfProjectDocumentSlot, 1); + return { + ...state, + files: { + ...state.files, + project: currentState, + deletingDocumentSlot: false, + }, + }; + + case DOCUMENT_SLOT_DELETE_ERROR: + return { + ...state, + deletingDocumentSlot: false, + deleteError: action.errorMsg, + }; + + case DELETE_DOCUMENT: + return { + ...state, + deletingDocument: true, + }; + + case DOCUMENT_DELETE_SUCCESS: + return { + ...state, + deletingDocument: false, + }; + + case DOCUMENT_DELETE_ERROR: + return { + ...state, + deletingDocument: false, + deleteError: action.errorMsg, + }; + default: return state; } diff --git a/src/containers/Documents/sagas.js b/src/containers/Documents/sagas.js index 67b2bc29c25458a81d1499a85081877a9752a8a0..4ac56865ebd245ced2d8de38dbf26f535ed18e56 100644 --- a/src/containers/Documents/sagas.js +++ b/src/containers/Documents/sagas.js @@ -1,12 +1,14 @@ import { call, put, takeLatest } from 'redux-saga/effects'; import request from '../../utils/request'; -import { getHeaders, documentURL } from '../../utils/restServices'; +import { getHeaders, documentURL, projectDocumentURL } from '../../utils/restServices'; import { LOAD_DOCUMENTS, UPLOAD_DOCUMENT, LOAD_FOLDER_URL, POST_TO_SERVICE, + DELETE_DOCUMENT_SLOT, + DELETE_DOCUMENT, } from './constants'; import { @@ -16,6 +18,10 @@ import { documentUploadError, folderUrlLoaded, postToService, + documentSlotDeleteSuccess, + documentSlotDeleteError, + documentDeleteSuccess, + documentDeleteError, } from './actions'; function* getDocuments(action) { @@ -90,6 +96,53 @@ function* uploadDocument(action) { } } +function* deleteDocumentSlot(action) { + const { documentKey, projectId } = action; + let errorMsg = null; + let projectDocumentSlotId = -1; + const res = yield call( + request, + `${projectDocumentURL}?project_id=${projectId}`, { + method: 'GET', + headers: getHeaders(), + }); + + if (!res.err) { + projectDocumentSlotId = res.data.reduce((acc, item) => { + if (item.document_key === action.documentKey) { + return item.id; + } + return acc; + }, -1); + } + + const deleteURL = `${projectDocumentURL}${projectDocumentSlotId}`; + fetch(deleteURL, { + method: 'DELETE', + headers: getHeaders(), + }).catch((err) => { errorMsg = err; }); + if (errorMsg) { + yield put(documentSlotDeleteError(errorMsg)); + } else { + yield put(documentSlotDeleteSuccess(res, documentKey)); + } +} + +function* deleteDocument(action) { + const { documentId } = action; + let errorMsg = null; + const deleteURL = `${documentURL}${documentId}`; + fetch(deleteURL, { + method: 'DELETE', + headers: getHeaders(), + }).catch((err) => { errorMsg = err; }); + if (errorMsg) { + yield put(documentDeleteError(errorMsg)); + } else { + yield put(documentDeleteSuccess()); + } +} + function* getFolderUrl(action) { const { folderPath } = action; @@ -111,4 +164,6 @@ export default function* () { yield takeLatest(UPLOAD_DOCUMENT, uploadDocument); yield takeLatest(POST_TO_SERVICE, postToServiceSaga); yield takeLatest(LOAD_FOLDER_URL, getFolderUrl); + yield takeLatest(DELETE_DOCUMENT_SLOT, deleteDocumentSlot); + yield takeLatest(DELETE_DOCUMENT, deleteDocument); }