Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UI for delete endpoint #147

Merged
merged 2 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions frontend/src/api/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ export const deleteHost = async (
})
return resp.data
}

export const deleteEndpoint = async (
endpointId: string,
headers?: AxiosRequestHeaders,
): Promise<any> => {
const resp = await axios.delete(`${getAPIURL()}/endpoint/${endpointId}`, {
headers,
})
return resp.data
}
114 changes: 94 additions & 20 deletions frontend/src/components/Endpoint/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react"
import React, { useState } from "react"
import NextLink from "next/link"
import { BiInfoCircle } from "@react-icons/all-files/bi/BiInfoCircle"
import { BsSearch } from "@react-icons/all-files/bs/BsSearch"
Expand All @@ -18,6 +18,15 @@ import {
Tab,
TabPanels,
TabPanel,
Button,
useDisclosure,
AlertDialog,
AlertDialogOverlay,
AlertDialogContent,
AlertDialogHeader,
AlertDialogBody,
AlertDialogFooter,
useToast,
} from "@chakra-ui/react"
import { useRouter } from "next/router"
import { SectionHeader } from "components/utils/Card"
Expand All @@ -33,6 +42,8 @@ import TraceList from "./TraceList"
import { AlertTab } from "./AlertTab"
import EndpointOverview from "./Overview"
import TestList from "./TestList"
import { deleteEndpoint } from "api/endpoints"
import { makeToast } from "utils"

interface EndpointPageProps {
endpoint: ApiEndpointDetailed
Expand All @@ -49,6 +60,10 @@ const EndpointPage: React.FC<EndpointPageProps> = React.memo(
"rgb(179, 181, 185)",
"rgb(91, 94, 109)",
)
const [deleting, setDeleting] = useState<boolean>(false)
const { isOpen, onOpen, onClose } = useDisclosure()
const cancelRef = React.useRef()
const toast = useToast()
const { tab, uuid } = router.query
const getDefaultTab = () => {
switch (tab) {
Expand All @@ -67,6 +82,27 @@ const EndpointPage: React.FC<EndpointPageProps> = React.memo(
}
}

const handleEndpointDelete = async () => {
try {
setDeleting(true)
await deleteEndpoint(endpoint.uuid)
router.push("/endpoints")
} catch (err) {
toast(
makeToast(
{
title: "Deleting endpoint failed...",
status: "error",
description: err.response?.data,
},
err.response?.status,
),
)
} finally {
setDeleting(false)
}
}

return (
<VStack
w="full"
Expand All @@ -75,25 +111,32 @@ const EndpointPage: React.FC<EndpointPageProps> = React.memo(
h="100vh"
overflow="hidden"
>
<VStack alignItems="flex-start" pt="6" px="6">
<NextLink href="/endpoints">
<HStack color={headerColor} spacing="1" cursor="pointer">
<TiFlowSwitch />
<Text fontWeight="semibold">Endpoints</Text>
</HStack>
</NextLink>
<HStack spacing="4" pb="6">
<Badge
fontSize="xl"
px="2"
py="1"
colorScheme={METHOD_TO_COLOR[endpoint?.method] || "gray"}
>
{endpoint?.method.toUpperCase()}
</Badge>
<Code fontSize="xl" fontWeight="semibold" p="1">
{endpoint.path}
</Code>
<VStack w="full" alignItems="flex-start" pt="6" px="6">
<HStack w="full" justifyContent="space-between">
<VStack alignItems="flex-start">
<NextLink href="/endpoints">
<HStack color={headerColor} spacing="1" cursor="pointer">
<TiFlowSwitch />
<Text fontWeight="semibold">Endpoints</Text>
</HStack>
</NextLink>
<HStack spacing="4" pb="6">
<Badge
fontSize="xl"
px="2"
py="1"
colorScheme={METHOD_TO_COLOR[endpoint?.method] || "gray"}
>
{endpoint?.method.toUpperCase()}
</Badge>
<Code fontSize="xl" fontWeight="semibold" p="1">
{endpoint.path}
</Code>
</HStack>
</VStack>
<Button colorScheme="red" isLoading={deleting} onClick={onOpen}>
Delete
</Button>
</HStack>
</VStack>
<Tabs
Expand Down Expand Up @@ -148,6 +191,37 @@ const EndpointPage: React.FC<EndpointPageProps> = React.memo(
</TabPanel>
</TabPanels>
</Tabs>
<AlertDialog
isOpen={isOpen}
leastDestructiveRef={cancelRef}
onClose={onClose}
>
<AlertDialogOverlay>
<AlertDialogContent>
<AlertDialogHeader fontSize="lg" fontWeight="bold">
Delete Endpoint
</AlertDialogHeader>

<AlertDialogBody>
Are you sure you want to delete this endpoint?
</AlertDialogBody>

<AlertDialogFooter>
<Button ref={cancelRef} onClick={onClose}>
Cancel
</Button>
<Button
isLoading={deleting}
colorScheme="red"
onClick={handleEndpointDelete}
ml={3}
>
Delete
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
</VStack>
)
},
Expand Down