Files
supabase/apps/studio/components/interfaces/Organization/GeneralSettings/DeleteOrganizationButton.tsx
Alaister YoungandKevin Grüneberg 70da0f1d1d chore: cleanup packages (#27770)
* chore: cleanup packages

- Avoid circular imports
- Export API-types as types
- pg-format without depending on Node internal Buffer (not browser-compatible)
- Avoid importing from barrel files in ui dir

* chore: avoid barrel file imports in studio (#27771)

* chore: avoid barrel file imports

- Removes some unused imports
- Avoids barrel file import for faster builds + less memory

* add eslint rule

* type fixes

* delete layouts barrel

* delete components/grid barrel file

* delete components/grid/utils barrel file

* delete components/grid/components/common barrel file

* delete components/grid/components/editor barrel file

* delete components/grid/components/formatter barrel file

* delete components/grid/components/grid barrel file

* delete components/grid/components/header/filter barrel file

* remote components/grid/store barrel file

* remove components/interfaces/Auth/Policies barrel file

* delete components/interfaces/Settings/Logs barrel file

* delete components/ui/CodeEditor barrel file

* delete components/ui/Forms barrel file

* delete components/ui/Shimmers barrel file

* delete data/analytics barrel file

* delete hooks barrel file

* cleanup lib/common/fetch barrel file

* final * barral files cleanup

* global react-data-grid styles

* remove console.log

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>

* fix build

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
2024-07-04 14:48:10 +08:00

119 lines
3.8 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useRouter } from 'next/router'
import { useState } from 'react'
import toast from 'react-hot-toast'
import { useOrganizationDeleteMutation } from 'data/organizations/organization-delete-mutation'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization'
import { Button, Form, Input, Modal } from 'ui'
const DeleteOrganizationButton = () => {
const router = useRouter()
const selectedOrganization = useSelectedOrganization()
const { slug: orgSlug, name: orgName } = selectedOrganization ?? {}
const [isOpen, setIsOpen] = useState(false)
const [value, setValue] = useState('')
const canDeleteOrganization = useCheckPermissions(PermissionAction.UPDATE, 'organizations')
const { mutate: deleteOrganization, isLoading: isDeleting } = useOrganizationDeleteMutation({
onSuccess: () => {
toast.success(`Successfully deleted ${orgName}`)
router.push('/projects')
},
})
const onValidate = (values: any) => {
const errors: any = {}
if (!values.orgName) {
errors.orgName = 'Enter the name of the organization.'
}
if (values.orgName !== orgSlug) {
errors.orgName = 'Value entered does not match the value above.'
}
return errors
}
const onConfirmDelete = async (values: any) => {
if (!canDeleteOrganization) {
return toast.error('You do not have the required permissions to delete this organization')
}
if (!orgSlug) return console.error('Org slug is required')
deleteOrganization({ slug: orgSlug })
}
return (
<>
<div className="mt-2">
<Button loading={!orgSlug} onClick={() => setIsOpen(true)} type="danger">
Delete organization
</Button>
</div>
<Modal
closable
hideFooter
size="small"
visible={isOpen}
onCancel={() => setIsOpen(false)}
header={
<div className="flex items-baseline gap-2">
<span>Delete organization</span>
<span className="text-xs text-foreground-lighter">Are you sure?</span>
</div>
}
>
<Form
validateOnBlur
initialValues={{ orgName: '' }}
onSubmit={onConfirmDelete}
validate={onValidate}
>
{() => (
<>
<Modal.Content>
<p className="text-sm text-foreground-lighter">
This action <span className="text-foreground">cannot</span> be undone. This will
permanently delete the <span className="text-foreground">{orgName}</span>{' '}
organization and remove all of its projects.
</p>
</Modal.Content>
<Modal.Separator />
<Modal.Content>
<Input
id="orgName"
label={
<span>
Please type <span className="font-bold">{orgSlug}</span> to confirm
</span>
}
onChange={(e) => setValue(e.target.value)}
value={value}
placeholder="Enter the string above"
className="w-full"
/>
</Modal.Content>
<Modal.Separator />
<Modal.Content>
<Button
block
size="small"
type="danger"
htmlType="submit"
loading={isDeleting}
disabled={isDeleting}
>
I understand, delete this organization
</Button>
</Modal.Content>
</>
)}
</Form>
</Modal>
</>
)
}
export default DeleteOrganizationButton