Skip to content

Commit

Permalink
Merge pull request #192 from kbss-cvut/180-implement-publish-button
Browse files Browse the repository at this point in the history
[#180] Implement publish button
  • Loading branch information
blcham authored Jul 23, 2024
2 parents 9c52b4c + d887a63 commit b8a82f3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
39 changes: 39 additions & 0 deletions src/actions/RecordsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
22 changes: 5 additions & 17 deletions src/components/record/RecordsController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions src/constants/ActionConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit b8a82f3

Please sign in to comment.