Skip to content

Commit

Permalink
[#130] Fix form notification messages
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Nov 25, 2024
1 parent bd8710b commit 1802ecd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/actions/RecordActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ActionConstants from "../constants/ActionConstants";
import { ACTION_FLAG } from "../constants/DefaultConstants";
import { ACTION_FLAG, FORM_ACTION_FLAG } from "../constants/DefaultConstants";
import { axiosBackend } from "./index";
import * as Utils from "../utils/Utils";
import { loadRecords } from "./RecordsActions";
Expand Down Expand Up @@ -115,7 +115,7 @@ export function createRecord(record) {
};
}

export function updateRecord(record) {
export function updateRecord(record, formActionFlag) {
return function (dispatch, getState) {
dispatch(saveRecordPending(ACTION_FLAG.UPDATE_ENTITY));
return axiosBackend
Expand All @@ -125,7 +125,13 @@ export function updateRecord(record) {
.then(() => {
dispatch(saveRecordSuccess(record, null, ACTION_FLAG.UPDATE_ENTITY));
dispatch(loadRecords());
dispatch(publishMessage(successMessage("record.save-success")));
if (formActionFlag === FORM_ACTION_FLAG.SAVE_FORM) {
dispatch(publishMessage(successMessage("record.save-success")));
} else if (formActionFlag === FORM_ACTION_FLAG.COMPLETE_FORM) {
dispatch(publishMessage(successMessage("record.complete-success")));
} else if (formActionFlag === FORM_ACTION_FLAG.REJECT_FORM) {
dispatch(publishMessage(successMessage("record.reject-success")));
}
})
.catch((error) => {
dispatch(saveRecordError(error.response.data, record, ACTION_FLAG.UPDATE_ENTITY));
Expand Down
8 changes: 4 additions & 4 deletions src/components/record/RecordController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import withI18n from "../../i18n/withI18n";
import Record from "./Record";
import Routes from "../../constants/RoutesConstants";
import { transitionToWithOpts } from "../../utils/Routing";
import { ACTION_FLAG, ACTION_STATUS, RECORD_PHASE } from "../../constants/DefaultConstants";
import { ACTION_FLAG, ACTION_STATUS, FORM_ACTION_FLAG, RECORD_PHASE } from "../../constants/DefaultConstants";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import {
Expand Down Expand Up @@ -102,7 +102,7 @@ class RecordController extends React.Component {
if (record.isNew) {
trackPromise(this.props.createRecord(omit(record, "isNew"), currentUser), "record");
} else {
trackPromise(this.props.updateRecord(record, currentUser), "record");
trackPromise(this.props.updateRecord(record, FORM_ACTION_FLAG.SAVE_FORM, currentUser), "record");
}
};

Expand Down Expand Up @@ -132,7 +132,7 @@ class RecordController extends React.Component {
_onComplete = () => {
this._handlePhaseChange(RECORD_PHASE.COMPLETED, () => {
this._handleRejectReason(null, () => {
this.props.updateRecord(this.state.record, this.props.currentUser);
this.props.updateRecord(this.state.record, FORM_ACTION_FLAG.COMPLETE_FORM, this.props.currentUser);
this._transitionToRecords();
});
});
Expand All @@ -141,7 +141,7 @@ class RecordController extends React.Component {
_onReject = (rejectionReason) => {
this._handlePhaseChange(RECORD_PHASE.REJECTED, () => {
this._handleRejectReason(rejectionReason, () => {
this.props.updateRecord(this.state.record, this.props.currentUser);
this.props.updateRecord(this.state.record, FORM_ACTION_FLAG.REJECT_FORM, this.props.currentUser);
this._transitionToRecords();
});
});
Expand Down
6 changes: 6 additions & 0 deletions src/constants/DefaultConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export const ACTION_FLAG = {
UPDATE_ENTITY: "UPDATE_ENTITY",
};

export const FORM_ACTION_FLAG = {
COMPLETE_FORM: "COMPLETE_FORM",
REJECT_FORM: "REJECT_FORM",
SAVE_FORM: "SAVE_FORM",
};

export const ACTION_STATUS = {
PENDING: "PENDING",
SUCCESS: "SUCCESS",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ export default {
"record.created-by-msg": "Vytvořil(a) {name} {date}.",
"record.last-edited-msg": "Naposledy upravil(a) {name} {date}.",
"record.save-success": "Záznam úspěšně uložen.",
"record.complete-success": "Formulář byl úspěšně dokončen.",
"record.reject-success": "Formulář byl úspěšně zamítnut.",
"record.save-error": "Záznam se nepodařilo uložit. {error}",
"record.save-error.user-not-assigned-to-institution": "Uživatel není přiřazen k žádné instituci.",
"record.form.please-wait": "Nahrávám formulář, prosím, čekejte...",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ export default {
"record.created-by-msg": "Created {date} by {name}.",
"record.last-edited-msg": "Last modified {date} by {name}.",
"record.save-success": "Form successfully saved.",
"record.complete-success": "Form successfully completed.",
"record.reject-success": "Form successfully rejected.",
"record.save-error": "Unable to save record. {error}",
"record.save-error.user-not-assigned-to-institution": "User is not assigned to any institution.",
"record.form.please-wait": "Loading form, please wait...",
Expand Down
6 changes: 3 additions & 3 deletions tests/__tests__/actions/RecordActions.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as ActionConstants from "../../../src/constants/ActionConstants";
import MockAdapter from "axios-mock-adapter";
import { TEST_TIMEOUT } from "../../constants/DefaultTestConstants";
import { axiosBackend } from "../../../src/actions";
import { ACTION_FLAG, ROLE } from "../../../src/constants/DefaultConstants";
import { ACTION_FLAG, FORM_ACTION_FLAG, ROLE } from "../../../src/constants/DefaultConstants";
import {
createRecord,
deleteRecord,
Expand Down Expand Up @@ -183,14 +183,14 @@ describe("Record asynchronous actions", function () {
{ type: ActionConstants.SAVE_RECORD_PENDING, actionFlag: ACTION_FLAG.UPDATE_ENTITY },
{ type: ActionConstants.SAVE_RECORD_SUCCESS, key: null, actionFlag: ACTION_FLAG.UPDATE_ENTITY, record },
{ type: ActionConstants.LOAD_RECORDS_PENDING },
{ type: ActionConstants.PUBLISH_MESSAGE, message: successMessage("record.save-success") },
{ type: ActionConstants.PUBLISH_MESSAGE, message: successMessage("record.complete-success") },
{ type: ActionConstants.LOAD_RECORDS_SUCCESS, records },
];

mockApi.onPut(`${API_URL}/rest/records/${record.key}`).reply(200, null, { location });
mockApi.onGet(`${API_URL}/rest/records`).reply(200, records, {});

store.dispatch(updateRecord(record));
store.dispatch(updateRecord(record, FORM_ACTION_FLAG.COMPLETE_FORM));

setTimeout(() => {
expect(store.getActions()).toEqual(expectedActions);
Expand Down

0 comments on commit 1802ecd

Please sign in to comment.