Skip to content

Commit

Permalink
parentOrg Selection fixed + autoComplete POST request working
Browse files Browse the repository at this point in the history
  • Loading branch information
adityadeshlahre committed Jun 21, 2024
1 parent fcc06d6 commit c5e99d9
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useContext, useState, useEffect, useRef } from "react";
import { useLocation } from "react-router-dom";
import {
Form,
Heading,
Expand Down Expand Up @@ -29,14 +28,17 @@ import {
postToOpenElisServerFullResponse,
postToOpenElisServerJsonResponse,
} from "../../utils/Utils.js";
import { NotificationContext } from "../../layout/Layout.js";
import {
ConfigurationContext,
NotificationContext,
} from "../../layout/Layout.js";
import {
AlertDialog,
NotificationKinds,
} from "../../common/CustomNotification.js";
import { Field, Formik } from "formik";
import { FormattedMessage, injectIntl, useIntl } from "react-intl";
import PageBreadCrumb from "../../common/PageBreadCrumb.js";
import AutoComplete from "../../common/AutoComplete.js";

let breadcrumbs = [
{ label: "home.label", link: "/" },
Expand All @@ -50,6 +52,7 @@ let breadcrumbs = [
function OrganizationAddModify() {
const { notificationVisible, setNotificationVisible, addNotification } =
useContext(NotificationContext);
const { configurationProperties } = useContext(ConfigurationContext);

const componentMounted = useRef(false);
const intl = useIntl();
Expand All @@ -60,29 +63,23 @@ function OrganizationAddModify() {
const [orgSelectedTypeOfActivity, setOrgSelectedTypeOfActivity] = useState(
[],
);
const [parentOrgList, setParentOrgList] = useState([]);
const [parentOrgId, setParentOrgId] = useState("");
const [parentOrg, setParentOrg] = useState({});
const [parentOrgPost, setParentOrgPost] = useState({});
const [orgInfo, setOrgInfo] = useState({});
const [orgInfoPost, setOrgInfoPost] = useState({});
const [saveButton, setSaveButton] = useState(true);
const [typeOfActivity, setTypeOfActivity] = useState();
const [typeOfActivityShow, setTypeOfActivityShow] = useState([]);
const [id, setId] = useState(null);

//const id = new URLSearchParams(useLocation().search).get("ID");

useEffect(() => {
const getIdFromUrl = () => {
// Get the hash part of the URL
const hash = window.location.hash;

// Check if the hash contains the query parameters
if (hash.includes("?")) {
// Extract the query part from the hash
const queryParams = hash.split("?")[1];

// Create a URLSearchParams object to easily access the parameters
const urlParams = new URLSearchParams(queryParams);

// Get the value of the 'ID' parameter
const id = urlParams.get("ID");

return id;
Expand Down Expand Up @@ -116,6 +113,18 @@ function OrganizationAddModify() {
}
};

useEffect(() => {
getFromOpenElisServer(`/rest/ParentOrganization`, handleParentOrgList);
}, []);

const handleParentOrgList = (res) => {
if (!res) {
setLoading(true);
} else {
setParentOrgList(res);
}
};

useEffect(() => {
if (typeOfActivity) {
const newOrganizationsManagementList = typeOfActivity.orgTypes.map(
Expand Down Expand Up @@ -237,16 +246,85 @@ function OrganizationAddModify() {

function handleInternetAddressChange(e) {
setSaveButton(false);
const value = e.target.value.trim();
const urlPattern =
/^(https?:\/\/)?(www\.)?[\w-]+\.[a-z]{2,}(\.[a-z]{2,})?$/i;

if (value && !urlPattern.test(value)) {
if (!notificationVisible) {
setNotificationVisible(true);
addNotification({
title: intl.formatMessage({
id: "notification.title",
}),
message: intl.formatMessage({
id: "notification.organization.post.internetAddress",
}),
kind: NotificationKinds.info,
});
}
} else {
setNotificationVisible(false);
}

setOrgInfoPost((prevOrgInfoPost) => ({
...prevOrgInfoPost,
internetAddress: e.target.value,
internetAddress: value,
}));
setOrgInfo((prevOrgInfo) => ({
...prevOrgInfo,
internetAddress: e.target.value,
internetAddress: value,
}));
}

function handleParentOrganizationName(e) {
setParentOrgPost({
...parentOrgPost,
parentOrganizationName: e.target.value,
});
setSaveButton(false);
}

function handleAutoCompleteParentOrganizationNames(parentOrgId) {
setParentOrgId(parentOrgId);
setSaveButton(false);
}

const handleParentOrgPost = (res) => {
if (!res) {
setLoading(true);
} else {
setParentOrg(res);
}
};

useEffect(() => {
getFromOpenElisServer(
`/rest/organization/${parentOrgId}`,
handleParentOrgPost,
);
}, [parentOrgId]);

useEffect(() => {
if (parentOrg) {
const parentOrgPost = {
id: parentOrg.id,
isActive: parentOrg.isActive,
lastupdated: parentOrg.lastupdated,
mlsSentinelLabFlag: parentOrg.mlsSentinelLabFlag,
organizationName: parentOrg.organizationName,
organizationTypes: parentOrg.organizationTypes,
shortName: parentOrg.shortName,
testSections: parentOrg.testSections,
};
setParentOrgPost(parentOrgPost);
setOrgInfoPost((prevOrgInfo) => ({
...prevOrgInfo,
organization: parentOrgPost,
}));
}
}, [parentOrg]);

function submitAddUpdatedOrgInfo() {
setLoading(true);
postToOpenElisServerJsonResponse(
Expand Down Expand Up @@ -428,7 +506,7 @@ function OrganizationAddModify() {
type="text"
labelText=""
placeholder={intl.formatMessage({
id: "organization.add.placeholder",
id: "organization.add.placeholder.internetAddress",
})}
// invalid={errors.order && touched.order}
// invalidText={errors.order}
Expand All @@ -441,6 +519,45 @@ function OrganizationAddModify() {
/>
</Column>
</Grid>
<Grid fullWidth={true}>
<Column lg={8} md={4} sm={4}>
<>
<FormattedMessage id="organization.parent" /> :
</>
</Column>
<Column lg={8} md={4} sm={4}>
<AutoComplete
name="parentOrgName"
id="parentOrgName"
allowFreeText={
!(
configurationProperties.restrictFreeTextRefSiteEntry ===
"true"
)
}
value={
typeOfActivity &&
typeOfActivity.organization &&
typeOfActivity.organization.organizationName != ""
? typeOfActivity.organization.organizationName
: ""
}
onChange={handleParentOrganizationName}
onSelect={handleAutoCompleteParentOrganizationNames}
label={
<>
<FormattedMessage id="organization.search.parent.name" />{" "}
<span className="requiredlabel">*</span>
</>
}
style={{ width: "!important 100%" }}
suggestions={
parentOrgList.length > 0 ? parentOrgList : []
}
required
/>
</Column>
</Grid>
</Form>
</Column>
</Grid>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,10 @@
"organization.type.CI.name": "Type of Activity : Name",
"organization.type.CI.description": "Type of Activity : Description",
"organization.add.placeholder": "AlphaNumeric Values",
"organization.add.placeholder.internetAddress": "https://orgname.com",
"organization.search.parent.name": "Parent Organization Names",
"notification.organization.post.internetAddress": "Enter a valid Organization Url.",
"organization.add.placeholder.parent": "Parent Organization",
"organization.add.placeholder.active": "Y or N",
"organization.search.byorgname": "Search By Org Name",
"organization.search.placeHolder": "Search By Org Name...",
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/languages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,9 @@
"organization.type.CI.select": "Type d'activité : Sélectionner",
"organization.type.CI.name": "Type d'activité : Nom",
"organization.type.CI.description": "Type d'activité : Description",
"organization.add.placeholder.internetAddress": "https://orgname.com",
"organization.search.parent.name": "Noms des organisations parentes",
"notification.organization.post.internetAddress": "Entrez une URL d'organisation valide.",
"organization.add.placeholder": "Valeurs alphanumériques",
"organization.add.placeholder.active": "Oui ou Non",
"organization.search.byorgname": "Rechercher par nom d'organisation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openelisglobal.common.formfields.FormFields.Field;
import org.openelisglobal.common.log.LogEvent;
import org.openelisglobal.common.services.DisplayListService;
import org.openelisglobal.common.util.IdValuePair;
import org.openelisglobal.common.util.StringUtil;
import org.openelisglobal.common.validator.ValidationHelper;
import org.openelisglobal.dataexchange.fhir.exception.FhirPersistanceException;
Expand Down Expand Up @@ -345,6 +346,19 @@ public ResponseEntity<Organization> getOrganization(@PathVariable("id") String i
}
}

@GetMapping("/ParentOrganization")
public ResponseEntity<List<IdValuePair>> getParentOrganization() {
List<Organization> parentOrgs = organizationService.getAllOrganizations();
List<IdValuePair> idValuePairs = new ArrayList<>();

for (Organization parentOrg : parentOrgs) {
IdValuePair idValuePair = new IdValuePair(parentOrg.getId(), parentOrg.getOrganizationName());
idValuePairs.add(idValuePair);
}

return ResponseEntity.ok(idValuePairs);
}

@PostMapping(value = "/Organization")
public ResponseEntity<Object> showUpdateOrganization(
@RequestBody @Valid OrganizationForm form, BindingResult result)
Expand Down

0 comments on commit c5e99d9

Please sign in to comment.