Skip to content

Commit

Permalink
Merge pull request #204 from kbss-cvut/203-available-status-list
Browse files Browse the repository at this point in the history
[#203] Add loadRecordsPhases action to retrieve available phase from …
  • Loading branch information
blcham authored Aug 12, 2024
2 parents f448900 + 1296804 commit 379c7ca
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
29 changes: 29 additions & 0 deletions src/actions/RecordsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,32 @@ export function importRecords(file) {
});
};
}

export function loadRecordsPhases() {
return function (dispatch) {
dispatch(loadRecordsPhasesPending());
return axiosBackend
.get(`${API_URL}/rest/records/used-record-phases`)
.then((response) => {
dispatch(loadRecordsPhasesSuccess(response.data));
})
.catch((error) => {
dispatch(loadRecordsPhasesError(error.response.data));
});
};
}

export function loadRecordsPhasesPending() {
return asyncRequest(ActionConstants.LOAD_RECORDS_PHASES_PENDING);
}

export function loadRecordsPhasesSuccess(phases) {
return {
type: ActionConstants.LOAD_RECORDS_PHASES_SUCCESS,
phases,
};
}

export function loadRecordsPhasesError(error) {
return asyncError(ActionConstants.LOAD_RECORDS_PHASES_ERROR, error);
}
9 changes: 8 additions & 1 deletion src/components/record/RecordsController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import React from "react";
import Records from "./Records";
import Routes from "../../constants/RoutesConstants";
import { transitionToWithOpts } from "../../utils/Routing";
import { exportRecords, importRecords, loadRecords, publishRecords } from "../../actions/RecordsActions";
import {
exportRecords,
importRecords,
loadRecords,
loadRecordsPhases,
publishRecords,
} from "../../actions/RecordsActions";
import { injectIntl } from "react-intl";
import withI18n from "../../i18n/withI18n";
import { connect } from "react-redux";
Expand Down Expand Up @@ -192,6 +198,7 @@ function mapDispatchToProps(dispatch) {
deleteRecord: bindActionCreators(deleteRecord, dispatch),
updateRecord: bindActionCreators(updateRecord, dispatch),
loadRecords: bindActionCreators(loadRecords, dispatch),
loadRecordsPhase: bindActionCreators(loadRecordsPhases, dispatch),
exportRecords: bindActionCreators(exportRecords, dispatch),
publishRecords: bindActionCreators(publishRecords, dispatch),
importRecords: bindActionCreators(importRecords, dispatch),
Expand Down
30 changes: 21 additions & 9 deletions src/components/record/filter/PhaseFilter.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import React from "react";
import React, { useEffect } from "react";
import { Button, Col, Form, Row } from "react-bootstrap";
import { IntelligentTreeSelect } from "intelligent-tree-select";
import { RECORD_PHASE } from "../../../constants/DefaultConstants";
import { useI18n } from "../../../hooks/useI18n";
import { sanitizeArray } from "../../../utils/Utils";
import PropTypes from "prop-types";
import { useDispatch, useSelector } from "react-redux";
import { loadRecordsPhases } from "../../../actions/RecordsActions.js";

const PhaseFilter = ({ value, onChange }) => {
const { i18n } = useI18n();
const options = React.useMemo(
() =>
Object.keys(RECORD_PHASE).map((phase) => ({
label: i18n("records.completion-status." + RECORD_PHASE[phase]),
value: phase,
})),
[i18n],
);

const phases = useSelector((state) => state.records.recordsPhases.data);

const dispatch = useDispatch();
useEffect(() => {
dispatch(loadRecordsPhases());
}, [dispatch]);

const options = React.useMemo(() => {
if (!phases) {
return [];
}
return phases.map((phase) => ({
label: i18n("records.completion-status." + phase),
value: phase,
}));
}, [phases, i18n]);

const values = sanitizeArray(value);
const selected = options.filter((o) => values.indexOf(o.value) !== -1);
return (
Expand Down
4 changes: 4 additions & 0 deletions src/constants/ActionConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,7 @@ export const IMPERSONATE_LOGOUT_SUCCESS = "IMPERSONATE_LOGOUT_SUCCESS";

export const PUBLISH_MESSAGE = "PUBLISH_MESSAGE";
export const DISMISS_MESSAGE = "DISMISS_MESSAGE";

export const LOAD_RECORDS_PHASES_PENDING = "LOAD_PHASES_PENDING";
export const LOAD_RECORDS_PHASES_SUCCESS = "LOAD_PHASES_SUCCESS";
export const LOAD_RECORDS_PHASES_ERROR = "LOAD_PHASES_ERROR";
26 changes: 26 additions & 0 deletions src/reducers/RecordsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ACTION_STATUS } from "../constants/DefaultConstants";

const initialState = {
recordsLoaded: {},
recordsPhases: {},
};

export default function (state = initialState, action) {
Expand Down Expand Up @@ -33,6 +34,31 @@ export default function (state = initialState, action) {
error: action.error,
},
};
case ActionConstants.LOAD_RECORDS_PHASES_PENDING:
return {
...state,
recordsPhases: {
...state.phases,
status: ACTION_STATUS.PENDING,
},
};
case ActionConstants.LOAD_RECORDS_PHASES_SUCCESS:
return {
...state,
recordsPhases: {
status: ACTION_STATUS.SUCCESS,
data: action.phases,
error: "",
},
};
case ActionConstants.LOAD_RECORDS_PHASES_ERROR:
return {
...state,
recordsPhases: {
status: ACTION_STATUS.ERROR,
error: action.error,
},
};
default:
return state;
}
Expand Down

0 comments on commit 379c7ca

Please sign in to comment.