Skip to content

Commit

Permalink
#1273 - Encounter edit and cancel uses EditRule.
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Feb 26, 2024
1 parent 21458ca commit 0490adc
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {Encounter, Privilege} from "avni-models";
import Colors from "../../views/primitives/Colors";
import PrivilegeService from "../../service/PrivilegeService";
import DraftEncounterService from '../../service/draft/DraftEncounterService';
import {firebaseEvents, logEvent} from "../../utility/Analytics";
import {Form} from "openchs-models";
import {EditFormRuleResponse} from "rules-config";

export class IndividualGeneralHistoryActions {
static getInitialState() {
Expand Down Expand Up @@ -94,6 +97,26 @@ export class IndividualGeneralHistoryActions {
draftEncounters
};
}

static onEditEncounter(state, action, context) {
logEvent(firebaseEvents.EDIT_ENCOUNTER);
const formType = action.cancel ? Form.formTypes.IndividualEncounterCancellation : Form.formTypes.Encounter;
const form = context.get(FormMappingService).findFormForEncounterType(action.encounter.encounterType, formType, state.individual.subjectType);
const editFormRuleResponse = context.get(RuleEvaluationService).runEditFormRule(form, action.encounter, 'Encounter');

if (editFormRuleResponse.isEditAllowed()) {
action.onEncounterEditAllowed();
return state;
} else {
const newState = {...state};
newState.editFormRuleResponse = editFormRuleResponse;
return newState;
}
}

static onEditErrorShown(state) {
return {...state, editFormRuleResponse: EditFormRuleResponse.createEditAllowedResponse()}
}
}

