Skip to content

Commit

Permalink
Merge pull request kbss-cvut#77 from akaene/main
Browse files Browse the repository at this point in the history
Refactor code to use the common messaging component
  • Loading branch information
blcham committed Jan 26, 2024
2 parents 9b73223 + 4cc898f commit 41d16c4
Show file tree
Hide file tree
Showing 55 changed files with 581 additions and 930 deletions.
2 changes: 1 addition & 1 deletion deploy/keycloak-auth/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ services:
- "8080"
volumes:
- auth-server:/opt/keycloak/data
- ../shared/keycloak:/opt/keycloak/data/import
- ./keycloak:/opt/keycloak/data/import
depends_on:
- auth-server-db

Expand Down
7 changes: 7 additions & 0 deletions js/actions/AsyncActionUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {publishMessage} from "./MessageActions";
import {errorMessage} from "../model/Message";

export function asyncRequest(type) {
return {type};
}
Expand All @@ -11,4 +14,8 @@ export function asyncError(type, error) {

export function asyncSuccess(type) {
return {type};
}

export function showServerResponseErrorMessage(error, i18nMessageId) {
return publishMessage(errorMessage(i18nMessageId, {error: error.response.data.message}))
}
9 changes: 6 additions & 3 deletions js/actions/HistoryActions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {axiosBackend} from "./index";
import * as ActionConstants from "../constants/ActionConstants";
import {omit, startsWith, endsWith} from 'lodash';
import {endsWith, omit, startsWith} from 'lodash';
import {API_URL} from '../../config';
import {showServerResponseErrorMessage} from "./AsyncActionUtils";

const URL_PREFIX = 'rest/history';

Expand Down Expand Up @@ -33,21 +34,23 @@ export function loadActions(pageNumber, searchData) {
}
return function (dispatch) {
dispatch({type: ActionConstants.LOAD_ACTIONS_HISTORY_PENDING});
axiosBackend.get(`${API_URL}/${URL_PREFIX}${urlSuffix}`).then((response) => {
return axiosBackend.get(`${API_URL}/${URL_PREFIX}${urlSuffix}`).then((response) => {
dispatch({type: ActionConstants.LOAD_ACTIONS_HISTORY_SUCCESS, actionsHistory: response.data});
}).catch((error) => {
dispatch({type: ActionConstants.LOAD_ACTIONS_HISTORY_ERROR, error: error.response.data});
dispatch(showServerResponseErrorMessage(error, 'history.loading-error'));
});
}
}

export function loadActionByKey(key) {
return function (dispatch) {
dispatch({type: ActionConstants.LOAD_ACTION_HISTORY_PENDING});
axiosBackend.get(`${API_URL}/${URL_PREFIX}/${key}`).then((response) => {
return axiosBackend.get(`${API_URL}/${URL_PREFIX}/${key}`).then((response) => {
dispatch({type: ActionConstants.LOAD_ACTION_HISTORY_SUCCESS, actionHistory: response.data});
}).catch((error) => {
dispatch({type: ActionConstants.LOAD_ACTION_HISTORY_ERROR, error: error.response.data});
dispatch(showServerResponseErrorMessage(error, 'history.load-error'));
});
}
}
21 changes: 15 additions & 6 deletions js/actions/InstitutionActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import {ACTION_FLAG} from "../constants/DefaultConstants";
import * as Utils from "../utils/Utils";
import {loadInstitutions} from "./InstitutionsActions";
import {API_URL} from '../../config';
import {publishMessage} from "./MessageActions";
import {errorMessage, successMessage} from "../model/Message";
import {showServerResponseErrorMessage} from "./AsyncActionUtils";

export function deleteInstitution(institution) {
//console.log("Deleting institution: ", institution);
return function (dispatch) {
return function (dispatch, getState) {
dispatch(deleteInstitutionPending(institution.key));
axiosBackend.delete(`${API_URL}/rest/institutions/${institution.key}`, {
return axiosBackend.delete(`${API_URL}/rest/institutions/${institution.key}`, {
...institution
}).then(() => {
dispatch(loadInstitutions());
dispatch(deleteInstitutionSuccess(institution));
dispatch(publishMessage(successMessage('institution.delete-success')));
}).catch((error) => {
dispatch(deleteInstitutionError(error.response.data, institution));
dispatch(publishMessage(errorMessage('institution.delete-error', {error: getState().intl.messages[error.response.data.messageId]})));
});
}
}
Expand Down Expand Up @@ -46,10 +50,11 @@ export function loadInstitution(key) {
//console.log("Loading institution with key: ", key);
return function (dispatch) {
dispatch(loadInstitutionPending());
axiosBackend.get(`${API_URL}/rest/institutions/${key}`).then((response) => {
return axiosBackend.get(`${API_URL}/rest/institutions/${key}`).then((response) => {
dispatch(loadInstitutionSuccess(response.data));
}).catch((error) => {
dispatch(loadInstitutionError(error.response.data));
dispatch(showServerResponseErrorMessage(error, 'institution.load-error'));
});
}
}
Expand Down Expand Up @@ -84,14 +89,16 @@ export function createInstitution(institution) {
//console.log("Creating institution: ", institution);
return function (dispatch) {
dispatch(saveInstitutionPending(ACTION_FLAG.CREATE_ENTITY));
axiosBackend.post(`${API_URL}/rest/institutions`, {
return axiosBackend.post(`${API_URL}/rest/institutions`, {
...institution
}).then((response) => {
const key = Utils.extractKeyFromLocationHeader(response);
dispatch(saveInstitutionSuccess(institution, key, ACTION_FLAG.CREATE_ENTITY));
dispatch(loadInstitutions());
dispatch(publishMessage(successMessage('institution.save-success')));
}).catch((error) => {
dispatch(saveInstitutionError(error.response.data, institution, ACTION_FLAG.CREATE_ENTITY));
dispatch(showServerResponseErrorMessage(error, 'institution.save-error'));
});
}
}
Expand All @@ -100,13 +107,15 @@ export function updateInstitution(institution) {
//console.log("Updating institution: ", institution);
return function (dispatch) {
dispatch(saveInstitutionPending(ACTION_FLAG.UPDATE_ENTITY));
axiosBackend.put(`${API_URL}/rest/institutions/${institution.key}`, {
return axiosBackend.put(`${API_URL}/rest/institutions/${institution.key}`, {
...institution
}).then(() => {
dispatch(saveInstitutionSuccess(institution, null, ACTION_FLAG.UPDATE_ENTITY));
dispatch(loadInstitutions());
dispatch(publishMessage(successMessage('institution.save-success')));
}).catch((error) => {
dispatch(saveInstitutionError(error.response.data, institution, ACTION_FLAG.UPDATE_ENTITY));
dispatch(showServerResponseErrorMessage(error, 'institution.save-error'));
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions js/actions/InstitutionsActions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import * as ActionConstants from "../constants/ActionConstants";
import {axiosBackend} from "./index";
import {API_URL} from '../../config';
import {showServerResponseErrorMessage} from "./AsyncActionUtils";

export function loadInstitutions() {
//console.log("Loading all institutions");
return function (dispatch) {
dispatch(loadInstitutionsPending());
axiosBackend.get(`${API_URL}/rest/institutions`).then((response) => {
return axiosBackend.get(`${API_URL}/rest/institutions`).then((response) => {
dispatch(loadInstitutionsSuccess(response.data));
}).catch((error) => {
dispatch(loadInstitutionsError(error.response.data));
dispatch(showServerResponseErrorMessage(error, 'institutions.loading-error'));
});
}
}
Expand Down
58 changes: 31 additions & 27 deletions js/actions/RecordActions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import * as ActionConstants from "../constants/ActionConstants";
import {ACTION_FLAG, ACTION_STATUS} from "../constants/DefaultConstants";
import {ACTION_FLAG} from "../constants/DefaultConstants";
import {axiosBackend} from "./index";
import * as Utils from "../utils/Utils";
import {loadRecords} from "./RecordsActions";
import {API_URL} from '../../config';
import {publishMessage} from "./MessageActions";
import {errorMessage, successMessage} from "../model/Message";
import {asyncError, asyncRequest, asyncSuccess, showServerResponseErrorMessage} from "./AsyncActionUtils";

export function deleteRecord(record, currentUser) {
//console.log("Deleting record: ", record);
return function (dispatch) {
dispatch(deleteRecordPending(record.key));
axiosBackend.delete(`${API_URL}/rest/records/${record.key}`, {
return axiosBackend.delete(`${API_URL}/rest/records/${record.key}`, {
...record
}).then(() => {
dispatch(loadRecords(currentUser));
dispatch(deleteRecordSuccess(record, record.key));
dispatch(publishMessage(successMessage("record.delete-success")));
}).catch((error) => {
dispatch(deleteRecordError(error.response.data, record, record.key));
dispatch(showServerResponseErrorMessage(error, 'record.delete-error'));
});
}
}
Expand Down Expand Up @@ -45,13 +49,13 @@ export function deleteRecordError(error, record, key) {
}

export function loadRecord(key) {
//console.log("Loading record with key: ", key);
return function (dispatch) {
dispatch(loadRecordPending());
axiosBackend.get(`${API_URL}/rest/records/${key}`).then((response) => {
return axiosBackend.get(`${API_URL}/rest/records/${key}`).then((response) => {
dispatch(loadRecordSuccess(response.data));
}).catch((error) => {
dispatch(loadRecordError(error.response.data));
dispatch(showServerResponseErrorMessage(error, 'record.load-error'));
});
}
}
Expand Down Expand Up @@ -83,32 +87,34 @@ export function unloadRecord() {
}

export function createRecord(record, currentUser) {
//console.log("Creating record: ", record);
return function (dispatch) {
return function (dispatch, getState) {
dispatch(saveRecordPending(ACTION_FLAG.CREATE_ENTITY));
axiosBackend.post(`${API_URL}/rest/records`, {
return axiosBackend.post(`${API_URL}/rest/records`, {
...record
}).then((response) => {
const key = Utils.extractKeyFromLocationHeader(response);
dispatch(saveRecordSuccess(record, key, ACTION_FLAG.CREATE_ENTITY));
dispatch(loadRecords(currentUser));
dispatch(publishMessage(successMessage("record.save-success")));
}).catch((error) => {
dispatch(saveRecordError(error.response.data, record, ACTION_FLAG.CREATE_ENTITY));
dispatch(publishMessage(errorMessage('record.save-error', {error: getState().intl.messages[error.response.data.messageId]})));
});
}
}

export function updateRecord(record, currentUser) {
//console.log("Updating record: ", record);
return function (dispatch) {
return function (dispatch, getState) {
dispatch(saveRecordPending(ACTION_FLAG.UPDATE_ENTITY));
axiosBackend.put(`${API_URL}/rest/records/${record.key}`, {
return axiosBackend.put(`${API_URL}/rest/records/${record.key}`, {
...record
}).then((response) => {
}).then(() => {
dispatch(saveRecordSuccess(record, null, ACTION_FLAG.UPDATE_ENTITY));
dispatch(loadRecords(currentUser));
dispatch(publishMessage(successMessage("record.save-success")));
}).catch((error) => {
dispatch(saveRecordError(error.response.data, record, ACTION_FLAG.UPDATE_ENTITY));
dispatch(publishMessage(errorMessage('record.save-error', {error: getState().intl.messages[error.response.data.messageId]})));
});
}
}
Expand Down Expand Up @@ -144,20 +150,18 @@ export function unloadSavedRecord() {
}
}

export function loadFormgen(status, error = null) {
switch (status) {
case ACTION_STATUS.PENDING:
return {
type: ActionConstants.LOAD_FORMGEN_PENDING
};
case ACTION_STATUS.SUCCESS:
return {
type: ActionConstants.LOAD_FORMGEN_SUCCESS
};
case ACTION_STATUS.ERROR:
return {
type: ActionConstants.LOAD_FORMGEN_ERROR,
error
}
export function loadFormgen(record) {
return dispatch => {
dispatch(asyncRequest(ActionConstants.LOAD_FORMGEN_PENDING));
return axiosBackend.post(`${API_URL}/rest/formGen`, record)
.then(resp => {
dispatch(asyncSuccess(ActionConstants.LOAD_FORMGEN_SUCCESS));
return Promise.resolve(resp.data);
})
.catch(error => {
dispatch(asyncError(ActionConstants.LOAD_FORMGEN_ERROR, error));
dispatch(showServerResponseErrorMessage(error, 'record.load-form-error'));
return Promise.reject(error);
})
}
}
40 changes: 13 additions & 27 deletions js/actions/RecordsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import * as ActionConstants from "../constants/ActionConstants";
import {HttpHeaders, ROLE} from "../constants/DefaultConstants";
import {axiosBackend} from "./index";
import {API_URL} from '../../config';
import {asyncError, asyncRequest, asyncSuccess} from "./AsyncActionUtils";
import {asyncError, asyncRequest, asyncSuccess, showServerResponseErrorMessage} from "./AsyncActionUtils";
import {fileDownload} from "../utils/Utils";
import {publishMessage} from "./MessageActions";
import Message, {MessageType} from "../model/Message";
import {infoMessage, successMessage} from "../model/Message";

export function loadRecords(currentUser, institutionKey = null) {
//console.log("Loading records");
let urlSuffix = '';
if (institutionKey) {
urlSuffix = `?institution=${institutionKey}`;
Expand All @@ -17,10 +16,11 @@ export function loadRecords(currentUser, institutionKey = null) {
}
return function (dispatch) {
dispatch(loadRecordsPending());
axiosBackend.get(`${API_URL}/rest/records${urlSuffix}`).then((response) => {
return axiosBackend.get(`${API_URL}/rest/records${urlSuffix}`).then((response) => {
dispatch(loadRecordsSuccess(response.data));
}).catch((error) => {
dispatch(loadRecordsError(error.response.data));
dispatch(showServerResponseErrorMessage(error, 'records.loading-error'));
});
}
}
Expand Down Expand Up @@ -72,30 +72,16 @@ export function importRecords(file) {
dispatch(asyncSuccess(ActionConstants.IMPORT_RECORDS_SUCCESS));
dispatch(loadRecords(getState().auth.user));
if (resp.data.importedCount < resp.data.totalCount) {
dispatch(publishMessage(new Message({
messageId: "records.import.partialSuccess.message",
values: {
importedCount: resp.data.importedCount,
totalCount: resp.data.totalCount
},
type: MessageType.INFO
})))
dispatch(publishMessage(infoMessage("records.import.partialSuccess.message", {
importedCount: resp.data.importedCount,
totalCount: resp.data.totalCount
})));
} else {
dispatch(publishMessage(new Message({
messageId: "records.import.success.message",
values: {
importedCount: resp.data.importedCount
},
type: MessageType.SUCCESS
})))
dispatch(publishMessage(successMessage("records.import.success.message", {importedCount: resp.data.importedCount})));
}
})
.catch(error => {
dispatch(publishMessage(new Message({
messageId: "records.import.error.message",
type: MessageType.ERROR
})))
return dispatch(asyncError(ActionConstants.IMPORT_RECORDS_ERROR, error.response.data));
});
}).catch(error => {
dispatch(asyncError(ActionConstants.IMPORT_RECORDS_ERROR, error.response.data));
dispatch(showServerResponseErrorMessage(error, "records.import.error.message"));
});
};
}
5 changes: 3 additions & 2 deletions js/actions/StatisticsActions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {axiosBackend} from "./index";
import {LOAD_STATISTICS_ERROR, LOAD_STATISTICS_PENDING, LOAD_STATISTICS_SUCCESS} from "../constants/ActionConstants";
import {API_URL} from '../../config';
import {showServerResponseErrorMessage} from "./AsyncActionUtils";

export function loadStatistics() {
//console.log("Loading statistics: ");
return function (dispatch) {
dispatch({type: LOAD_STATISTICS_PENDING});
axiosBackend.get(`${API_URL}/rest/statistics`).then((response) => {
return axiosBackend.get(`${API_URL}/rest/statistics`).then((response) => {
dispatch({type: LOAD_STATISTICS_SUCCESS, payload: response.data});
}).catch((error) => {
dispatch({type: LOAD_STATISTICS_ERROR, error: error.response.data});
dispatch(showServerResponseErrorMessage(error, 'history.loading-error'));
});
}
}
Loading

0 comments on commit 41d16c4

Please sign in to comment.