Skip to content

Commit

Permalink
CM-359: move fetching logic to social_protection module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan committed Jan 11, 2024
1 parent 53b6586 commit 6f4c08f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
5 changes: 0 additions & 5 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ export function fetchGroupHistory(params) {
return graphql(payload, ACTION_TYPE.SEARCH_GROUP_HISTORY);
}

export function fetchBenefitPlanSchemaFields(params) {
const payload = formatQuery('benefitPlanSchemaField', params, ['schemaFields']);
return graphql(payload, ACTION_TYPE.GET_FIELDS_FROM_BF_SCHEMA);
}

export function deleteIndividual(individual, clientMutationLabel) {
const individualUuids = `ids: ["${individual?.id}"]`;
const mutation = formatMutation('deleteIndividual', individualUuids, clientMutationLabel);
Expand Down
39 changes: 26 additions & 13 deletions src/components/IndividualSearcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
CLEARED_STATE_FILTER,
} from '@openimis/fe-core';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { connect, useDispatch } from 'react-redux';
import {
IconButton, Tooltip, Button,
Dialog,
Expand All @@ -25,14 +25,18 @@ import {
import EditIcon from '@material-ui/icons/Edit';
import DeleteIcon from '@material-ui/icons/Delete';
import {
fetchIndividuals, deleteIndividual, downloadIndividuals, clearIndividualExport, fetchBenefitPlanSchemaFields,
fetchIndividuals, deleteIndividual, downloadIndividuals, clearIndividualExport,
} from '../actions';
import {
DEFAULT_PAGE_SIZE,
ROWS_PER_PAGE_OPTIONS,
EMPTY_STRING,
RIGHT_INDIVIDUAL_UPDATE,
RIGHT_INDIVIDUAL_DELETE, BENEFIT_PLAN_LABEL, SOCIAL_PROTECTION_MODULE_NAME, RIGHT_SCHEMA_SEARCH,
RIGHT_INDIVIDUAL_DELETE,
BENEFIT_PLAN_LABEL,
SOCIAL_PROTECTION_MODULE_NAME,
RIGHT_SCHEMA_SEARCH,
FETCH_BENEFIT_PLAN_SCHEMA_FIELDS_REF,
} from '../constants';
import { applyNumberCircle } from '../util/searcher-utils';
import IndividualFilter from './IndividualFilter';
Expand Down Expand Up @@ -64,8 +68,8 @@ function IndividualSearcher({
fieldsFromBfSchema,
fetchingFieldsFromBfSchema,
fetchedFieldsFromBfSchema,
fetchBenefitPlanSchemaFields,
}) {
const dispatch = useDispatch();
const [individualToDelete, setIndividualToDelete] = useState(null);
const [appliedCustomFilters, setAppliedCustomFilters] = useState([CLEARED_STATE_FILTER]);
const [appliedFiltersRowStructure, setAppliedFiltersRowStructure] = useState([CLEARED_STATE_FILTER]);
Expand All @@ -85,11 +89,21 @@ function IndividualSearcher({
const prevSubmittingMutationRef = useRef();

useEffect(() => {
if (!fetchedFieldsFromBfSchema && !fetchingFieldsFromBfSchema && rights.includes(RIGHT_SCHEMA_SEARCH)) {
fetchBenefitPlanSchemaFields(['bfType: INDIVIDUAL']);
const canFetchBenefitPlanSchemaFields = !fetchedFieldsFromBfSchema
&& !fetchingFieldsFromBfSchema
&& rights.includes(RIGHT_SCHEMA_SEARCH);

if (canFetchBenefitPlanSchemaFields) {
const fetchBenefitPlanSchemaFields = modulesManager.getRef(FETCH_BENEFIT_PLAN_SCHEMA_FIELDS_REF);
if (fetchBenefitPlanSchemaFields) {
dispatch(fetchBenefitPlanSchemaFields(['bfType: INDIVIDUAL']));
}
}

if (!canFetchBenefitPlanSchemaFields) {
setExportFields([...exportFields, ...fieldsFromBfSchema]);
}
setExportFields([...exportFields, ...fieldsFromBfSchema]);
}, [fetchedFieldsFromBfSchema, fetchingFieldsFromBfSchema]);
}, [fetchedFieldsFromBfSchema, fetchingFieldsFromBfSchema, rights, modulesManager]);

function individualUpdatePageUrl(individual) {
return `${modulesManager.getRef('individual.route.individual')}/${individual?.id}`;
Expand Down Expand Up @@ -301,10 +315,10 @@ const mapStateToProps = (state) => ({
individualExport: state.individual.individualExport,
individualExportPageInfo: state.individual.individualExportPageInfo,
errorIndividualExport: state.individual.errorIndividualExport,
fieldsFromBfSchema: state.individual.fieldsFromBfSchema,
fetchingFieldsFromBfSchema: state.individual.fetchingFieldsFromBfSchema,
fetchedFieldsFromBfSchema: state.individual.fetchedFieldsFromBfSchema,
errorFieldsFromBfSchema: state.individual.errorFieldsFromBfSchema,
fieldsFromBfSchema: state?.socialProtection?.fieldsFromBfSchema,
fetchingFieldsFromBfSchema: state?.socialProtection?.fetchingFieldsFromBfSchema,
fetchedFieldsFromBfSchema: state?.socialProtection?.fetchedFieldsFromBfSchema,
errorFieldsFromBfSchema: state?.socialProtection?.errorFieldsFromBfSchema,
});

const mapDispatchToProps = (dispatch) => bindActionCreators(
Expand All @@ -313,7 +327,6 @@ const mapDispatchToProps = (dispatch) => bindActionCreators(
deleteIndividual,
downloadIndividuals,
clearIndividualExport,
fetchBenefitPlanSchemaFields,
coreConfirm,
clearConfirm,
journalize,
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ export const GROUP_INDIVIDUAL_ROLES_LIST = [
export const BENEFIT_PLAN_LABEL = 'BenefitPlan';

export const SOCIAL_PROTECTION_MODULE_NAME = 'social_protection';

export const FETCH_BENEFIT_PLAN_SCHEMA_FIELDS_REF = 'socialProtection.fetchBenefitPlanSchemaFields';
27 changes: 0 additions & 27 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const ACTION_TYPE = {
GROUP_INDIVIDUAL_EXPORT: 'GROUP_INDIVIDUAL_EXPORT',
SEARCH_INDIVIDUAL_HISTORY: 'SEARCH_INDIVIDUAL_HISTORY',
SEARCH_GROUP_HISTORY: 'SEARCH_GROUP_HISTORY',
GET_FIELDS_FROM_BF_SCHEMA: 'GET_FIELDS_FROM_BF_SCHEMA',
};

function reducer(
Expand Down Expand Up @@ -93,22 +92,10 @@ function reducer(
groupHistory: [],
groupHistoryPageInfo: {},
groupHistoryTotalCount: 0,
fieldsFromBfSchema: [],
fetchingFieldsFromBfSchema: false,
fetchedFieldsFromBfSchema: false,
errorFieldsFromBfSchema: null,
},
action,
) {
switch (action.type) {
case REQUEST(ACTION_TYPE.GET_FIELDS_FROM_BF_SCHEMA):
return {
...state,
fieldsFromBfSchema: [],
fetchingFieldsFromBfSchema: true,
fetchedFieldsFromBfSchema: false,
errorFieldsFromBfSchema: null,
};
case REQUEST(ACTION_TYPE.SEARCH_INDIVIDUALS):
return {
...state,
Expand Down Expand Up @@ -175,14 +162,6 @@ function reducer(
groupHistoryTotalCount: 0,
errorGroupHistory: null,
};
case SUCCESS(ACTION_TYPE.GET_FIELDS_FROM_BF_SCHEMA):
return {
...state,
fieldsFromBfSchema: action?.payload?.data?.benefitPlanSchemaField?.schemaFields || [],
fetchingFieldsFromBfSchema: false,
fetchedFieldsFromBfSchema: true,
errorFieldsFromBfSchema: formatGraphQLError(action.payload),
};
case SUCCESS(ACTION_TYPE.SEARCH_INDIVIDUALS):
return {
...state,
Expand Down Expand Up @@ -289,12 +268,6 @@ function reducer(
}))?.[0],
errorGroup: null,
};
case ERROR(ACTION_TYPE.GET_FIELDS_FROM_BF_SCHEMA):
return {
...state,
fetchingFieldsFromBfSchema: false,
errorFieldsFromBfSchema: formatGraphQLError(action.payload),
};
case ERROR(ACTION_TYPE.SEARCH_INDIVIDUALS):
return {
...state,
Expand Down

0 comments on commit 6f4c08f

Please sign in to comment.