From 29aa17ca7c6661de4ffd0b896af2fda2cc390dbb Mon Sep 17 00:00:00 2001 From: sara-pervana Date: Sun, 28 Jan 2024 17:59:19 +0100 Subject: [PATCH] added consume and register deregister as simple fetch --- .../src/components/consume_action/index.tsx | 40 +++++++++----- .../src/components/entity_actions/index.tsx | 54 +++++++++++++++---- pkgs/ui/src/config/client_1/index.tsx | 5 +- 3 files changed, 71 insertions(+), 28 deletions(-) diff --git a/pkgs/ui/src/components/consume_action/index.tsx b/pkgs/ui/src/components/consume_action/index.tsx index dbf4e7b..84f43a0 100644 --- a/pkgs/ui/src/components/consume_action/index.tsx +++ b/pkgs/ui/src/components/consume_action/index.tsx @@ -1,26 +1,38 @@ import { Button } from "@mui/material"; -import useAxios from "../hooks/useAxios"; import { useState } from "react"; +import axios from "axios"; const ConsumeAction = ({ endpoint }: { endpoint: string }) => { + const [data, setData] = useState(null); + const [error, setError] = useState(null); - const [currentEndpoint, setCurrentEndpoint] = useState(""); - const [shouldFetch, setShouldFetch] = useState(false); - const { data, error } = useAxios(currentEndpoint, "GET", null, true, shouldFetch); - - if (error) console.error("Error consuming:", error); - - if (data) console.log('what the response', data) + if (data) console.log("Data in state", data); + if (error) console.log("Error in state", error); const onConsume = () => { - setCurrentEndpoint(endpoint); - setShouldFetch(true); - } + const axiosConfig = { + url: endpoint, + method: "GET", + data: null, + }; + axios(axiosConfig) + .then((response) => { + setData(response.data); + console.log("I got the data from consume: ", response.data); + }) + .catch((error) => { + console.error("Error happened during consume: ", error); + setError(error); + }) + .finally(() => {}); + }; - return + return ( + + ); }; export default ConsumeAction; diff --git a/pkgs/ui/src/components/entity_actions/index.tsx b/pkgs/ui/src/components/entity_actions/index.tsx index 27d2b3d..538f03c 100644 --- a/pkgs/ui/src/components/entity_actions/index.tsx +++ b/pkgs/ui/src/components/entity_actions/index.tsx @@ -1,8 +1,8 @@ import { IEntityActions } from "@/types"; import { Button, Snackbar, Alert, AlertColor } from "@mui/material"; import { useState } from "react"; -import useAxios from "../hooks/useAxios"; import { deleteEntity } from "@/api/entities/entities"; +import axios from "axios"; interface Props { endpointData: IEntityActions[]; @@ -16,17 +16,23 @@ const SNACKBAR_DEFAULT = { }; const EntityActions = ({ endpointData, rowData }: Props) => { - const [currentEndpoint, setCurrentEndpoint] = useState(""); - const [shouldFetch, setShouldFetch] = useState(false); - const { error } = useAxios(currentEndpoint, "GET", null, true, shouldFetch); - const [snackbar, setSnackbar] = useState<{ open: boolean; message: string; severity: AlertColor; }>(SNACKBAR_DEFAULT); - if (error) console.error("Error registering/deregistering:", error); + const [registerData, setRegisterData] = useState(null); + const [registerError, setRegisterError] = useState(null); + + const [DeregisterData, setDeRegisterData] = useState(null); + const [DeregisterError, setDeRegisterError] = useState(null); + + if (registerData) console.log("Register Data in state", registerData); + if (registerError) console.log("Register Error in state", registerError); + + if (DeregisterData) console.log("Register Data in state", DeregisterData); + if (DeregisterError) console.log("Register Error in state", DeregisterError); const onDeleteEntity = async () => { if (rowData) @@ -50,13 +56,41 @@ const EntityActions = ({ endpointData, rowData }: Props) => { }; const onRegisterEntity = (endpoint: string) => { - setCurrentEndpoint(endpoint); - setShouldFetch(true); + const axiosConfig = { + url: endpoint, + method: "GET", + data: null, + }; + + axios(axiosConfig) + .then((response) => { + setRegisterData(response.data); + console.log("I got the data from register: ", response.data); + }) + .catch((error) => { + console.error("Error happened during register: ", error); + setRegisterError(error); + }) + .finally(() => {}); }; const onDeregisterEntity = (endpoint: string) => { - setCurrentEndpoint(endpoint); - setShouldFetch(true); + const axiosConfig = { + url: endpoint, + method: "GET", + data: null, + }; + + axios(axiosConfig) + .then((response) => { + setDeRegisterData(response.data); + console.log("I got the data from deregister: ", response.data); + }) + .catch((error) => { + console.error("Error happened during deregister: ", error); + setDeRegisterError(error); + }) + .finally(() => {}); }; const handleCloseSnackbar = () => { diff --git a/pkgs/ui/src/config/client_1/index.tsx b/pkgs/ui/src/config/client_1/index.tsx index 7037545..aaa15af 100644 --- a/pkgs/ui/src/config/client_1/index.tsx +++ b/pkgs/ui/src/config/client_1/index.tsx @@ -1,4 +1,3 @@ -import { Button } from "@mui/material"; import EntityActions from "@/components/entity_actions"; import ConsumeAction from "@/components/consume_action"; @@ -15,9 +14,7 @@ export const ClientTableConfig = [ key: "endpoint_url", label: "End Point", render: (value: any) => { - return ( - - ); + return ; }, }, // {