From 53b0952887ad4f5e9e996d46709e527273f37ffd Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Thu, 21 Nov 2024 13:56:20 +0100 Subject: [PATCH] refactor getting publication authorization --- .../documentation/documents-by-operator.js | 5 +- .../documentation/documents-certification.js | 8 +- components/operators-detail/layout.js | 10 ++- components/ui/doc-card-upload.js | 5 +- modules/operators-detail.js | 62 ++++--------- pages/operators/[id]/documentation.js | 7 +- selectors/operators-detail/documentation.js | 88 +------------------ utils/documentation.js | 1 - utils/documents.js | 40 +++++++++ 9 files changed, 74 insertions(+), 152 deletions(-) create mode 100644 utils/documents.js diff --git a/components/operators-detail/documentation/documents-by-operator.js b/components/operators-detail/documentation/documents-by-operator.js index 499c4cc3..5724dccb 100644 --- a/components/operators-detail/documentation/documents-by-operator.js +++ b/components/operators-detail/documentation/documents-by-operator.js @@ -10,7 +10,7 @@ import { groupBy } from 'utils/general'; // Redux import { connect } from 'react-redux'; -import { getOperator, getOperatorDocumentation, getOperatorDocumentationCurrent, getOperatorTimeline } from 'modules/operators-detail'; +import { getOperator, getOperatorDocumentation, getOperatorPublicationAuthorization, getOperatorTimeline } from 'modules/operators-detail'; // Components import DocCard from 'components/ui/doc-card'; @@ -165,7 +165,6 @@ function DocumentsByOperator({ groupedByCategory, searchText, user, id, intl, .. props.getOperator(_id) props.getOperatorDocumentation(id) props.getOperatorTimeline(id) - props.getOperatorDocumentationCurrent(id); }} /> )} @@ -192,5 +191,5 @@ export default injectIntl(connect( (state) => ({ user: state.user, }), - { getOperator, getOperatorDocumentation, getOperatorDocumentationCurrent, getOperatorTimeline } + { getOperator, getOperatorDocumentation, getOperatorPublicationAuthorization, getOperatorTimeline } )(DocumentsByOperator)); diff --git a/components/operators-detail/documentation/documents-certification.js b/components/operators-detail/documentation/documents-certification.js index 8037f5d4..ef3363a2 100644 --- a/components/operators-detail/documentation/documents-certification.js +++ b/components/operators-detail/documentation/documents-certification.js @@ -6,7 +6,7 @@ import { isEmpty } from 'utils/general'; import { connect } from 'react-redux'; import { injectIntl } from 'react-intl'; -import { getOperator, getOperatorDocumentation, getOperatorDocumentationCurrent, getOperatorTimeline } from 'modules/operators-detail'; +import { getOperator, getOperatorDocumentation, getOperatorPublicationAuthorization, getOperatorTimeline } from 'modules/operators-detail'; // Components import DocCard from 'components/ui/doc-card'; @@ -78,7 +78,7 @@ function DocumentsCertification(props) { props.getOperator(id); props.getOperatorDocumentation(id); props.getOperatorTimeline(id); - props.getOperatorDocumentationCurrent(id); + props.getOperatorPublicationAuthorization(id); }} /> )} @@ -100,7 +100,7 @@ DocumentsCertification.propTypes = { getOperator: PropTypes.func, getOperatorDocumentation: PropTypes.func, getOperatorTimeline: PropTypes.func, - getOperatorDocumentationCurrent: PropTypes.func, + getOperatorPublicationAuthorization: PropTypes.func, }; export default injectIntl( @@ -109,6 +109,6 @@ export default injectIntl( user: state.user, doc: getContractSignatureDocumentation(state), }), - { getOperator, getOperatorDocumentation, getOperatorTimeline, getOperatorDocumentationCurrent } + { getOperator, getOperatorDocumentation, getOperatorTimeline, getOperatorPublicationAuthorization } )(DocumentsCertification) ); diff --git a/components/operators-detail/layout.js b/components/operators-detail/layout.js index 7f626c09..cc76b7f2 100644 --- a/components/operators-detail/layout.js +++ b/components/operators-detail/layout.js @@ -15,7 +15,7 @@ import { getOperator, getOperatorBySlug, getOperatorDocumentation, - getOperatorDocumentationCurrent, + getOperatorPublicationAuthorization, getOperatorTimeline, getOperatorObservations } from 'modules/operators-detail'; @@ -51,7 +51,7 @@ const COUNTRIES_FRENCH_FIX = { // shared getInitialProps for operator's detail pages export async function getInitialProps({ query, asPath, res, store, ...rest }) { - let { operatorsDetail } = store.getState(); + let { operatorsDetail, user } = store.getState(); const requests = []; const {id} = query; const tab = asPath.split('/').pop(); @@ -76,8 +76,10 @@ export async function getInitialProps({ query, asPath, res, store, ...rest }) { if (operator && !isEmpty(operator)) { if (operatorsDetail.documentation.operatorId !== operator.id && tab === 'documentation') { requests.push(store.dispatch(getOperatorDocumentation(operator.id))); - requests.push(store.dispatch(getOperatorDocumentationCurrent(operator.id))); requests.push(store.dispatch(getOperatorTimeline(operator.id))); + if (user.token && user.operator_ids && user.operator_ids.includes(+operator.id)) { + requests.push(store.dispatch(getOperatorPublicationAuthorization(operator.id))); + } } if (operatorsDetail.observations.operatorId !== operator.id && (tab === 'observations' || tab === 'overview')) { @@ -217,7 +219,7 @@ export default withRouter(injectIntl( { getOperator, getOperatorDocumentation, - getOperatorDocumentationCurrent, + getOperatorPublicationAuthorization, getOperatorTimeline, getOperatorObservations, getIntegratedAlertsMetadata diff --git a/components/ui/doc-card-upload.js b/components/ui/doc-card-upload.js index c8252228..315da1bd 100644 --- a/components/ui/doc-card-upload.js +++ b/components/ui/doc-card-upload.js @@ -112,10 +112,7 @@ class DocCardUpload extends React.Component { const { deleteLoading } = this.state; const currentDate = dayjs(new Date()); const selectedDate = dayjs(date); - const isEditable = - currentDate.year() === selectedDate.year() && - currentDate.month() === selectedDate.month() && - currentDate.dayOfYear() === selectedDate.dayOfYear(); + const isEditable = selectedDate.isSame(currentDate, 'day'); const btnTooltip = !isEditable ? 'Please select the most recent date on the filters to edit any document' : null; diff --git a/modules/operators-detail.js b/modules/operators-detail.js index 7d98fbfc..0dfa46d8 100644 --- a/modules/operators-detail.js +++ b/modules/operators-detail.js @@ -1,6 +1,7 @@ import dayjs from 'dayjs'; import API from 'services/api'; +import { parseDocument } from 'utils/documents'; /* Constants */ const GET_OPERATOR_SUCCESS = 'GET_OPERATOR_SUCCESS'; @@ -15,9 +16,8 @@ const GET_OPERATOR_DOCUMENTATION_SUCCESS = 'GET_OPERATOR_DOCUMENTATION_SUCCESS'; const GET_OPERATOR_DOCUMENTATION_ERROR = 'GET_OPERATOR_DOCUMENTATION_ERROR'; const GET_OPERATOR_DOCUMENTATION_LOADING = 'GET_OPERATOR_DOCUMENTATION_LOADING'; -const GET_OPERATOR_CURRENT_DOCUMENTATION_SUCCESS = 'GET_OPERATOR_CURRENT_DOCUMENTATION_SUCCESS'; -const GET_OPERATOR_CURRENT_DOCUMENTATION_ERROR = 'GET_OPERATOR_CURRENT_DOCUMENTATION_ERROR'; -const GET_OPERATOR_CURRENT_DOCUMENTATION_LOADING = 'GET_OPERATOR_CURRENT_DOCUMENTATION_LOADING'; +const GET_OPERATOR_PUBLICATION_AUTHORIZATION_SUCCESS = 'GET_OPERATOR_PUBLICATION_AUTHORIZATION_SUCCESS'; +const GET_OPERATOR_PUBLICATION_AUTHORIZATION_ERROR = 'GET_OPERATOR_PUBLICATION_AUTHORIZATION_ERROR'; const GET_OPERATOR_TIMELINE_SUCCESS = 'GET_OPERATOR_TIMELINE_SUCCESS'; const GET_OPERATOR_TIMELINE_ERROR = 'GET_OPERATOR_TIMELINE_ERROR'; @@ -54,12 +54,7 @@ const initialState = { error: false, timestamp: null }, - documentationCurrent: { - data: [], - loading: false, - error: false, - timestamp: null - }, + publicationAuthorization: null, date: dayjs().format('YYYY-MM-DD'), fmu: null, timeline: [], @@ -176,32 +171,11 @@ export default function reducer(state = initialState, action) { }); return Object.assign({}, state, { observations }); } - case GET_OPERATOR_CURRENT_DOCUMENTATION_SUCCESS: { - if (!isLatestAction(state.documentationCurrent, action)) return state; - - const documentationCurrent = Object.assign({}, state.documentationCurrent, { - data: action.payload, - loading: false, - error: false, - }); - return Object.assign({}, state, { documentationCurrent }); + case GET_OPERATOR_PUBLICATION_AUTHORIZATION_SUCCESS: { + return Object.assign({}, state, { publicationAuthorization: action.payload }); } - case GET_OPERATOR_CURRENT_DOCUMENTATION_ERROR: { - if (!isLatestAction(state.documentationCurrent, action)) return state; - - const documentationCurrent = Object.assign({}, state.documentationCurrent, { - error: true, - loading: false, - }); - return Object.assign({}, state, { documentationCurrent }); - } - case GET_OPERATOR_CURRENT_DOCUMENTATION_LOADING: { - const documentationCurrent = Object.assign({}, state.documentationCurrent, { - loading: true, - error: false, - timestamp: action.metadata.timestamp - }); - return Object.assign({}, state, { documentationCurrent }); + case GET_OPERATOR_PUBLICATION_AUTHORIZATION_ERROR: { + return Object.assign({}, state, { publicationAuthorization: null }); } case GET_SAWMILLS_SUCCESS: { const sawmills = Object.assign({}, state.sawmills, { @@ -424,12 +398,9 @@ export function getOperatorObservations(operatorId) { }; } -export function getOperatorDocumentationCurrent(id) { +export function getOperatorPublicationAuthorization(id) { return (dispatch, getState) => { const { user, language } = getState(); - const metadata = { timestamp: new Date(), operatorId: id }; - - dispatch({ type: GET_OPERATOR_CURRENT_DOCUMENTATION_LOADING, metadata }); const includeFields = [ 'required-operator-document', @@ -439,23 +410,22 @@ export function getOperatorDocumentationCurrent(id) { return API.get('operator-documents', { locale: language, include: includeFields.join(','), - 'page[size]': 1000, 'filter[operator-id]': id, + 'filter[contract-signature]': true, }, { token: user.token, }) .then(({ data }) => { + const doc = data.find((doc) => doc['required-operator-document']['contract-signature']); + dispatch({ - type: GET_OPERATOR_CURRENT_DOCUMENTATION_SUCCESS, - payload: data, - metadata + type: GET_OPERATOR_PUBLICATION_AUTHORIZATION_SUCCESS, + payload: parseDocument(doc) }); }) - .catch((err) => { + .catch((_err) => { dispatch({ - type: GET_OPERATOR_CURRENT_DOCUMENTATION_ERROR, - payload: err.message, - metadata + type: GET_OPERATOR_PUBLICATION_AUTHORIZATION_ERROR }); }); }; diff --git a/pages/operators/[id]/documentation.js b/pages/operators/[id]/documentation.js index 9bd718c5..68bff50b 100644 --- a/pages/operators/[id]/documentation.js +++ b/pages/operators/[id]/documentation.js @@ -10,8 +10,7 @@ import { getParsedTimeline } from 'selectors/operators-detail/timeline'; import { connect } from 'react-redux'; import { getOperator, - getOperatorDocumentation, - getOperatorDocumentationCurrent, + getOperatorDocumentation } from 'modules/operators-detail'; // Components @@ -31,7 +30,6 @@ class OperatorsDetailDocumentationPage extends React.Component { const { operatorsDetail } = this.props; const operator = operatorsDetail.data; this.props.getOperatorDocumentation(operator.id); - this.props.getOperatorDocumentationCurrent(operator.id); } } @@ -72,7 +70,6 @@ export default connect( }), { getOperator, - getOperatorDocumentation, - getOperatorDocumentationCurrent, + getOperatorDocumentation } )(OperatorsDetailDocumentationPage); diff --git a/selectors/operators-detail/documentation.js b/selectors/operators-detail/documentation.js index 1f2b983c..75d524ce 100644 --- a/selectors/operators-detail/documentation.js +++ b/selectors/operators-detail/documentation.js @@ -1,10 +1,10 @@ import { createSelector } from 'reselect'; import uniqBy from 'lodash/uniqBy'; import sortBy from 'lodash/sortBy'; +import { parseDocument } from 'utils/documents'; // Get the datasets and filters from state const operatorDocumentation = (state) => state.operatorsDetail.documentation; -const operatorDocumentationCurrent = (state) => state.operatorsDetail.documentationCurrent; export const getFMUs = (state) => state.operatorsDetail.data.fmus; export const getOperatorDocumentationFMU = (state) => state.operatorsDetail.fmu; export const getOperatorDocumentationDate = (state) => @@ -21,42 +21,7 @@ const getParsedDocumentation = createSelector( .filter((doc) => !fmu || (doc.fmu && doc.fmu.id === fmu.id)) .map((doc) => { try { - return { - id: doc.id, - docId: doc['operator-document-id'], - fmu: doc.fmu, - requiredDocId: doc['required-operator-document'].id, - url: doc.attachment?.url, - type: doc.type, - source: doc['source-type'], - sourceInfo: doc['source-info'], - title: doc['required-operator-document'].name, - public: doc.public, - explanation: doc['required-operator-document'].explanation, - position: doc['required-operator-document'].position, - category: - doc['required-operator-document'][ - 'required-operator-document-group' - ].name, - categoryPosition: - doc['required-operator-document'][ - 'required-operator-document-group' - ].position, - status: doc.status, - reason: doc.reason, - adminComment: doc['admin-comment'], - startDate: new Date(doc['start-date']) - .toJSON() - .slice(0, 10) - .replace(/-/g, '/'), - endDate: new Date(doc['expire-date']) - .toJSON() - .slice(0, 10) - .replace(/-/g, '/'), - annexes: doc['operator-document-annexes'] - ? doc['operator-document-annexes'] - : [], - }; + return parseDocument(doc); } catch (error) { return null; } @@ -64,54 +29,7 @@ const getParsedDocumentation = createSelector( } ); -const getContractSignatureDocumentation = createSelector( - operatorDocumentationCurrent, - (documentation) => { - let contractSignature = {}; - - if (documentation.data) { - const doc = documentation.data.find( - (d) => d['required-operator-document']['contract-signature'] - ); - - if (doc) { - contractSignature = { - id: doc.id, - docId: doc.id, - requiredDocId: doc['required-operator-document'].id, - url: doc.attachment?.url, - type: doc.type, - public: doc.public, - title: doc['required-operator-document'].name, - explanation: doc['required-operator-document'].explanation, - category: - doc['required-operator-document'][ - 'required-operator-document-group' - ].name, - categoryPosition: - doc['required-operator-document'][ - 'required-operator-document-group' - ].position, - status: doc.status, - reason: doc.reason, - startDate: new Date(doc['start-date']) - .toJSON() - .slice(0, 10) - .replace(/-/g, '/'), - endDate: new Date(doc['expire-date']) - .toJSON() - .slice(0, 10) - .replace(/-/g, '/'), - annexes: doc['operator-document-annexes'] - ? doc['operator-document-annexes'] - : [], - }; - } - } - - return contractSignature; - } -); +const getContractSignatureDocumentation = (state) => state.operatorsDetail.publicationAuthorization || {}; const getHistoricFMUs = createSelector( [operatorDocumentation], diff --git a/utils/documentation.js b/utils/documentation.js index f224dbb1..34532f23 100644 --- a/utils/documentation.js +++ b/utils/documentation.js @@ -40,7 +40,6 @@ export const PALETTE = { } }; - const HELPERS_DOC = { getMetadata() { return JSON.parse(JSON.stringify(PALETTE)); diff --git a/utils/documents.js b/utils/documents.js new file mode 100644 index 00000000..c3961625 --- /dev/null +++ b/utils/documents.js @@ -0,0 +1,40 @@ +export function parseDocument(doc) { + if (!doc) return null; + + return { + id: doc.id, + docId: doc['operator-document-id'] || doc.id, + fmu: doc.fmu, + requiredDocId: doc['required-operator-document'].id, + url: doc.attachment?.url, + type: doc.type, + source: doc['source-type'], + sourceInfo: doc['source-info'], + title: doc['required-operator-document'].name, + public: doc.public, + explanation: doc['required-operator-document'].explanation, + position: doc['required-operator-document'].position, + category: + doc['required-operator-document'][ + 'required-operator-document-group' + ].name, + categoryPosition: + doc['required-operator-document'][ + 'required-operator-document-group' + ].position, + status: doc.status, + reason: doc.reason, + adminComment: doc['admin-comment'], + startDate: new Date(doc['start-date']) + .toJSON() + .slice(0, 10) + .replace(/-/g, '/'), + endDate: new Date(doc['expire-date']) + .toJSON() + .slice(0, 10) + .replace(/-/g, '/'), + annexes: doc['operator-document-annexes'] + ? doc['operator-document-annexes'] + : [], + }; +}