Skip to content

Commit 0c0f528

Browse files
committed
feat: type server name to confirm deletion
1 parent f51470f commit 0c0f528

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

components/Modal.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Dialog, Transition } from '@headlessui/react'
22
import { Fragment, useRef } from 'react'
33
import clsx from 'clsx'
44

5-
export default function Modal ({ open = false, icon, title, children, confirmButton, onCancel, onConfirm, loading }) {
5+
export default function Modal ({ open = false, icon, title, children, confirmButton, confirmDisabled, onCancel, onConfirm, loading }) {
66
const cancelButtonRef = useRef(null)
77
const onClose = () => {
88
onCancel()
@@ -57,8 +57,9 @@ export default function Modal ({ open = false, icon, title, children, confirmBut
5757
<div className='bg-gray-900 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse'>
5858
<button
5959
type='button'
60-
className='w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:ml-3 sm:w-auto sm:text-sm'
60+
className='w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 disabled:opacity-50 disabled:cursor-not-allowed sm:ml-3 sm:w-auto sm:text-sm'
6161
onClick={onConfirm}
62+
disabled={confirmDisabled}
6263
>
6364
{loading && <div className='loader -ml-1 mr-3 h-5 w-5' />}
6465
{confirmButton}

components/admin/ServerItem.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import tailwindConfig from '../../tailwind.config'
1111
import Modal from '../Modal'
1212
import { useMutateApi } from '../../utils'
1313
import ErrorMessages from '../ErrorMessages'
14+
import Message from '../Message'
15+
import Input from '../Input'
1416

1517
const fullConfig = resolveConfig(tailwindConfig)
1618

@@ -25,6 +27,7 @@ const StatItem = ({ onClick, icon, value, selected }) => {
2527

2628
export default function ServerItem ({ canDelete, server, onDeleted }) {
2729
const [open, setOpen] = useState(false)
30+
const [confirmValue, setConfirmValue] = useState('')
2831
const { load, loading, errors, data } = useMutateApi({
2932
query: `mutation deleteServer($id: ID!) {
3033
deleteServer(id: $id)
@@ -45,9 +48,13 @@ export default function ServerItem ({ canDelete, server, onDeleted }) {
4548
setOpen(true)
4649
}
4750
const handleConfirmDelete = async () => {
51+
setConfirmValue('')
4852
await load({ id: server.id })
4953
}
50-
const handleDeleteCancel = () => setOpen(false)
54+
const handleDeleteCancel = () => {
55+
setConfirmValue('')
56+
setOpen(false)
57+
}
5158

5259
const chartData = [
5360
{
@@ -82,15 +89,28 @@ export default function ServerItem ({ canDelete, server, onDeleted }) {
8289
icon={<AiOutlineWarning className='h-6 w-6 text-red-600' aria-hidden='true' />}
8390
title='Delete server'
8491
confirmButton='Delete'
92+
confirmDisabled={confirmValue !== server.name}
8593
open={open}
8694
onConfirm={handleConfirmDelete}
8795
onCancel={handleDeleteCancel}
8896
loading={loading}
8997
>
9098
<ErrorMessages errors={errors} />
91-
<p className='pb-1'>Are you sure you want to delete this server?</p>
92-
<p className='pb-1'>Related <strong>appeals and roles</strong> will be removed</p>
93-
<p className='pb-1'>This action cannot be undone</p>
99+
<Message warning>
100+
<Message.Header>Warning</Message.Header>
101+
<Message.List>
102+
<Message.Item>Related <strong>appeals and roles</strong> will be removed</Message.Item>
103+
<Message.Item>This action cannot be undone</Message.Item>
104+
</Message.List>
105+
</Message>
106+
<p className='mb-4'>Please type <strong>{server.name}</strong> to confirm</p>
107+
<Input
108+
onChange={(e, { value }) => setConfirmValue(value)}
109+
placeholder='Type server name'
110+
className='mb-0'
111+
inputClassName='border border-gray-900'
112+
required
113+
/>
94114
</Modal>
95115
<div className='pt-5 px-5 flex justify-between items-center'>
96116
<h5 className='text-xl font-semibold mb-2 underline'>

0 commit comments

Comments
 (0)