Skip to content

Commit

Permalink
feat(documents): major refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarseguerra committed Jan 14, 2024
1 parent 648fccb commit 709fdc6
Show file tree
Hide file tree
Showing 87 changed files with 2,269 additions and 4,445 deletions.
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
28 changes: 9 additions & 19 deletions packages/web-app/src/actions/Region.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetch from 'isomorphic-fetch';
import { regionsSearchUrl } from '../conf/apiRoutes';
import { checkAndGetStatus } from './utils';
import makeErrorMessage from '../helpers/makeErrorMessage';

export const REGIONS_SEARCH = 'REGIONS_SEARCH';
Expand All @@ -11,9 +12,9 @@ export const searchRegions = () => ({
type: REGIONS_SEARCH
});

export const searchRegionsSuccess = regions => ({
export const searchRegionsSuccess = results => ({
type: REGIONS_SEARCH_SUCCESS,
regions
results
});

export const searchRegionsFailure = error => ({
Expand All @@ -25,32 +26,21 @@ export const resetRegionsSearch = () => ({
type: RESET_REGIONS_SEARCH
});

export function loadRegionsSearch(regionCode, regionName, isDeprecated) {
export function loadRegionsSearch(query) {
return dispatch => {
dispatch(searchRegions());

return fetch(regionsSearchUrl, {
method: 'POST',
body: JSON.stringify({
code: regionCode,
name: regionName,
isDeprecated
})
body: JSON.stringify({ query })
})
.then(response => {
if (response.status >= 400) {
throw new Error(response.status);
}
return response.text();
})
.then(text => dispatch(searchRegionsSuccess(JSON.parse(text).regions)))
.then(checkAndGetStatus)
.then(response => response.json())
.then(data => dispatch(searchRegionsSuccess(data.results)))
.catch(error =>
dispatch(
searchRegionsFailure(
makeErrorMessage(
error.message,
`Fetching region ${regionCode} ${regionName}`
)
makeErrorMessage(error.message, `Fetching region ${query}`)
)
)
);
Expand Down

0 comments on commit 709fdc6

Please sign in to comment.