This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'delete function' similar to delete table (#52)
- Loading branch information
Showing
4 changed files
with
195 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { DeleteOutlined, MoreOutlined } from '@ant-design/icons'; | ||
import { Button, Dropdown, MenuProps } from 'antd'; | ||
import { useMemo, useState } from 'react'; | ||
import { DeleteFunctionModal } from '../modals/DeleteFunctionModal'; | ||
|
||
interface FunctionActionDropdownProps { | ||
catalog: string; | ||
schema: string; | ||
ucFunction: string; | ||
} | ||
|
||
enum FunctionActionsEnum { | ||
Delete, | ||
} | ||
|
||
export default function FunctionActionsDropdown({ | ||
catalog, | ||
schema, | ||
ucFunction, | ||
}: FunctionActionDropdownProps) { | ||
const [dropdownVisible, setDropdownVisible] = useState<boolean>(false); | ||
const [action, setAction] = useState<FunctionActionsEnum | null>(null); | ||
|
||
const menuItems = useMemo( | ||
(): MenuProps['items'] => [ | ||
{ | ||
key: 'deleteFunction', | ||
label: 'Delete Function', | ||
onClick: () => setAction(FunctionActionsEnum.Delete), | ||
icon: <DeleteOutlined />, | ||
danger: true, | ||
}, | ||
], | ||
[], | ||
); | ||
|
||
return ( | ||
<> | ||
<Dropdown | ||
menu={{ items: menuItems }} | ||
trigger={['click']} | ||
onOpenChange={() => setDropdownVisible(!dropdownVisible)} | ||
> | ||
<Button | ||
type="text" | ||
icon={ | ||
<MoreOutlined | ||
rotate={dropdownVisible ? 90 : 0} | ||
style={{ transition: 'transform 0.5s' }} | ||
/> | ||
} | ||
/> | ||
</Dropdown> | ||
<DeleteFunctionModal | ||
open={action === FunctionActionsEnum.Delete} | ||
closeModal={() => setAction(null)} | ||
catalog={catalog} | ||
schema={schema} | ||
ucFunction={ucFunction} | ||
/> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Modal, Typography } from 'antd'; | ||
import React, { useCallback } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useDeleteFunction } from '../../hooks/functions'; | ||
import { useNotification } from '../../utils/NotificationContext'; | ||
|
||
interface DeleteFunctionModalProps { | ||
open: boolean; | ||
closeModal: () => void; | ||
catalog: string; | ||
schema: string; | ||
ucFunction: string; | ||
} | ||
|
||
export function DeleteFunctionModal({ | ||
open, | ||
closeModal, | ||
catalog, | ||
schema, | ||
ucFunction, | ||
}: DeleteFunctionModalProps) { | ||
const navigate = useNavigate(); | ||
const { setNotification } = useNotification(); | ||
const mutation = useDeleteFunction({ | ||
onSuccessCallback: () => { | ||
setNotification(`${ucFunction} function successfully deleted`, 'success'); | ||
navigate(`/data/${catalog}/${schema}`); | ||
}, | ||
catalog, | ||
schema, | ||
}); | ||
|
||
const handleSubmit = useCallback(() => { | ||
mutation.mutate( | ||
{ catalog_name: catalog, schema_name: schema, name: ucFunction }, | ||
{ | ||
onError: (error: Error) => { | ||
setNotification(error.message, 'error'); | ||
}, | ||
}, | ||
); | ||
}, [mutation, ucFunction, setNotification]); | ||
|
||
return ( | ||
<Modal | ||
title={ | ||
<Typography.Title type={'danger'} level={4}> | ||
Delete function | ||
</Typography.Title> | ||
} | ||
okText="Delete" | ||
okType="danger" | ||
cancelText="Cancel" | ||
open={open} | ||
destroyOnClose | ||
onCancel={closeModal} | ||
onOk={handleSubmit} | ||
okButtonProps={{ loading: mutation.isPending }} | ||
> | ||
<Typography.Text> | ||
Are you sure you want to delete the function | ||
</Typography.Text> | ||
<Typography.Text strong>{` ${ucFunction}`}</Typography.Text> | ||
<Typography.Text>? This operation cannot be undone.</Typography.Text> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters