Skip to content

Commit

Permalink
More Delete Dialog refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
salilponde committed Dec 24, 2023
1 parent 1149299 commit c8f4de8
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 414 deletions.
91 changes: 0 additions & 91 deletions web/src/app/images/dialogs/delete-image-dialog.tsx

This file was deleted.

55 changes: 43 additions & 12 deletions web/src/app/images/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
import { IImage } from "@/lib/api-models"
import { useState } from "react"
import useImages from "@/hooks/useImages"
import DeleteImageDialog from "./dialogs/delete-image-dialog"
import { convertByteToMb } from "@/lib/utils"
import { convertByteToMb, toastFailed, toastSuccess } from "@/lib/utils"
import PruneImagesDialog from "./dialogs/prune-images-dialog"
import MainArea from "@/components/widgets/main-area"
import TopBar from "@/components/widgets/top-bar"
Expand All @@ -27,28 +26,60 @@ import { useParams } from "react-router-dom"
import useNodeHead from "@/hooks/useNodeHead"
import TableButtonDelete from "@/components/widgets/table-button-delete"
import { TableNoData } from "@/components/widgets/table-no-data"
import apiBaseUrl from "@/lib/api-base-url"
import DeleteDialog from "@/components/delete-dialog"

export default function Images() {
const { nodeId } = useParams()
const { nodeHead } = useNodeHead(nodeId!)
const { isLoading, images } = useImages(nodeId!)
const [deleteImageOpen, setDeleteImageOpen] = useState(false)
const { isLoading, images, mutateImages } = useImages(nodeId!)
const [image, setImage] = useState<IImage | null>(null)
const [deleteImageConfirmationOpen, setDeleteImageConfirmationOpen] =
useState(false)
const [deleteInProgress, setDeleteInProgress] = useState(false)

if (isLoading) return <Loading />

const handleDeleteImage = (image: IImage) => {
const handleDeleteImageConfirmation = (image: IImage) => {
setImage({ ...image })
setDeleteImageOpen(true)
setDeleteImageConfirmationOpen(true)
}

const handleDelete = async () => {
setDeleteInProgress(true)
const response = await fetch(
`${apiBaseUrl()}/nodes/${nodeId}/images/remove`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: image?.id, force: true }),
}
)
if (!response.ok) {
const r = await response.json()
setDeleteImageConfirmationOpen(false)
toastFailed(r.errors?.body)
} else {
mutateImages()
setTimeout(() => {
setDeleteImageConfirmationOpen(false)
toastSuccess("Image deleted.")
}, 500)
}
setDeleteInProgress(false)
}

return (
<MainArea>
{deleteImageOpen && (
<DeleteImageDialog
openState={deleteImageOpen}
setOpenState={setDeleteImageOpen}
image={image!}
{deleteImageConfirmationOpen && (
<DeleteDialog
openState={deleteImageConfirmationOpen}
setOpenState={setDeleteImageConfirmationOpen}
deleteCaption=""
deleteHandler={handleDelete}
isProcessing={deleteInProgress}
title="Delete Image"
message={`Are you sure you want to delete image '${image?.name}?'`}
/>
)}
<TopBar>
Expand Down Expand Up @@ -96,7 +127,7 @@ export default function Images() {
<TableButtonDelete
onClick={(e) => {
e.stopPropagation()
handleDeleteImage(item)
handleDeleteImageConfirmation(item)
}}
/>
</TableCell>
Expand Down
92 changes: 0 additions & 92 deletions web/src/app/networks/dialogs/delete-network-dialog.tsx

This file was deleted.

54 changes: 43 additions & 11 deletions web/src/app/networks/networks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
import { INetwork } from "@/lib/api-models"
import { useState } from "react"
import useNetworks from "@/hooks/useNetworks"
import DeleteNetworkDialog from "./dialogs/delete-network-dialog"
import PruneNetworksDialog from "./dialogs/prune-networks-dialog"
import MainArea from "@/components/widgets/main-area"
import TopBar from "@/components/widgets/top-bar"
Expand All @@ -26,29 +25,62 @@ import { useParams } from "react-router-dom"
import useNodeHead from "@/hooks/useNodeHead"
import TableButtonDelete from "@/components/widgets/table-button-delete"
import { TableNoData } from "@/components/widgets/table-no-data"
import { toastFailed, toastSuccess } from "@/lib/utils"
import apiBaseUrl from "@/lib/api-base-url"
import DeleteDialog from "@/components/delete-dialog"

export default function Networks() {
const { nodeId } = useParams()
const { nodeHead } = useNodeHead(nodeId!)
const { isLoading, networks } = useNetworks(nodeId!)
const { isLoading, networks, mutateNetworks } = useNetworks(nodeId!)

const [deleteNetworkOpen, setDeleteNetworkOpen] = useState(false)
const [network, setNetwork] = useState<INetwork | null>(null)
const [deleteNetworkOpenConfirmation, setDeleteNetworkOpenConfirmation] =
useState(false)
const [deleteInProgress, setDeleteInProgress] = useState(false)

if (isLoading) return <Loading />

const handleDeleteNetwork = (network: INetwork) => {
const handleDeleteNetworkConfirmation = (network: INetwork) => {
setNetwork({ ...network })
setDeleteNetworkOpen(true)
setDeleteNetworkOpenConfirmation(true)
}

const handleDelete = async () => {
setDeleteInProgress(true)
const response = await fetch(
`${apiBaseUrl()}/nodes/${nodeId}/networks/remove`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: network?.id, force: true }),
}
)
if (!response.ok) {
const r = await response.json()
setDeleteNetworkOpenConfirmation(false)
toastFailed(r.errors?.body)
} else {
mutateNetworks()
setTimeout(() => {
setDeleteNetworkOpenConfirmation(false)
toastSuccess("Network deleted.")
}, 500)
}
setDeleteInProgress(false)
}

return (
<MainArea>
{deleteNetworkOpen && (
<DeleteNetworkDialog
openState={deleteNetworkOpen}
setOpenState={setDeleteNetworkOpen}
network={network!}
{deleteNetworkOpenConfirmation && (
<DeleteDialog
openState={deleteNetworkOpenConfirmation}
setOpenState={setDeleteNetworkOpenConfirmation}
deleteCaption=""
deleteHandler={handleDelete}
isProcessing={deleteInProgress}
title="Delete Network"
message={`Are you sure you want to delete network '${network?.name}?'`}
/>
)}
<TopBar>
Expand Down Expand Up @@ -89,7 +121,7 @@ export default function Networks() {
<TableButtonDelete
onClick={(e) => {
e.stopPropagation()
handleDeleteNetwork(item)
handleDeleteNetworkConfirmation(item)
}}
/>
</TableCell>
Expand Down
Loading

0 comments on commit c8f4de8

Please sign in to comment.