Skip to content

Commit

Permalink
refactor. incorporate PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
abinaya-u committed Mar 7, 2024
1 parent cda449a commit 64cc3bf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 59 deletions.
87 changes: 32 additions & 55 deletions micro-frontends/src/next-ui/Components/AddAllergy/AddAllergy.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import React, {Fragment, useEffect} from "react";
import propTypes from "prop-types";
import { Close24 } from "@carbon/icons-react";
import SaveAndCloseButtons from "../SaveAndCloseButtons/SaveAndCloseButtons.jsx";
import "./AddAllergy.scss";
import "../../../styles/common.scss";
import { SearchAllergen } from "../SearchAllergen/SearchAllergen.jsx";
import { ArrowLeft } from "@carbon/icons-react/next";
import { RadioButton, RadioButtonGroup, TextArea } from "carbon-components-react";
import { isEmpty } from "lodash";
import {
RadioButton,
RadioButtonGroup,
TextArea,
} from "carbon-components-react";
import propTypes from "prop-types";
import React, { Fragment, useEffect } from "react";
import { FormattedMessage } from "react-intl";
import { ArrowLeft } from "@carbon/icons-react/next";
import "../../../styles/common.scss";
import {
saveAllergiesAPICall
} from "../../utils/PatientAllergiesControl/AllergyControlUtils";
import SaveAndCloseButtons from "../SaveAndCloseButtons/SaveAndCloseButtons.jsx";
import { SearchAllergen } from "../SearchAllergen/SearchAllergen.jsx";
import { SelectReactions } from "../SelectReactions/SelectReactions";
import { bahmniEncounter, getEncounterType } from "../../utils/PatientAllergiesControl/AllergyControlUtils";
import { getCookies } from "../../utils/cookieHandler/cookieHandler";
import "./AddAllergy.scss";

