diff --git a/src/actions.js b/src/actions.js index d52bff5..5e24c9e 100644 --- a/src/actions.js +++ b/src/actions.js @@ -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); diff --git a/src/components/IndividualSearcher.js b/src/components/IndividualSearcher.js index d8142c9..fb35e86 100644 --- a/src/components/IndividualSearcher.js +++ b/src/components/IndividualSearcher.js @@ -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, @@ -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'; @@ -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]); @@ -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}`; @@ -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( @@ -313,7 +327,6 @@ const mapDispatchToProps = (dispatch) => bindActionCreators( deleteIndividual, downloadIndividuals, clearIndividualExport, - fetchBenefitPlanSchemaFields, coreConfirm, clearConfirm, journalize, diff --git a/src/constants.js b/src/constants.js index a05e04b..89491c3 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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'; diff --git a/src/reducer.js b/src/reducer.js index 47d4c6d..067a49b 100644 --- a/src/reducer.js +++ b/src/reducer.js @@ -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( @@ -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, @@ -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, @@ -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,