Skip to content

Commit 22b4cef

Browse files
committed
feat: delete flow connections frontend
1 parent 7b8e37b commit 22b4cef

File tree

4 files changed

+106
-17
lines changed

4 files changed

+106
-17
lines changed

packages/frontend/src/components/EditorSettings/FlowConnections/ConnectionsTable.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { IFlow } from '@plumber/types'
22

33
import { useContext } from 'react'
4-
import { BiTrash } from 'react-icons/bi'
54
import { useQuery } from '@apollo/client'
65
import {
76
Box,
87
Center,
98
Flex,
10-
IconButton,
119
Table,
1210
TableContainer,
1311
Tbody,
@@ -23,7 +21,9 @@ import PrimarySpinner from '@/components/PrimarySpinner'
2321
import { EditorSettingsContext } from '@/contexts/EditorSettings'
2422
import { GET_FLOW_CONNECTIONS } from '@/graphql/queries/get-flow-connections'
2523

26-
interface SharedConnection {
24+
import DeleteFlowConnectionButton from './DeleteFlowConnectionButton'
25+
26+
export interface SharedConnection {
2727
addedBy: string
2828
appName: string
2929
appIconUrl: string
@@ -43,10 +43,19 @@ const COLUMNS = ['App', 'Connection name', 'Created by', 'Status', '']
4343

4444
const StatusTag = ({ inUse }: { inUse: boolean }) => {
4545
if (inUse) {
46-
return <Tag colorScheme="success">In use</Tag>
46+
return (
47+
<Tag colorScheme="success" _active={{}} _hover={{}}>
48+
In use
49+
</Tag>
50+
)
4751
}
4852
return (
49-
<Tag bg="interaction.sub-subtle.default" color="base.divider.strong">
53+
<Tag
54+
bg="interaction.sub-subtle.default"
55+
color="base.divider.strong"
56+
_active={{}}
57+
_hover={{}}
58+
>
5059
Not in use
5160
</Tag>
5261
)
@@ -65,7 +74,7 @@ const TableHeader = () => {
6574
}
6675

6776
const TableRow = (props: TableRowProps) => {
68-
const { connection, hasEditPermission } = props
77+
const { connection, flow, hasEditPermission } = props
6978
const {
7079
connectionId,
7180
connectionName,
@@ -108,16 +117,11 @@ const TableRow = (props: TableRowProps) => {
108117
<StatusTag inUse={isInUse} />
109118
</Td>
110119
<Td>
111-
{/* TODO: Add the delete functionality */}
112120
{hasEditPermission && (
113-
<IconButton
114-
onClick={(event) => {
115-
event.stopPropagation()
116-
}}
117-
colorScheme="critical"
118-
variant="clear"
119-
aria-label="Delete flow connection"
120-
icon={<BiTrash />}
121+
<DeleteFlowConnectionButton
122+
flow={flow}
123+
connection={connection}
124+
isDisabled={flow.active}
121125
/>
122126
)}
123127
</Td>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { IFlow } from '@plumber/types'
2+
3+
import { useCallback, useRef } from 'react'
4+
import { BiTrash } from 'react-icons/bi'
5+
import { useMutation } from '@apollo/client'
6+
import { useDisclosure } from '@chakra-ui/react'
7+
import { IconButton, TouchableTooltip } from '@opengovsg/design-system-react'
8+
9+
import MenuAlertDialog from '@/components/MenuAlertDialog'
10+
import { DELETE_FLOW_CONNECTION } from '@/graphql/mutations/delete-flow-connection'
11+
import { GET_FLOW_CONNECTIONS } from '@/graphql/queries/get-flow-connections'
12+
13+
import { SharedConnection } from './ConnectionsTable'
14+
15+
interface DeleteFlowConnectionButtonProps {
16+
flow: IFlow
17+
connection: SharedConnection
18+
isDisabled?: boolean
19+
}
20+
21+
export default function DeleteFlowConnectionButton(
22+
props: DeleteFlowConnectionButtonProps,
23+
) {
24+
const { flow, connection, isDisabled } = props
25+
const { connectionId, connectionName, connectionType } = connection
26+
27+
const cancelRef = useRef<HTMLButtonElement>(null)
28+
const {
29+
isOpen: isDialogOpen,
30+
onOpen: onDialogOpen,
31+
onClose: onDialogClose,
32+
} = useDisclosure()
33+
34+
const [deleteConnection, { loading: isDeletingConnection }] = useMutation(
35+
DELETE_FLOW_CONNECTION,
36+
{ refetchQueries: [GET_FLOW_CONNECTIONS] },
37+
)
38+
39+
const onDeleteConnection = useCallback(async () => {
40+
await deleteConnection({
41+
variables: {
42+
input: { flowId: flow.id, connectionId, connectionType },
43+
},
44+
onCompleted: () => onDialogClose(),
45+
})
46+
}, [deleteConnection, flow.id, connectionId, connectionType, onDialogClose])
47+
return (
48+
<TouchableTooltip
49+
label={
50+
isDisabled
51+
? 'You cannot delete a connection when the Pipe is published'
52+
: ''
53+
}
54+
>
55+
<IconButton
56+
onClick={(event) => {
57+
event.stopPropagation()
58+
onDialogOpen()
59+
}}
60+
colorScheme="critical"
61+
variant="clear"
62+
aria-label="Delete flow connection"
63+
icon={<BiTrash />}
64+
isDisabled={isDisabled}
65+
/>
66+
<MenuAlertDialog
67+
cancelRef={cancelRef}
68+
isLoading={isDeletingConnection}
69+
isDialogOpen={isDialogOpen}
70+
onDialogClose={onDialogClose}
71+
dialogType="delete"
72+
dialogHeader={connectionName}
73+
onClick={onDeleteConnection}
74+
customBody="Are you sure you want to delete this shared connection? You will need to reconfigure the connection for all steps using this connection."
75+
/>
76+
</TouchableTooltip>
77+
)
78+
}

packages/frontend/src/components/MenuAlertDialog/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface MenuAlertDialogProps {
3030
cancelRef: React.RefObject<HTMLButtonElement>
3131
onDialogClose: () => void
3232
dialogType: AlertDialogType
33-
dialogHeader: AlertHeaderType
33+
dialogHeader: AlertHeaderType | string
3434
onClick: (() => void) | MouseEventHandler
3535
isLoading: boolean
3636
customBody?: string
@@ -44,7 +44,7 @@ interface AlertDialogContent {
4444
}
4545

4646
function getAlertDialogContent(
47-
dialogHeader: AlertHeaderType,
47+
dialogHeader: AlertHeaderType | string,
4848
dialogType: AlertDialogType,
4949
customBody?: string,
5050
): AlertDialogContent {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { graphql } from '@/graphql/__generated__'
2+
3+
export const DELETE_FLOW_CONNECTION = graphql(`
4+
mutation DeleteFlowConnection($input: DeleteFlowConnectionInput!) {
5+
deleteFlowConnection(input: $input)
6+
}
7+
`)

0 commit comments

Comments
 (0)