export function AddAllergy(props) {
const { patient, provider, onClose, allergens, reaction, severityOptions, onSave } = props;
Expand All @@ -24,16 +21,10 @@ export function AddAllergy(props) {
const [severity, setSeverity] = React.useState("");
const [notes, setNotes] = React.useState("");
const backToAllergenText = (
<FormattedMessage
id={"BACK_TO_ALLERGEN"}
defaultMessage={"Back to Allergies"}
/>
<FormattedMessage id={"BACK_TO_ALLERGEN"} defaultMessage={"Back to Allergies"} />
);
const allergiesHeading = (
<FormattedMessage
id={"ALLERGIES_HEADING"}
defaultMessage={"Allergies and Reactions"}
/>
<FormattedMessage id={"ALLERGIES_HEADING"} defaultMessage={"Allergies and Reactions"} />
);
const [isSaveEnabled, setIsSaveEnabled] = React.useState(false);
const [isSaveSuccess, setIsSaveSuccess] = React.useState(null);
Expand All @@ -44,34 +35,28 @@ export function AddAllergy(props) {
setSeverity("");
};
const saveAllergies = async (allergen, reactions, severity, notes) => {
const {uuid: consultationUuid} = await getEncounterType('Consultation');
const cookies = getCookies();
const {uuid:locationUuid} = JSON.parse(cookies["bahmni.user.location"])
const allergyReactions = reactions.map((reaction) => {
return {reaction: reaction}
return { reaction: { uuid: reaction } };
});
const payload = {
locationUuid,
patientUuid: patient.uuid,
providers: [{uuid: provider.uuid}],
encounterTypeUuid: consultationUuid,
allergy:{
allergen:{
allergenKind: allergen.kind.toUpperCase(),
codedAllergen: allergen.uuid
allergen: {
allergenType: allergen.kind.toUpperCase(),
codedAllergen: {
uuid: allergen.uuid,
},
reactions: allergyReactions,
severity: severity,
comment: notes
}
}
const response = await bahmniEncounter(payload);
if(response.status === 200){
},
reactions: allergyReactions,
severity: { uuid: severity },
comment: notes,
};
const response = await saveAllergiesAPICall(payload, patient.uuid);
console.log("response", response)
if (response.status === 201) {
setIsSaveSuccess(true);
}else{
} else {
setIsSaveSuccess(false);
}
}
};
useEffect(() => {
onSave(isSaveSuccess);
}, [isSaveSuccess]);
Expand Down Expand Up @@ -111,10 +96,7 @@ export function AddAllergy(props) {

<div className={"section-next-ui"}>
<div className={"font-large bold"}>
<FormattedMessage
id={"SEVERITY"}
defaultMessage={"Severity"}
/>
<FormattedMessage id={"SEVERITY"} defaultMessage={"Severity"} />
<span className={"red-text"}>&nbsp;*</span>
</div>
<RadioButtonGroup
Expand All @@ -126,12 +108,7 @@ export function AddAllergy(props) {
}}
>
{severityOptions.map((option) => {
return (
<RadioButton
labelText={option.name}
value={option.uuid}
></RadioButton>
);
return <RadioButton labelText={option.name} value={option.uuid}></RadioButton>;
})}
</RadioButtonGroup>
<TextArea
Expand All @@ -148,7 +125,7 @@ export function AddAllergy(props) {
<div>
<SaveAndCloseButtons
onSave={async () => {
await saveAllergies( allergen, reactions, severity, notes);
await saveAllergies(allergen, reactions, severity, notes);
}}
onClose={onClose}
isSaveDisabled={!isSaveEnabled}
Expand All @@ -166,5 +143,5 @@ AddAllergy.propTypes = {
onSave: propTypes.func.isRequired,
patient: propTypes.object.isRequired,
provider: propTypes.object.isRequired,
severityOptions: propTypes.array.isRequired
severityOptions: propTypes.array.isRequired,
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export const SelectReactions = (props) => {
const [isSearchResultEmpty, setIsSearchResultEmpty] = useState(true);
const [selectedReactions, setSelectedReactions] = useState([]);
const [allReactions] = useState(cloneDeep(reactions));
const [searchKey, setSearchKey] = useState("");

const search = (key) => {
setSearchKey(key)
if (!key) {
setIsSearchResultEmpty(true);
setSearchResults(initialReactionIds);
Expand Down Expand Up @@ -48,6 +50,9 @@ export const SelectReactions = (props) => {
}
};
useEffect(() => {
if(selectedReactions.length > 0){
setSearchKey("")
}
onChange(selectedReactions);
}, [selectedReactions]);

Expand All @@ -60,6 +65,7 @@ export const SelectReactions = (props) => {
<Search
id={"reaction-search"}
placeholder={"Type to search Reactions"}
value={searchKey}
onChange={(e) => {
search(e.target.value);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,16 @@ export function PatientAlergiesControl(props) {
return {allergen, severity, reactions, note, provider, date};
});
allergiesData && allergiesData.sort((a, b) => b?.date - a?.date);
allergiesData ? setAllergiesAndReactions(allergiesData) : setAllergiesAndReactions([]);
}
const filterSeverity = (severity) =>
allergiesData.filter((allergy) => allergy.severity === severity);
allergiesData
? setAllergiesAndReactions([
...filterSeverity("severe"),
...filterSeverity("moderate"),
...filterSeverity("mild"),
])
: setAllergiesAndReactions([]);
};

const [showAddAllergyPanel, setShowAddAllergyPanel] = useState(false);
const [isLoading, setLoading] = useState(false);
Expand Down
1 change: 1 addition & 0 deletions micro-frontends/src/next-ui/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export const LATEST_PUBLISHED_FORMS_URL = RESTWS_V1 + "/bahmniie/form/latestPubl
export const GET_DRUG_ACKNOWLEDGEMENT_URL = RESTWS_V1 + "/bahmnicore/sql?q={property}&v=full&location_uuid={location_uuid}&provider_uuid={provider_uuid}";
export const EMERGENCY_MEDICATIONS_BASE_URL = RESTWS_V1 + "/ipd/adhocMedicationAdministrations/{medication_administration_uuid}";
export const GET_PROVIDER_UUID_URL = RESTWS_V1 + "/session";
export const SAVE_ALLERGIES_URL = RESTWS_V1 + "/patient/{patientId}/allergy";

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { BAHMNI_ENCOUNTER_URL, ENCOUNTER_TYPE_URL, FETCH_CONCEPT_URL, GET_ALLERGIES_URL } from "../../constants";
import { BAHMNI_ENCOUNTER_URL, ENCOUNTER_TYPE_URL, FETCH_CONCEPT_URL, GET_ALLERGIES_URL, SAVE_ALLERGIES_URL } from "../../constants";
import { getLocale } from "../../Components/i18n/utils";

export const fetchAllergensOrReactions = async (conceptId) => {
Expand Down Expand Up @@ -36,8 +36,20 @@ export const bahmniEncounter = async (payload) => {
withCredentials: true,
headers: {"Accept": "application/json", "Content-Type": "application/json"}
});
} catch (error) {
console.log(error);
return error;
}
catch (error) {
};

export const saveAllergiesAPICall = async (payload, patientId) => {
const saveAllergies = SAVE_ALLERGIES_URL.replace("{patientId}", patientId);
try {
return await axios.post(saveAllergies, payload, {
withCredentials: true,
headers: { Accept: "application/json", "Content-Type": "application/json" },
});
} catch (error) {
console.log(error);
return error;
}
Expand Down

0 comments on commit 64cc3bf

Please sign in to comment.