Skip to content

Commit

Permalink
feat(entities): adds delete / restore for document
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarseguerra committed Sep 23, 2024
1 parent 3a1e06d commit a694a3e
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 287 deletions.
47 changes: 47 additions & 0 deletions packages/web-app/src/actions/Document/DeleteDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import fetch from 'isomorphic-fetch';
import { deleteDocumentUrl } from '../../conf/apiRoutes';
import { checkAndGetStatus } from '../utils';

export const DELETE_DOCUMENT = 'DELETE_DOCUMENT';
export const DELETE_DOCUMENT_SUCCESS = 'DELETE_DOCUMENT_SUCCESS';
export const DELETE_DOCUMENT_PERMANENT_SUCCESS =
'DELETE_DOCUMENT_PERMANENT_SUCCESS';
export const DELETE_DOCUMENT_FAILURE = 'DELETE_DOCUMENT_FAILURE';

const deleteDocumentAction = () => ({
type: DELETE_DOCUMENT
});

const deleteDocumentSuccess = (data, isPermanent) => ({
type: isPermanent
? DELETE_DOCUMENT_PERMANENT_SUCCESS
: DELETE_DOCUMENT_SUCCESS,
data
});

const deleteDocumentFailure = error => ({
type: DELETE_DOCUMENT_FAILURE,
error
});

export const deleteDocument =
({ id, entityId, isPermanent }) =>
(dispatch, getState) => {
dispatch(deleteDocumentAction());

const requestOptions = {
method: 'DELETE',
headers: getState().login.authorizationHeader
};

return fetch(
deleteDocumentUrl(id, { entityId, isPermanent }),
requestOptions
)
.then(checkAndGetStatus)
.then(response => response.json())
.then(data => dispatch(deleteDocumentSuccess(data, isPermanent)))
.catch(error => {
dispatch(deleteDocumentFailure(error));
});
};
40 changes: 40 additions & 0 deletions packages/web-app/src/actions/Document/RestoreDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fetch from 'isomorphic-fetch';
import { restoreDocumentUrl } from '../../conf/apiRoutes';
import { checkAndGetStatus } from '../utils';

export const RESTORE_DOCUMENT = 'RESTORE_DOCUMENT';
export const RESTORE_DOCUMENT_SUCCESS = 'RESTORE_DOCUMENT_SUCCESS';
export const RESTORE_DOCUMENT_FAILURE = 'RESTORE_DOCUMENT_FAILURE';

const restoreDocumentAction = () => ({
type: RESTORE_DOCUMENT
});

const restoreDocumentSuccess = data => ({
type: RESTORE_DOCUMENT_SUCCESS,
data
});

const restoreDocumentFailure = error => ({
type: RESTORE_DOCUMENT_FAILURE,
error
});

export const restoreDocument =
({ id }) =>
(dispatch, getState) => {
dispatch(restoreDocumentAction());

const requestOptions = {
method: 'POST',
headers: getState().login.authorizationHeader
};

return fetch(restoreDocumentUrl(id), requestOptions)
.then(checkAndGetStatus)
.then(response => response.json())
.then(data => dispatch(restoreDocumentSuccess(data)))
.catch(errorMessage => {
dispatch(restoreDocumentFailure(errorMessage));
});
};
16 changes: 12 additions & 4 deletions packages/web-app/src/components/common/card/Deleted.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ const StyledEntityIcon = styled(EntityIcon)`
export const Deleted = ({ entityType, entity }) => (
<Layout
title={entity.name}
content={<DeletedCard entityType={entityType} entity={entity} />}
content={
<DeletedCard
entityType={entityType}
entity={entity}
includeSeparator={false}
/>
}
/>
);

Expand All @@ -84,7 +90,8 @@ export const DeletedCard = ({
entity,
isLoading,
onRestorePress,
onPermanentDeletePress
onPermanentDeletePress,
includeSeparator = true
}) => {
const { formatMessage } = useIntl();
const entityI18n = formatMessage({ id: entityType.str });
Expand Down Expand Up @@ -171,7 +178,7 @@ export const DeletedCard = ({
</Tooltip>
)}
</Box>
<hr />
{includeSeparator && <hr />}
</>
);
};
Expand Down Expand Up @@ -401,7 +408,8 @@ DeletedCard.propTypes = {

isLoading: PropTypes.bool,
onRestorePress: PropTypes.func,
onPermanentDeletePress: PropTypes.func
onPermanentDeletePress: PropTypes.func,
includeSeparator: PropTypes.bool
};

Deleted.propTypes = {
Expand Down
12 changes: 12 additions & 0 deletions packages/web-app/src/conf/apiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ export const postDocumentUrl = `${API_BASE_PATH}/documents`;
export const processDocumentIdsUrl = `${API_BASE_PATH}/documents/validate`;
export const putDocumentUrl = documentId =>
`${API_BASE_PATH}/documents/${documentId}`;
export const deleteDocumentUrl = (
documentId,
{ entityId, isPermanent = false }
) =>
`${API_BASE_PATH}/documents/${documentId}?${[
isPermanent ? 'isPermanent=1' : '',
entityId ? `entityId=${entityId}` : ''
]
.filter(e => e)
.join('&')}`;
export const restoreDocumentUrl = documentId =>
`${API_BASE_PATH}/documents/${documentId}/restore`;

export const identifierTypesUrl = `${API_BASE_PATH}/documents/identifierTypes`;
export const subjectsUrl = `${API_BASE_PATH}/documents/subjects`;
Expand Down
Loading

0 comments on commit a694a3e

Please sign in to comment.