const actions = {
Expand All @@ -103,6 +126,9 @@ const actions = {
HIDE_ENCOUNTER_SELECTOR: "IGHA.HIDE_ENCOUNTER_SELECTOR",
LAUNCH_ENCOUNTER_SELECTOR: "IGHA.LAUNCH_ENCOUNTER_SELECTOR",
DELETE_DRAFT: "IGHA.DELETE_DRAFT",
ON_EDIT_ENCOUNTER: "IGHA.ON_EDIT_ENCOUNTER",
ON_EDIT_ERROR_SHOWN: "IGHA.ON_EDIT_ERROR_SHOWN"

};

export default new Map([
Expand All @@ -112,6 +138,8 @@ export default new Map([
[actions.HIDE_ENCOUNTER_SELECTOR, IndividualGeneralHistoryActions.hideEncounterSelector],
[actions.LAUNCH_ENCOUNTER_SELECTOR, IndividualGeneralHistoryActions.launchEncounterSelector],
[actions.DELETE_DRAFT, IndividualGeneralHistoryActions.deleteDraft],
[actions.ON_EDIT_ENCOUNTER, IndividualGeneralHistoryActions.onEditEncounter],
[actions.ON_EDIT_ERROR_SHOWN, IndividualGeneralHistoryActions.onEditErrorShown],
]);

export {actions as Actions};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ProgramEnrolmentService from "../../service/ProgramEnrolmentService";
import IndividualService from "../../service/IndividualService";
import {firebaseEvents, logEvent} from "../../utility/Analytics";
import {EditFormRuleResponse} from "rules-config";
import {Form} from "openchs-models";

class ProgramEnrolmentDashboardActions {
static setEncounterType(encounterType) {
Expand Down Expand Up @@ -199,6 +200,22 @@ class ProgramEnrolmentDashboardActions {
return state;
}

static onEditProgramEncounter(state, action, context) {
logEvent(firebaseEvents.EDIT_PROGRAM_ENCOUNTER);
const formType = action.cancel ? Form.formTypes.ProgramEncounterCancellation : Form.formTypes.ProgramEncounter;
const form = context.get(FormMappingService).findFormForEncounterType(action.encounter.encounterType, formType, state.enrolment.individual.subjectType);
const editFormRuleResponse = context.get(RuleEvaluationService).runEditFormRule(form, action.encounter, 'ProgramEncounter');

if (editFormRuleResponse.isEditAllowed()) {
action.onProgramEncounterEditAllowed();
return state;
} else {
const newState = {...state};
newState.editFormRuleResponse = editFormRuleResponse;
return newState;
}
}

static onEditErrorShown(state) {
return {...state, editFormRuleResponse: EditFormRuleResponse.createEditAllowedResponse()}
}
Expand Down Expand Up @@ -319,6 +336,7 @@ const ProgramEnrolmentDashboardActionsNames = {
ON_LANDING: 'PEDA.ON_LANDING',
ON_FOCUS: 'PEDA.ON_FOCUS',
ON_EDIT_ENROLMENT: 'PEDA.ON_EDIT_ENROLMENT',
ON_EDIT_PROGRAM_ENCOUNTER: 'PEDA.ON_EDIT_PROGRAM_ENCOUNTER',
ON_EDIT_ENROLMENT_EXIT: 'PEDA.ON_EDIT_ENROLMENT_EXIT',
ON_EXIT_ENROLMENT: 'PEDA.ON_EXIT_ENROLMENT',
ON_ENROLMENT_CHANGE: 'PEDA.ON_ENROLMENT_CHANGE',
Expand All @@ -342,6 +360,7 @@ const ProgramEnrolmentDashboardActionsMap = new Map([
[ProgramEnrolmentDashboardActionsNames.RESET, ProgramEnrolmentDashboardActions.getInitialState],
[ProgramEnrolmentDashboardActionsNames.SHOW_MORE, ProgramEnrolmentDashboardActions.onShowMore],
[ProgramEnrolmentDashboardActionsNames.ON_EDIT_ENROLMENT, ProgramEnrolmentDashboardActions.onEditEnrolment],
[ProgramEnrolmentDashboardActionsNames.ON_EDIT_PROGRAM_ENCOUNTER, ProgramEnrolmentDashboardActions.onEditProgramEncounter],
[ProgramEnrolmentDashboardActionsNames.ON_EXIT_ENROLMENT, ProgramEnrolmentDashboardActions.onExitEnrolment],
[ProgramEnrolmentDashboardActionsNames.ON_EDIT_ENROLMENT_EXIT, ProgramEnrolmentDashboardActions.onEditEnrolmentExit],
[ProgramEnrolmentDashboardActionsNames.ON_EDIT_ERROR_SHOWN, ProgramEnrolmentDashboardActions.onEditErrorShown],
Expand Down
20 changes: 10 additions & 10 deletions packages/openchs-android/src/encounter/CompletedEncountersView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {CompletedEncountersActionNames as Actions} from "../action/encounter/Com
import SearchResultsHeader from "../views/individual/SearchResultsHeader";
import CompletedVisitsFilterView from "../views/filter/CompletedVisitsFilterView";
import TypedTransition from "../framework/routing/TypedTransition";
import CollapsibleEncounters from "../views/common/CollapsibleEncounters";
import CollapsibleEncounter from "../views/common/CollapsibleEncounter";
import DGS from "../views/primitives/DynamicGlobalStyles";
import Separator from "../views/primitives/Separator";

Expand Down Expand Up @@ -64,15 +64,15 @@ class CompletedEncountersView extends AbstractComponent {
data={chronologicalEncounters}
renderItem={({item: encounter}) =>
<View style={styles.container}>
<CollapsibleEncounters encountersInfo={encounter}
onToggleAction={Actions.ON_EXPAND_TOGGLE}
renderTitleAndDetails={this.props.params.renderTitleAndDetails.bind(this, encounter.encounter)}
encounterActions={this.props.params.encounterActions.bind(this, encounter.encounter)}
cancelVisitAction={this.props.params.cancelVisitAction.bind(this, encounter.encounter)}
isEditAllowed={this.props.params.isEditAllowed.bind(this, encounter.encounter)}
style={styles.textContainer}
formType={this.props.params.formType}
cancelFormType={this.props.params.cancelFormType}/>
<CollapsibleEncounter encountersInfo={encounter}
onToggleAction={Actions.ON_EXPAND_TOGGLE}
renderTitleAndDetails={this.props.params.renderTitleAndDetails.bind(this, encounter.encounter)}
encounterActions={this.props.params.encounterActions.bind(this, encounter.encounter)}
cancelVisitAction={this.props.params.cancelVisitAction.bind(this, encounter.encounter)}
isEditAllowed={this.props.params.isEditAllowed.bind(this, encounter.encounter)}
style={styles.textContainer}
formType={this.props.params.formType}
cancelFormType={this.props.params.cancelFormType}/>
</View>}
initialNumToRender={15}
updateCellsBatchingPeriod={500}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import Observations from "./Observations";
import _ from "lodash";
import CHSNavigator from "../../utility/CHSNavigator";

class CollapsibleEncounters extends AbstractComponent {

class CollapsibleEncounter extends AbstractComponent {
static propTypes = {
encountersInfo: PropTypes.any.isRequired,
onToggleAction: PropTypes.string.isRequired,
Expand Down Expand Up @@ -69,4 +68,4 @@ class CollapsibleEncounters extends AbstractComponent {
}
}

export default CollapsibleEncounters
export default CollapsibleEncounter
28 changes: 17 additions & 11 deletions packages/openchs-android/src/views/common/PreviousEncounters.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Distances from '../primitives/Distances';
import ObservationsSectionOptions from '../common/ObservationsSectionOptions';
import TypedTransition from '../../framework/routing/TypedTransition';
import CompletedEncountersView from '../../encounter/CompletedEncountersView';
import CollapsibleEncounters from './CollapsibleEncounters';
import CollapsibleEncounter from './CollapsibleEncounter';
import PrivilegeService from '../../service/PrivilegeService';
import ListViewHelper from '../../utility/ListViewHelper';
import UserInfoService from "../../service/UserInfoService";
Expand All @@ -42,7 +42,12 @@ class PreviousEncounters extends AbstractComponent {
onToggleAction: PropTypes.string,
containsDrafts: PropTypes.bool,
deleteDraft: PropTypes.func,
hideIfEmpty: PropTypes.bool
hideIfEmpty: PropTypes.bool,
onEdit: PropTypes.func
};

static defaultProps = {
onEdit: _.noop
};

constructor(props, context) {
Expand All @@ -54,10 +59,11 @@ class PreviousEncounters extends AbstractComponent {
encounter = encounter.cloneForEdit();
const editing = !encounter.isScheduled();
encounter.encounterDateTime = _.isNil(encounter.encounterDateTime) ? new Date() : encounter.encounterDateTime;
CHSNavigator.navigateToEncounterView(this, {encounter, editing});
this.props.onEdit({encounter, editing});
}

cancelEncounter(encounter) {
this.props.onEdit({encounter, cancel: true});
CHSNavigator.navigateToEncounterView(this, {encounter, cancel: true});
}

Expand Down Expand Up @@ -229,14 +235,14 @@ class PreviousEncounters extends AbstractComponent {
removeClippedSubviews={true}
renderRow={(encounter) => <View style={styles.container}>
{this.props.expandCollapseView ?
<CollapsibleEncounters encountersInfo={encounter}
onToggleAction={this.props.onToggleAction}
renderTitleAndDetails={() => this.renderTitleAndDetails(encounter.encounter)}
encounterActions={() => this.encounterActions(encounter.encounter)}
cancelVisitAction={() => this.cancelVisitAction(encounter.encounter)}
formType={this.props.formType}
cancelFormType={this.props.cancelFormType}
isEditAllowed={() => this.isEditAllowed(encounter.encounter)}
<CollapsibleEncounter encountersInfo={encounter}
onToggleAction={this.props.onToggleAction}
renderTitleAndDetails={() => this.renderTitleAndDetails(encounter.encounter)}
encounterActions={() => this.encounterActions(encounter.encounter)}
cancelVisitAction={() => this.cancelVisitAction(encounter.encounter)}
formType={this.props.formType}
cancelFormType={this.props.cancelFormType}
isEditAllowed={() => this.isEditAllowed(encounter.encounter)}
/>
: this.renderNormalView(encounter)}
</View>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import CHSNavigator from "../../utility/CHSNavigator";
import ActionSelector from "../common/ActionSelector";
import PrivilegeService from "../../service/PrivilegeService";
import NewFormButton from "../common/NewFormButton";
import AvniToast from "../common/AvniToast";

class SubjectDashboardGeneralTab extends AbstractComponent {
static propTypes = {
Expand Down Expand Up @@ -96,11 +97,15 @@ class SubjectDashboardGeneralTab extends AbstractComponent {
emptyTitle={this.I18n.t('noEncounters')}
expandCollapseView={true}
subjectInfo={this.state.individual.name}
onToggleAction={Actions.ON_TOGGLE}/>);
onToggleAction={Actions.ON_TOGGLE}
onEdit={({encounter, ...others}) => this.dispatchAction(Actions.ON_EDIT_ENCOUNTER, {
encounter: encounter,
onEncounterEditAllowed: () => CHSNavigator.navigateToEncounterView(this, {encounter, ...others})
})}
/>);
}

render() {

return (
<View style={{backgroundColor: Colors.GreyContentBackground, marginTop: 10}}>
<ActionSelector
Expand All @@ -116,6 +121,8 @@ class SubjectDashboardGeneralTab extends AbstractComponent {
{this.renderCompletedVisits()}
</View>
<Separator height={110} backgroundColor={Colors.GreyContentBackground}/>
{this.state.editFormRuleResponse.isEditDisallowed() &&
<AvniToast message={this.I18n.t(this.state.editFormRuleResponse.getMessageKey())} onAutoClose={() => this.dispatchAction(Actions.ON_EDIT_ERROR_SHOWN)}/>}
</View>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Alert, ScrollView, ToastAndroid, TouchableOpacity, View} from "react-native";
import {Alert, ScrollView, TouchableOpacity, View} from "react-native";
import PropTypes from 'prop-types';
import React from "react";
import AbstractComponent from "../../framework/view/AbstractComponent";
Expand Down Expand Up @@ -179,6 +179,11 @@ class SubjectDashboardProgramsTab extends AbstractComponent {
expandCollapseView={true}
onToggleAction={Actions.ON_ENCOUNTER_TOGGLE}
subjectInfo={`${programEnrolment.individual.name}, ${programEnrolment.program.displayName}`}
onEdit={({encounter, ...others}) => this.dispatchAction(Actions.ON_EDIT_PROGRAM_ENCOUNTER, {
encounter: encounter,
cancel: others.cancel,
onProgramEncounterEditAllowed: () => CHSNavigator.navigateToEncounterView(this, {encounter, ...others})
})}
/>);
}

Expand Down

0 comments on commit 0490adc

Please sign in to comment.