diff --git a/frontend/src/components/admin/testManagementConfigMenu/PanelRenameEntry.js b/frontend/src/components/admin/testManagementConfigMenu/PanelRenameEntry.js index 13fee394e0..6a187c0c97 100644 --- a/frontend/src/components/admin/testManagementConfigMenu/PanelRenameEntry.js +++ b/frontend/src/components/admin/testManagementConfigMenu/PanelRenameEntry.js @@ -1,35 +1,7 @@ import React, { useContext, useState, useEffect, useRef } from "react"; -import { - Form, - Heading, - Button, - Loading, - Grid, - Column, - Section, - DataTable, - Table, - TableHead, - TableRow, - TableBody, - TableHeader, - TableCell, - TableSelectRow, - TableSelectAll, - TableContainer, - Pagination, - Search, - Select, - SelectItem, - Stack, - UnorderedList, - ListItem, -} from "@carbon/react"; +import { Heading, Button, Grid, Column, Section } from "@carbon/react"; import { getFromOpenElisServer, - postToOpenElisServer, - postToOpenElisServerFormData, - postToOpenElisServerFullResponse, postToOpenElisServerJsonResponse, } from "../../utils/Utils.js"; import { NotificationContext } from "../../layout/Layout.js"; @@ -39,8 +11,7 @@ import { } from "../../common/CustomNotification.js"; import { FormattedMessage, injectIntl, useIntl } from "react-intl"; import PageBreadCrumb from "../../common/PageBreadCrumb.js"; -import CustomCheckBox from "../../common/CustomCheckBox.js"; -import ActionPaginationButtonType from "../../common/ActionPaginationButtonType.js"; +import RenameModelBox from "./renameModel/RenameModelBox.js"; let breadcrumbs = [ { label: "home.label", link: "/" }, @@ -60,33 +31,197 @@ function PanelRenameEntry() { useContext(NotificationContext); const intl = useIntl(); + const modalHeading = intl.formatMessage({ id: "field.panel" }); const componentMounted = useRef(false); + const [isLoading, setIsLoading] = useState(false); + const [finished, setFinished] = useState(true); + const [isAddModalOpen, setIsAddModalOpen] = useState(false); + const [confirmationStep, setConfirmationStep] = useState(false); + const [inputError, setInputError] = useState(false); + const [panel, setPanel] = useState({}); + const [panelListShow, setPanelListShow] = useState([]); + const [panelPost, setPanelPost] = useState({}); + const [entityNamesProvider, setEntityNamesProvider] = useState({}); + const [entityNamesProviderPost, setEntityNamesProviderPost] = useState({}); + const [entityId, setEntityId] = useState(); + const [entityName, setEntityName] = useState("panel"); + const [selectedItem, setSelectedItem] = useState({}); + + useEffect(() => { + componentMounted.current = true; + getFromOpenElisServer("/rest/PanelRenameEntry", handelPanelRename); + return () => { + componentMounted.current = false; + }; + }, []); + + const handelPanelRename = (res) => { + if (!res) { + setIsLoading(true); + } else { + setPanel(res); + setPanelPost(res); + setPanelListShow(res.panelList); + } + }; + + useEffect(() => { + getFromOpenElisServer( + `/rest/EntityNamesProvider?entityId=${entityId}&entityName=${entityName}`, + handelEntityNamesProvider, + ); + }, [entityId]); + + const handelEntityNamesProvider = (res) => { + if (!res) { + setIsLoading(true); + } else { + setEntityNamesProvider(res); + setEntityNamesProviderPost(res); + } + }; + + function panelUpdatePost() { + setIsLoading(true); + if (confirmationStep) { + postToOpenElisServerJsonResponse( + `/rest/PanelRenameEntry`, + JSON.stringify(panelPost), + (res) => { + panelUpdatePostCallback(res); + }, + ); + } else { + setConfirmationStep(true); + } + } + + function panelUpdatePostCallback(res) { + if (res) { + setIsLoading(false); + setFinished(false); + addNotification({ + title: intl.formatMessage({ + id: "notification.title", + }), + message: intl.formatMessage({ + id: "notification.user.post.save.success", + }), + kind: NotificationKinds.success, + }); + setNotificationVisible(true); + setIsAddModalOpen(false); + } else { + addNotification({ + kind: NotificationKinds.error, + title: intl.formatMessage({ id: "notification.title" }), + message: intl.formatMessage({ id: "server.error.msg" }), + }); + setNotificationVisible(true); + setTimeout(() => { + window.location.reload(); + }, 200); + } + } + + const openAppModle = (item) => { + setConfirmationStep(false); + setIsAddModalOpen(true); + setEntityId(item.id); + // setEntityName(test.value); + setSelectedItem(item); + }; + + const onInputChangeEn = (e) => { + const englishName = e.target.value; + setEntityNamesProviderPost((prev) => ({ + name: { + ...prev.name, + english: englishName, + }, + })); + setInputError(false); + }; + + const onInputChangeFr = (e) => { + const frenchName = e.target.value; + setEntityNamesProviderPost((prev) => ({ + name: { + ...prev.name, + french: frenchName, + }, + })); + setInputError(false); + }; + + useEffect(() => { + if (entityId && entityNamesProviderPost && entityNamesProviderPost.name) { + setPanelPost((prev) => ({ + ...prev, + panelId: entityId, + nameEnglish: entityNamesProviderPost.name.english, + nameFrench: entityNamesProviderPost.name.french, + })); + } + }, [entityNamesProviderPost, entityId]); + + const closeAddModal = () => { + setIsAddModalOpen(false); + }; return ( <> {notificationVisible === true ? : ""}
- - -
- - - -
-
-
-
-
-
- - - - - +
+ + +
+ + + +
+
+
+
+
+
+ + + + + +
+ +
); diff --git a/frontend/src/components/admin/testManagementConfigMenu/renameModel/RenameModelBox.js b/frontend/src/components/admin/testManagementConfigMenu/renameModel/RenameModelBox.js new file mode 100644 index 0000000000..546ec25534 --- /dev/null +++ b/frontend/src/components/admin/testManagementConfigMenu/renameModel/RenameModelBox.js @@ -0,0 +1,197 @@ +import React from "react"; +import { + Heading, + Button, + Loading, + Grid, + Column, + Section, + Modal, + TextInput, +} from "@carbon/react"; +import { FormattedMessage, injectIntl } from "react-intl"; +import PropTypes from "prop-types"; + +const RenameModelBox = ({ + data, + isModalOpen, + openModel, + closeModel, + onSubmit, + onInputChangeEn, + onInputChangeFr, + isLoading, + modalHeading, + heading, + mainLabel, + confirmationStep, + inputError, + lang, + langPost, + selectedItem, +}) => { + return ( + <> + {data ? ( + + {data.map((item, index) => ( + + + + + ) : ( + <> + + + ) + } + secondaryButtonText={ + confirmationStep ? ( + <> + + + ) : ( + <> + + + ) + } + onRequestSubmit={onSubmit} + onRequestClose={closeModel} + > + {lang && lang.name && langPost && langPost.name ? ( + + + {/* Edit or Confirmation */} +
+
+ + + +
+
+
+
+
+
+ + {/* main blue lable */} + + +
+
+
+
+ <> + :{" "} + {lang?.name?.english} + +
+
+ { + onInputChangeEn(e); + }} + required + invalid={inputError} + invalidText={ + + } + /> +
+ <> + :{" "} + {lang?.name?.french} + +
+
+ { + onInputChangeFr(e); + }} + required + invalid={inputError} + invalidText={ + + } + /> +
+
+ ) : ( + <> +
+ +
+ + )} +
+ {confirmationStep && ( + <> +
+
+
+ + + +
+
+
+ + )} +
+ +
+ ))} +
+ ) : ( + <> + + + )} + + ); +}; + +RenameModelBox.propTypes = { + data: PropTypes.arrayOf(PropTypes.object).isRequired, + isModalOpen: PropTypes.bool.isRequired, + closeModel: PropTypes.func.isRequired, + onSubmit: PropTypes.func.isRequired, + onInputChangeEn: PropTypes.func.isRequired, + onInputChangeFr: PropTypes.func.isRequired, + isLoading: PropTypes.bool.isRequired, + modalHeading: PropTypes.string.isRequired, + heading: PropTypes.string.isRequired, + mainLabel: PropTypes.string.isRequired, + confirmationStep: PropTypes.bool.isRequired, + inputError: PropTypes.bool.isRequired, + lang: PropTypes.object.isRequired, + langPost: PropTypes.object.isRequired, + selectedItem: PropTypes.object.isRequired, +}; + +export default injectIntl(RenameModelBox); diff --git a/src/main/java/org/openelisglobal/common/provider/query/rest/EntityNamesProviderRestController.java b/src/main/java/org/openelisglobal/common/provider/query/rest/EntityNamesProviderRestController.java new file mode 100644 index 0000000000..d41c81a8ca --- /dev/null +++ b/src/main/java/org/openelisglobal/common/provider/query/rest/EntityNamesProviderRestController.java @@ -0,0 +1,148 @@ +/* + * The contents of this file are subject to the Mozilla Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + * License for the specific language governing rights and limitations under + * the License. + * + * The Original Code is OpenELIS code. + * + * Copyright (C) ITECH, University of Washington, Seattle WA. All Rights Reserved. + */ +package org.openelisglobal.common.provider.query.rest; + +import java.util.Locale; +import org.apache.commons.validator.GenericValidator; +import org.json.simple.JSONObject; +import org.openelisglobal.localization.valueholder.Localization; +import org.openelisglobal.panel.service.PanelService; +import org.openelisglobal.renamemethod.service.RenameMethodService; +import org.openelisglobal.renametestsection.service.RenameTestSectionService; +import org.openelisglobal.typeofsample.service.TypeOfSampleService; +import org.openelisglobal.unitofmeasure.service.UnitOfMeasureService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/rest") +public class EntityNamesProviderRestController { + + @Autowired + private PanelService panelService; + + @Autowired + private RenameTestSectionService renameTestSectionService; + + @Autowired + private TypeOfSampleService typeOfSampleService; + + @Autowired + private UnitOfMeasureService unitOfMeasureService; + + @Autowired + private RenameMethodService renameMethodService; + + public static final String PANEL = "panel"; + public static final String SAMPLE_TYPE = "sampleType"; + public static final String TEST_SECTION = "testSection"; + public static final String UNIT_OF_MEASURE = "unitOfMeasure"; + public static final String METHOD = "method"; + String INVALID = "invalid"; + String VALID = "valid"; + + @SuppressWarnings("unchecked") + @GetMapping("/EntityNamesProvider") + public ResponseEntity processRequest(@RequestParam("entityId") String id, + @RequestParam("entityName") String entityName) { + + if (GenericValidator.isBlankOrNull(id) || GenericValidator.isBlankOrNull(entityName)) { + String errorMessage = "Internal error, please contact Admin and file bug report"; + JSONObject errorJson = new JSONObject(); + errorJson.put("status", INVALID); + errorJson.put("message", errorMessage); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorJson); + } + + JSONObject jsonResult = new JSONObject(); + String status = createJsonTestNames(id, entityName, jsonResult); + + if (status.equals(VALID)) { + return ResponseEntity.ok(jsonResult); + } else { + JSONObject errorJson = new JSONObject(); + errorJson.put("status", INVALID); + errorJson.put("message", "No localization found for the given entity."); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorJson); + } + } + + @SuppressWarnings("unchecked") + private String createJsonTestNames(String id, String entityName, JSONObject jsonResult) + throws IllegalStateException { + + Localization localization = null; + + if (PANEL.equals(entityName)) { + localization = getLocalizationForPanel(id); + } else if (SAMPLE_TYPE.equals(entityName)) { + localization = getLocalizationForSampleType(id); + } else if (TEST_SECTION.equals(entityName)) { + localization = getLocalizationForRenameTestSection(id); + } else if (UNIT_OF_MEASURE.equals(entityName)) { + localization = getLocalizationForUnitOfMeasure(id); + } else if (METHOD.equals(entityName)) { + localization = getLocalizationForRenameMethod(id); + } + // add entity types as needed + + if (localization != null) { + + JSONObject nameObject = new JSONObject(); + addAllLocalizations(nameObject, localization); + // nameObject.put("english", localization.getEnglish()); + // nameObject.put("french", localization.getFrench()); + jsonResult.put("name", nameObject); + + return VALID; + } + + return INVALID; + } + + @SuppressWarnings("unchecked") + private void addAllLocalizations(JSONObject jsonObject, Localization localization) { + for (Locale locale : localization.getLocalesWithValue()) { + jsonObject.put(locale.getDisplayLanguage(Locale.ENGLISH).toLowerCase(), + localization.getLocalizedValue(locale)); + } + } + + private Localization getLocalizationForPanel(String id) { + return panelService.getLocalizationForPanel(id); + } + + private Localization getLocalizationForSampleType(String id) { + return typeOfSampleService.getLocalizationForSampleType(id); + } + + private Localization getLocalizationForRenameTestSection(String id) { + return renameTestSectionService.getLocalizationForRenameTestSection(id); + } + + private Localization getLocalizationForUnitOfMeasure(String id) { + return unitOfMeasureService.getLocalizationForUnitOfMeasure(id); + } + + private Localization getLocalizationForRenameMethod(String id) { + return renameMethodService.getLocalizationForRenameMethod(id); + } +}