From e6b854c9b9ed5e63b7dfed9ed313f76d9d49f8c9 Mon Sep 17 00:00:00 2001 From: Wojciech Wrzalik <74771103+ralikio@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:03:00 +0200 Subject: [PATCH] Dialog and StatusMessage Mechanics Corrections (#799) Mechanics Corection for Dialog and Status Messages --- .../ServiceInstancesDetailsView.tsx | 28 ++++-- ui/src/components/ServiceInstancesView.tsx | 92 ++++++++++--------- ui/src/components/StatusMessage.tsx | 4 - 3 files changed, 68 insertions(+), 56 deletions(-) diff --git a/ui/src/components/ServiceInstancesDetailsView.tsx b/ui/src/components/ServiceInstancesDetailsView.tsx index ca30d1c4e..c7ae5d9c2 100644 --- a/ui/src/components/ServiceInstancesDetailsView.tsx +++ b/ui/src/components/ServiceInstancesDetailsView.tsx @@ -4,23 +4,34 @@ import { ApiError, ServiceInstance, } from "../shared/models"; -import { useEffect, useRef, useState } from "react"; +import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react"; import ServiceBindingsList from "./ServiceBindingsList"; import '@ui5/webcomponents/dist/features/InputElementsFormSupport.js'; import CreateBindingForm from "./CreateBindingForm"; - -function ServiceInstancesDetailsView(props: any) { +const ServiceInstancesDetailsView = forwardRef((props: any, ref) => { const [loading, setLoading] = useState(true); const [error] = useState(); const [instance, setInstance] = useState(); const dialogRef = useRef(null); + useImperativeHandle(ref, () => ({ + + open() { + if (dialogRef.current) { + // @ts-ignore + dialogRef.current.show(); + } + } + + })); const handleClose = () => { - // @ts-ignoren - dialogRef.current.close(); + if (dialogRef.current) { + // @ts-ignore + dialogRef.current.close(); + } }; useEffect(() => { @@ -32,7 +43,6 @@ function ServiceInstancesDetailsView(props: any) { setLoading(true) - setLoading(false) }, [props.instance]); @@ -53,7 +63,6 @@ function ServiceInstancesDetailsView(props: any) { return ( - - + @@ -102,6 +110,6 @@ function ServiceInstancesDetailsView(props: any) { } // @ts-ignore return <>{renderData()}; -} +}) export default ServiceInstancesDetailsView; \ No newline at end of file diff --git a/ui/src/components/ServiceInstancesView.tsx b/ui/src/components/ServiceInstancesView.tsx index 3b1dc0b8e..5aa270f53 100644 --- a/ui/src/components/ServiceInstancesView.tsx +++ b/ui/src/components/ServiceInstancesView.tsx @@ -1,19 +1,22 @@ import * as ui5 from "@ui5/webcomponents-react"; -import { ServiceInstances } from "../shared/models"; +import { ServiceInstance, ServiceInstances } from "../shared/models"; import axios from "axios"; -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { createPortal } from "react-dom"; import api from "../shared/api"; import Ok from "../shared/validator"; import serviceInstancesData from '../test-data/serivce-instances.json'; import ServiceInstancesDetailsView from "./ServiceInstancesDetailsView"; import { useParams } from "react-router-dom"; +import StatusMessage from "./StatusMessage"; function ServiceInstancesView() { const [serviceInstances, setServiceInstances] = useState(); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); - const [portal, setPortal] = useState(); + const [selectedInstance, setSelectedInstance] = useState(new ServiceInstance()); + const dialogRef = useRef(); + const [success, setSuccess] = useState(""); let { id } = useParams(); @@ -53,29 +56,28 @@ function ServiceInstancesView() { /> } - if (error) { - return - } - function openPortal(instance: any) { - console.log("Row clicked") - const instanceView = - const portal = createPortal(instanceView, document.getElementById("App")!!) - setPortal(portal) + setSelectedInstance(instance) + //@ts-ignore + dialogRef.current.open() } function deleteInstance(id: string): boolean { setLoading(true); - axios - .delete(api("service-instances") + "/" + id) - .then((response) => { - setLoading(false); - }) - .catch((error) => { - setLoading(false); - setError(error); - }); + .delete(api("service-instances") + "/" + id) + .then((response) => { + serviceInstances!!.items = serviceInstances!!.items.filter(instance => instance.id !== id) + setServiceInstances(serviceInstances); + setSuccess("Service instance deleted successfully") + setError(null) + setLoading(false); + + }) + .catch((error) => { + setLoading(false); + setError(error); + }); return true; } @@ -110,9 +112,11 @@ function ServiceInstancesView() { @@ -128,30 +132,34 @@ function ServiceInstancesView() { return ( <> - - - - Service Instance - + - - Service Namespace - + - - Action - - - } - > - {renderData()} - - - {portal != null && portal} + + + Service Instance + + + + Service Namespace + + + + Action + + + } + > + {renderData()} + + + {createPortal(, document.getElementById("App")!!)} ); } diff --git a/ui/src/components/StatusMessage.tsx b/ui/src/components/StatusMessage.tsx index f8e68d497..eec131201 100644 --- a/ui/src/components/StatusMessage.tsx +++ b/ui/src/components/StatusMessage.tsx @@ -32,23 +32,19 @@ function StatusMessage(props: StatusMessageProps) { if (props.error) { return ( - {message} - ); } else if (props.success) { return ( - {props.success} - ); } };