diff --git a/src/actions.js b/src/actions.js index 08bc47f..d52bff5 100644 --- a/src/actions.js +++ b/src/actions.js @@ -4,6 +4,7 @@ import { formatPageQueryWithCount, formatMutation, formatGQLString, + formatQuery, } from '@openimis/fe-core'; import { ACTION_TYPE } from './reducer'; import { @@ -84,6 +85,11 @@ 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 7f0983c..d8142c9 100644 --- a/src/components/IndividualSearcher.js +++ b/src/components/IndividualSearcher.js @@ -25,14 +25,14 @@ import { import EditIcon from '@material-ui/icons/Edit'; import DeleteIcon from '@material-ui/icons/Delete'; import { - fetchIndividuals, deleteIndividual, downloadIndividuals, clearIndividualExport, + fetchIndividuals, deleteIndividual, downloadIndividuals, clearIndividualExport, fetchBenefitPlanSchemaFields, } 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_INDIVIDUAL_DELETE, BENEFIT_PLAN_LABEL, SOCIAL_PROTECTION_MODULE_NAME, RIGHT_SCHEMA_SEARCH, } from '../constants'; import { applyNumberCircle } from '../util/searcher-utils'; import IndividualFilter from './IndividualFilter'; @@ -61,13 +61,36 @@ function IndividualSearcher({ downloadIndividuals, individualExport, errorIndividualExport, + fieldsFromBfSchema, + fetchingFieldsFromBfSchema, + fetchedFieldsFromBfSchema, + fetchBenefitPlanSchemaFields, }) { const [individualToDelete, setIndividualToDelete] = useState(null); const [appliedCustomFilters, setAppliedCustomFilters] = useState([CLEARED_STATE_FILTER]); const [appliedFiltersRowStructure, setAppliedFiltersRowStructure] = useState([CLEARED_STATE_FILTER]); const [deletedIndividualUuids, setDeletedIndividualUuids] = useState([]); + const [exportFields, setExportFields] = useState([ + 'id', + 'first_name', + 'last_name', + 'dob', + ]); + const exportFieldsColumns = { + id: 'ID', + first_name: formatMessage(intl, 'individual', 'export.firstName'), + last_name: formatMessage(intl, 'individual', 'export.lastName'), + dob: formatMessage(intl, 'individual', 'export.dob'), + }; const prevSubmittingMutationRef = useRef(); + useEffect(() => { + if (!fetchedFieldsFromBfSchema && !fetchingFieldsFromBfSchema && rights.includes(RIGHT_SCHEMA_SEARCH)) { + fetchBenefitPlanSchemaFields(['bfType: INDIVIDUAL']); + } + setExportFields([...exportFields, ...fieldsFromBfSchema]); + }, [fetchedFieldsFromBfSchema, fetchingFieldsFromBfSchema]); + function individualUpdatePageUrl(individual) { return `${modulesManager.getRef('individual.route.individual')}/${individual?.id}`; } @@ -241,19 +264,8 @@ function IndividualSearcher({ appliedFiltersRowStructure={appliedFiltersRowStructure} setAppliedFiltersRowStructure={setAppliedFiltersRowStructure} applyNumberCircle={applyNumberCircle} - exportFields={[ - 'id', - 'first_name', - 'last_name', - 'dob', - 'json_ext', // Unfolded by backend and removed from csv - ]} - exportFieldsColumns={{ - id: 'ID', - first_name: formatMessage(intl, 'individual', 'export.firstName'), - last_name: formatMessage(intl, 'individual', 'export.lastName'), - dob: formatMessage(intl, 'individual', 'export.dob'), - }} + exportFields={exportFields} + exportFieldsColumns={exportFieldsColumns} exportFieldLabel={formatMessage(intl, 'individual', 'export.label')} chooseExportableColumns cacheFiltersKey="individualsFilterCache" @@ -289,6 +301,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, }); const mapDispatchToProps = (dispatch) => bindActionCreators( @@ -297,6 +313,7 @@ const mapDispatchToProps = (dispatch) => bindActionCreators( deleteIndividual, downloadIndividuals, clearIndividualExport, + fetchBenefitPlanSchemaFields, coreConfirm, clearConfirm, journalize, diff --git a/src/constants.js b/src/constants.js index 7367724..a05e04b 100644 --- a/src/constants.js +++ b/src/constants.js @@ -20,6 +20,8 @@ export const RIGHT_GROUP_CREATE = 180002; export const RIGHT_GROUP_UPDATE = 180003; export const RIGHT_GROUP_DELETE = 180004; +export const RIGHT_SCHEMA_SEARCH = 171001; + export const BENEFIT_PLANS_LIST_TAB_VALUE = 'BenefitPlansListTab'; export const INDIVIDUALS_LIST_TAB_VALUE = 'IndividualsListTab'; export const INDIVIDUAL_CHANGELOG_TAB_VALUE = 'IndividualChangelogTab'; diff --git a/src/reducer.js b/src/reducer.js index 067a49b..47d4c6d 100644 --- a/src/reducer.js +++ b/src/reducer.js @@ -33,6 +33,7 @@ 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( @@ -92,10 +93,22 @@ 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, @@ -162,6 +175,14 @@ 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, @@ -268,6 +289,12 @@ 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,