Skip to content

Commit

Permalink
refactor getting publication authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Nov 28, 2024
1 parent a671681 commit 53b0952
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -165,7 +165,6 @@ function DocumentsByOperator({ groupedByCategory, searchText, user, id, intl, ..
props.getOperator(_id)
props.getOperatorDocumentation(id)
props.getOperatorTimeline(id)
props.getOperatorDocumentationCurrent(id);
}}
/>
)}
Expand All @@ -192,5 +191,5 @@ export default injectIntl(connect(
(state) => ({
user: state.user,
}),
{ getOperator, getOperatorDocumentation, getOperatorDocumentationCurrent, getOperatorTimeline }
{ getOperator, getOperatorDocumentation, getOperatorPublicationAuthorization, getOperatorTimeline }
)(DocumentsByOperator));
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -78,7 +78,7 @@ function DocumentsCertification(props) {
props.getOperator(id);
props.getOperatorDocumentation(id);
props.getOperatorTimeline(id);
props.getOperatorDocumentationCurrent(id);
props.getOperatorPublicationAuthorization(id);
}}
/>
)}
Expand All @@ -100,7 +100,7 @@ DocumentsCertification.propTypes = {
getOperator: PropTypes.func,
getOperatorDocumentation: PropTypes.func,
getOperatorTimeline: PropTypes.func,
getOperatorDocumentationCurrent: PropTypes.func,
getOperatorPublicationAuthorization: PropTypes.func,
};

export default injectIntl(
Expand All @@ -109,6 +109,6 @@ export default injectIntl(
user: state.user,
doc: getContractSignatureDocumentation(state),
}),
{ getOperator, getOperatorDocumentation, getOperatorTimeline, getOperatorDocumentationCurrent }
{ getOperator, getOperatorDocumentation, getOperatorTimeline, getOperatorPublicationAuthorization }
)(DocumentsCertification)
);
10 changes: 6 additions & 4 deletions components/operators-detail/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getOperator,
getOperatorBySlug,
getOperatorDocumentation,
getOperatorDocumentationCurrent,
getOperatorPublicationAuthorization,
getOperatorTimeline,
getOperatorObservations
} from 'modules/operators-detail';
Expand Down Expand Up @@ -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();
Expand All @@ -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')) {
Expand Down Expand Up @@ -217,7 +219,7 @@ export default withRouter(injectIntl(
{
getOperator,
getOperatorDocumentation,
getOperatorDocumentationCurrent,
getOperatorPublicationAuthorization,
getOperatorTimeline,
getOperatorObservations,
getIntegratedAlertsMetadata
Expand Down
5 changes: 1 addition & 4 deletions components/ui/doc-card-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 16 additions & 46 deletions modules/operators-detail.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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: [],
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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',
Expand All @@ -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
});
});
};
Expand Down
7 changes: 2 additions & 5 deletions pages/operators/[id]/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -72,7 +70,6 @@ export default connect(
}),
{
getOperator,
getOperatorDocumentation,
getOperatorDocumentationCurrent,
getOperatorDocumentation
}
)(OperatorsDetailDocumentationPage);
88 changes: 3 additions & 85 deletions selectors/operators-detail/documentation.js
Original file line number Diff line number Diff line change
@@ -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) =>
Expand All @@ -21,97 +21,15 @@ 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;
}
});
}
);

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],
Expand Down
1 change: 0 additions & 1 deletion utils/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const PALETTE = {
}
};


const HELPERS_DOC = {
getMetadata() {
return JSON.parse(JSON.stringify(PALETTE));
Expand Down
Loading

0 comments on commit 53b0952

Please sign in to comment.