Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major documents update #940

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 0 additions & 47 deletions packages/web-app/src/actions/AssociateDocumentToEntrance.js

This file was deleted.

13 changes: 4 additions & 9 deletions packages/web-app/src/actions/Document/CreateDocument.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import fetch from 'isomorphic-fetch';

import { postDocumentUrl } from '../../conf/apiRoutes';
import { buildFormData, buildPages } from './utils';
import { buildFormData } from './utils';

// ==========
export const POST_DOCUMENT = 'POST_DOCUMENT';
export const POST_DOCUMENT_SUCCESS = 'POST_DOCUMENT_SUCCESS';
export const POST_DOCUMENT_FAILURE = 'POST_DOCUMENT_FAILURE';

// ==========

export const postDocumentAction = () => ({
type: POST_DOCUMENT
});
Expand All @@ -28,16 +25,14 @@ export const postDocumentFailure = (errorMessages, httpCode) => ({
export function postDocument(docAttributes) {
return (dispatch, getState) => {
dispatch(postDocumentAction());
const { startPage, endPage } = docAttributes;
const pages = buildPages(startPage, endPage);
const attributes = { ...docAttributes, pages };
const attributes = { ...docAttributes };
const { files } = attributes;
delete attributes.files;
const formData = new FormData();

const formData = new FormData();
buildFormData(formData, attributes);

// Files must have the same key name for each file, as it is asked by the parser on server side.
const { files } = docAttributes;
files.forEach(file => {
formData.append('files', file.file, `${file.name}.${file.extension}`);
});
Expand Down
15 changes: 6 additions & 9 deletions packages/web-app/src/actions/Document/UpdateDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
putDocumentyWithNewEntitiesUrl
} from '../../conf/apiRoutes';
import { checkAndGetStatus } from '../utils';
import { buildFormData, buildPages } from './utils';
import { buildFormData } from './utils';

// ==========

Expand Down Expand Up @@ -37,20 +37,17 @@ export const updateDocumentFailure = (errorMessages, httpCode) => ({
export function updateDocument(docAttributes) {
return (dispatch, getState) => {
dispatch(updateDocumentAction());

const { startPage, endPage } = docAttributes;
const pages = buildPages(startPage, endPage);
const attributes = { ...docAttributes, pages };
const attributes = { ...docAttributes };
const { files } = attributes;
delete attributes.files;
const formData = new FormData();

const formData = new FormData();
buildFormData(formData, attributes);

// Files must have the same key name for each file, as it is asked by the parser on server side.
const { files } = docAttributes;
let indexDeleted = 0;
let indexModified = 0;
files.forEach(file => {
for (const file of files) {
// For a file that is modified or intact, baseFile corresponds to the file entity of the database.
const {
name,
Expand All @@ -75,7 +72,7 @@ export function updateDocument(docAttributes) {
break;
default:
}
});
}

const requestOptions = {
method: 'PUT',
Expand Down
15 changes: 0 additions & 15 deletions packages/web-app/src/actions/Document/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,3 @@ export const buildFormData = (formData, data, parentKey) => {
formData.append(parentKey, data);
}
};

// Merge startPage and endPage in one attribute 'pages'
export const buildPages = (startPage, endPage) => {
let pages = null;
// A page can be 0 so check for 'if(startPage)' only will not work.
// That's why we use 'if(startPage === null)' here.
if (startPage === null && endPage !== null) {
pages = endPage;
} else if (startPage !== null && endPage === null) {
pages = `${startPage},`;
} else if (startPage !== null && endPage !== null) {
pages = `${startPage}-${endPage}`;
}
return pages;
};
49 changes: 28 additions & 21 deletions packages/web-app/src/actions/DocumentChildren.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
import fetch from 'isomorphic-fetch';
import { getDocumentChildrenUrl } from '../conf/apiRoutes';
import makeErrorMessage from '../helpers/makeErrorMessage';
import { checkAndGetStatus } from './utils';

export const FETCH_DOCUMENT_CHILDREN = 'FETCH_DOCUMENT_CHILDREN';
export const FETCH_DOCUMENT_CHILDREN_SUCCESS =
'FETCH_DOCUMENT_CHILDREN_SUCCESS';
export const FETCH_DOCUMENT_CHILDREN_FAILURE =
'FETCH_DOCUMENT_CHILDREN_FAILURE';

export const fetchDocumentChildren = documentId => dispatch => {
dispatch({ type: FETCH_DOCUMENT_CHILDREN });
return fetch(getDocumentChildrenUrl(documentId))
.then(response => {
if (response.status >= 400) {
throw new Error(response.status);
}
return response.json();
})
.then(data =>
dispatch({ type: FETCH_DOCUMENT_CHILDREN_SUCCESS, data: data.documents })
)
.catch(error =>
dispatch({
type: FETCH_DOCUMENT_CHILDREN_FAILURE,
error: makeErrorMessage(
error.message,
`Fetching children of document with id ${documentId}`
)
export const fetchDocumentChildren =
(documentId, locale = 'en') =>
dispatch => {
dispatch({ type: FETCH_DOCUMENT_CHILDREN });
return fetch(getDocumentChildrenUrl(documentId))
.then(checkAndGetStatus)
.then(response => response.json())
.then(data => {
data.documents.sort((a, b) =>
a.title
.toLowerCase()
.localeCompare(b.title.toLowerCase(), locale, { numeric: true })
);
dispatch({
type: FETCH_DOCUMENT_CHILDREN_SUCCESS,
data: data.documents
});
})
);
};
.catch(error =>
dispatch({
type: FETCH_DOCUMENT_CHILDREN_FAILURE,
error: makeErrorMessage(
error.message,
`Fetching children of document with id ${documentId}`
)
})
);
};
9 changes: 3 additions & 6 deletions packages/web-app/src/actions/DocumentDetails.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch from 'isomorphic-fetch';
import { getDocumentDetailsUrl } from '../conf/apiRoutes';
import makeErrorMessage from '../helpers/makeErrorMessage';
import { checkAndGetStatus } from './utils';

export const FETCH_DOCUMENT_DETAILS = 'FETCH_DOCUMENT_DETAILS';
export const FETCH_DOCUMENT_DETAILS_SUCCESS = 'FETCH_DOCUMENT_DETAILS_SUCCESS';
Expand All @@ -10,12 +11,8 @@ export const fetchDocumentDetails = (documentId, requireUpdate) => dispatch => {
dispatch({ type: FETCH_DOCUMENT_DETAILS });
const updateParam = requireUpdate ? `?requireUpdate=${requireUpdate}` : '';
return fetch(getDocumentDetailsUrl + documentId + updateParam)
.then(response => {
if (response.status >= 400) {
throw new Error(response.status);
}
return response.json();
})
.then(checkAndGetStatus)
.then(response => response.json())
.then(data => dispatch({ type: FETCH_DOCUMENT_DETAILS_SUCCESS, data }))
.catch(error =>
dispatch({
Expand Down
28 changes: 20 additions & 8 deletions packages/web-app/src/actions/DocumentType.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import fetch from 'isomorphic-fetch';
import { getDocumentTypesUrl } from '../conf/apiRoutes';
import makeErrorMessage from '../helpers/makeErrorMessage';
import { checkAndGetStatus } from './utils';

export const FETCH_DOCUMENT_TYPES = 'FETCH_DOCUMENT_TYPES';
export const FETCH_DOCUMENT_TYPES_SUCCESS = 'FETCH_DOCUMENT_TYPES_SUCCESS';
export const FETCH_DOCUMENT_TYPES_FAILURE = 'FETCH_DOCUMENT_TYPES_FAILURE';

const FIRST_DOCUMENT_TYPES_TO_DISPLAY = ['Article', 'Collection', 'Issue'];

export const fetchDocumentTypes = () => ({
type: FETCH_DOCUMENT_TYPES
});
Expand All @@ -21,19 +24,28 @@ export const fetchDocumentTypesFailure = error => ({
});

export function loadDocumentTypes() {
// console.trace('loadDocumentTypes');
return dispatch => {
dispatch(fetchDocumentTypes());

return fetch(`${getDocumentTypesUrl}?isAvailable=true`)
.then(response => {
if (response.status >= 400) {
throw new Error(response.status);
}
return response.text();
.then(checkAndGetStatus)
.then(response => response.json())
.then(data => {
const { documentTypes } = data;
const firstDocumentTypes = documentTypes
.filter(dt => FIRST_DOCUMENT_TYPES_TO_DISPLAY.includes(dt.name))
.sort((dt1, dt2) => dt1.name > dt2.name);
const otherDocumentTypes = documentTypes
.filter(dt => !FIRST_DOCUMENT_TYPES_TO_DISPLAY.includes(dt.name))
.sort((dt1, dt2) => dt1.name > dt2.name);
dispatch(
fetchDocumentTypesSuccess([
...firstDocumentTypes,
...otherDocumentTypes
])
);
})
.then(text =>
dispatch(fetchDocumentTypesSuccess(JSON.parse(text).documentTypes))
)
.catch(error =>
dispatch(
fetchDocumentTypesFailure(
Expand Down
44 changes: 44 additions & 0 deletions packages/web-app/src/actions/LinkDocumentToEntrance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fetch from 'isomorphic-fetch';
import { associateDocumentToEntranceUrl } from '../conf/apiRoutes';
import { checkAndGetStatus } from './utils';

export const LINK_DOCUMENT_TO_ENTRANCE = 'LINK_DOCUMENT_TO_ENTRANCE';
export const LINK_DOCUMENT_TO_ENTRANCE_SUCCESS =
'LINK_DOCUMENT_TO_ENTRANCE_SUCCESS';
export const LINK_DOCUMENT_TO_ENTRANCE_FAILURE =
'LINK_DOCUMENT_TO_ENTRANCE_FAILURE';

export const linkDocumentToEntranceAction = () => ({
type: LINK_DOCUMENT_TO_ENTRANCE
});

export const linkDocumentToEntranceSuccess = document => ({
type: LINK_DOCUMENT_TO_ENTRANCE_SUCCESS,
document
});

export const linkDocumentToEntranceFailure = error => ({
type: LINK_DOCUMENT_TO_ENTRANCE_FAILURE,
error
});

export const linkDocumentToEntrance =
({ entranceId, document }) =>
(dispatch, getState) => {
dispatch(linkDocumentToEntranceAction());

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

return fetch(
associateDocumentToEntranceUrl(entranceId, document.id),
requestOptions
)
.then(checkAndGetStatus)
.then(() => dispatch(linkDocumentToEntranceSuccess(document)))
.catch(errorMessage => {
dispatch(linkDocumentToEntranceFailure(errorMessage));
});
};
44 changes: 44 additions & 0 deletions packages/web-app/src/actions/LinkDocumentToMassif.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fetch from 'isomorphic-fetch';
import { associateDocumentToMassifUrl } from '../conf/apiRoutes';
import { checkAndGetStatus } from './utils';

export const LINK_DOCUMENT_TO_MASSIF = 'LINK_DOCUMENT_TO_MASSIF';
export const LINK_DOCUMENT_TO_MASSIF_SUCCESS =
'LINK_DOCUMENT_TO_MASSIF_SUCCESS';
export const LINK_DOCUMENT_TO_MASSIF_FAILURE =
'LINK_DOCUMENT_TO_MASSIF_FAILURE';

export const linkDocumentToMassifAction = () => ({
type: LINK_DOCUMENT_TO_MASSIF
});

export const linkDocumentToMassifSuccess = document => ({
type: LINK_DOCUMENT_TO_MASSIF_SUCCESS,
document
});

export const linkDocumentToMassifFailure = error => ({
type: LINK_DOCUMENT_TO_MASSIF_FAILURE,
error
});

export const linkDocumentToMassif =
({ massifId, document }) =>
(dispatch, getState) => {
dispatch(linkDocumentToMassifAction());

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

return fetch(
associateDocumentToMassifUrl(massifId, document.id),
requestOptions
)
.then(checkAndGetStatus)
.then(() => dispatch(linkDocumentToMassifSuccess(document)))
.catch(errorMessage => {
dispatch(linkDocumentToMassifFailure(errorMessage));
});
};