From d887a6373fbb632adcfc49d3a5ca9902f482258f Mon Sep 17 00:00:00 2001 From: Miroslav Blasko Date: Mon, 22 Jul 2024 15:56:04 +0200 Subject: [PATCH] [#180] Implement publish button --- src/actions/RecordsActions.js | 39 +++++++++++++++++++++ src/components/record/RecordsController.jsx | 22 +++--------- src/constants/ActionConstants.js | 6 ++++ 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/actions/RecordsActions.js b/src/actions/RecordsActions.js index 25ca3575..ae13a5c4 100644 --- a/src/actions/RecordsActions.js +++ b/src/actions/RecordsActions.js @@ -79,6 +79,45 @@ export function exportRecords(exportType, params = {}) { }; } +export function publishRecords(params = {}) { + return (dispatch, getState) => { + dispatch(asyncRequest(ActionConstants.PUBLISH_RECORDS_PENDING)); + const currentUser = getState().auth.user; + if (currentUser && !isAdmin(currentUser) && currentUser.institution) { + params.institution = currentUser.institution.key; + } + return axiosBackend + .post(`${API_URL}/rest/records/publish`, null, { + params, + paramsSerializer, + }) + .then((resp) => { + dispatch(asyncSuccess(ActionConstants.PUBLISH_RECORDS_SUCCESS)); + dispatch(loadRecords()); + if (resp.data.importedCount < resp.data.totalCount) { + dispatch( + publishMessage( + infoMessage("records.import.partialSuccess.message", { + importedCount: resp.data.importedCount, + totalCount: resp.data.totalCount, + }), + ), + ); + } else { + dispatch( + publishMessage( + successMessage("records.import.success.message", { importedCount: resp.data.importedCount }), + ), + ); + } + }) + .catch((error) => { + dispatch(asyncError(ActionConstants.PUBLISH_RECORDS_ERROR, error.response?.data || error.message)); + dispatch(showServerResponseErrorMessage(error, "records.import.error.message")); + }); + }; +} + export function importRecords(file) { return (dispatch) => { dispatch(asyncRequest(ActionConstants.IMPORT_RECORDS_PENDING)); diff --git a/src/components/record/RecordsController.jsx b/src/components/record/RecordsController.jsx index 3ce2a2c3..4222d072 100644 --- a/src/components/record/RecordsController.jsx +++ b/src/components/record/RecordsController.jsx @@ -3,7 +3,7 @@ import React from "react"; import Records from "./Records"; import Routes from "../../constants/RoutesConstants"; import { transitionToWithOpts } from "../../utils/Routing"; -import { exportRecords, importRecords, loadRecords } from "../../actions/RecordsActions"; +import { exportRecords, importRecords, loadRecords, publishRecords } from "../../actions/RecordsActions"; import { injectIntl } from "react-intl"; import withI18n from "../../i18n/withI18n"; import { connect } from "react-redux"; @@ -81,22 +81,8 @@ class RecordsController extends React.Component { }; _onPublishRecords = async () => { - this.setState( - { - records: this.props.recordsLoaded.records, - }, - async () => { - const updatedRecords = this.state.records.map(async (record) => { - if (record.phase === RECORD_PHASE.COMPLETED) { - const updatedRecord = { ...record, phase: RECORD_PHASE.PUBLISHED }; - await this.props.updateRecord(updatedRecord); - return updatedRecord; - } - }); - - return await Promise.all(updatedRecords); - }, - ); + trackPromise(this.props.publishRecords(), "records"); + //TODO maybe refresh updated records in list ?? }; _onExportRecords = (exportType) => { @@ -178,6 +164,7 @@ RecordsController.propTypes = { deleteRecord: PropTypes.func.isRequired, updateRecord: PropTypes.func.isRequired, exportRecords: PropTypes.func.isRequired, + publishRecords: PropTypes.func.isRequired, importRecords: PropTypes.func.isRequired, formTemplatesLoaded: PropTypes.object.isRequired, recordsLoaded: PropTypes.shape({ @@ -206,6 +193,7 @@ function mapDispatchToProps(dispatch) { updateRecord: bindActionCreators(updateRecord, dispatch), loadRecords: bindActionCreators(loadRecords, dispatch), exportRecords: bindActionCreators(exportRecords, dispatch), + publishRecords: bindActionCreators(publishRecords, dispatch), importRecords: bindActionCreators(importRecords, dispatch), loadFormTemplates: bindActionCreators(loadFormTemplates, dispatch), transitionToWithOpts: bindActionCreators(transitionToWithOpts, dispatch), diff --git a/src/constants/ActionConstants.js b/src/constants/ActionConstants.js index 29809f17..dc1e2f0a 100644 --- a/src/constants/ActionConstants.js +++ b/src/constants/ActionConstants.js @@ -55,9 +55,15 @@ export const DELETE_RECORD_ERROR = "DELETE_RECORD_ERROR"; export const LOAD_RECORDS_PENDING = "LOAD_RECORDS_PENDING"; export const LOAD_RECORDS_SUCCESS = "LOAD_RECORDS_SUCCESS"; export const LOAD_RECORDS_ERROR = "LOAD_RECORDS_ERROR"; + export const EXPORT_RECORDS_PENDING = "EXPORT_RECORDS_PENDING"; export const EXPORT_RECORDS_SUCCESS = "EXPORT_RECORDS_SUCCESS"; export const EXPORT_RECORDS_ERROR = "EXPORT_RECORDS_ERROR"; + +export const PUBLISH_RECORDS_PENDING = "PUBLISH_RECORDS_PENDING"; +export const PUBLISH_RECORDS_SUCCESS = "PUBLISH_RECORDS_SUCCESS"; +export const PUBLISH_RECORDS_ERROR = "PUBLISH_RECORDS_ERROR"; + export const IMPORT_RECORDS_PENDING = "IMPORT_RECORDS_PENDING"; export const IMPORT_RECORDS_SUCCESS = "IMPORT_RECORDS_SUCCESS"; export const IMPORT_RECORDS_ERROR = "IMPORT_RECORDS_ERROR";