Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor TextConfirmModal to be typescript and add Form component

Joshen Limf5f6d0b63d55
diff --git a/studio/components/interfaces/Database/Functions/DeleteFunction.tsx b/studio/components/interfaces/Database/Functions/DeleteFunction.tsx
index 61e7ea7065f..93f0bda6709 100644
--- a/studio/components/interfaces/Database/Functions/DeleteFunction.tsx
+++ b/studio/components/interfaces/Database/Functions/DeleteFunction.tsx
@@ -1,7 +1,7 @@
import { FC, useState } from 'react'
import { observer } from 'mobx-react-lite'
import { useStore } from 'hooks'
-import TextConfirmModal from 'components/to-be-cleaned/ModalsDeprecated/TextConfirmModal'
+import TextConfirmModal from 'components/ui/Modals/TextConfirmModal'
type DeleteFunctionProps = {
func?: any
diff --git a/studio/components/interfaces/Database/Hooks/DeleteHook.tsx b/studio/components/interfaces/Database/Hooks/DeleteHook.tsx
index c31e7d34336..14efa7f8f5b 100644
--- a/studio/components/interfaces/Database/Hooks/DeleteHook.tsx
+++ b/studio/components/interfaces/Database/Hooks/DeleteHook.tsx
@@ -1,7 +1,7 @@
import * as React from 'react'
import { observer } from 'mobx-react-lite'
import { useStore } from 'hooks'
-import TextConfirmModal from 'components/to-be-cleaned/ModalsDeprecated/TextConfirmModal'
+import TextConfirmModal from 'components/ui/Modals/TextConfirmModal'
type DeleteHookProps = {
hook?: any
diff --git a/studio/components/interfaces/Database/Triggers/DeleteTrigger.tsx b/studio/components/interfaces/Database/Triggers/DeleteTrigger.tsx
index 416f72d20e7..62ebf02da44 100644
--- a/studio/components/interfaces/Database/Triggers/DeleteTrigger.tsx
+++ b/studio/components/interfaces/Database/Triggers/DeleteTrigger.tsx
@@ -1,7 +1,7 @@
import { FC, useState } from 'react'
import { observer } from 'mobx-react-lite'
import { useStore } from 'hooks'
-import TextConfirmModal from 'components/to-be-cleaned/ModalsDeprecated/TextConfirmModal'
+import TextConfirmModal from 'components/ui/Modals/TextConfirmModal'
type DeleteTriggerProps = {
trigger?: any
diff --git a/studio/components/to-be-cleaned/ModalsDeprecated/TextConfirmModal.js b/studio/components/to-be-cleaned/ModalsDeprecated/TextConfirmModal.js
index 6158dff956e..22551db86f0 100644
--- a/studio/components/to-be-cleaned/ModalsDeprecated/TextConfirmModal.js
+++ b/studio/components/to-be-cleaned/ModalsDeprecated/TextConfirmModal.js
@@ -1,5 +1,4 @@
-import { useState, useEffect } from 'react'
-import { Modal, Button, Typography, IconAlertCircle, Divider, Input, Alert } from '@supabase/ui'
+import { Modal, Button, Typography, Input, Alert, Form } from '@supabase/ui'
export default function TextConfirmModal({
title,
@@ -13,70 +12,71 @@ export default function TextConfirmModal({
alert,
text,
}) {
- const [confirmValue, setConfirmValue] = useState('')
-
- useEffect(() => {
- // reset confirm value
- if (!visible) {
- setConfirmValue('')
+ const validate = (values) => {
+ const errors = {}
+ if (values.confirmValue.length === 0) {
+ errors.confirmValue = 'Enter the required value.'
+ } else if (values.confirmValue !== confirmString) {
+ errors.confirmValue = 'Value entered does not match.'
}
- }, [visible])
+ return errors
+ }
+
+ const onSubmit = async (values, form) => {
+ console.log('onSUBMITTT', values, confirmString, form)
+ }
return (
- <Modal
- size="small"
- visible={visible}
- header={title}
- customFooter={
- <>
- <div className="w-full">
- <Button
- danger
- disabled={confirmValue != confirmString}
- size="medium"
- type={confirmValue != confirmString ? 'outline' : 'primary'}
- block
- loading={loading}
- onClick={() => onConfirm()}
- >
- {confirmLabel}
- </Button>
+ <Modal hideFooter closable size="small" visible={visible} header={title} onCancel={onCancel}>
+ <Form
+ validateOnBlur
+ initialValues={{ confirmValue: '' }}
+ validate={validate}
+ onSubmit={onSubmit}
+ >
+ {({ isSubmitting }) => (
+ <div className="w-full py-4">
+ <div className="space-y-4">
+ {alert && (
+ <Modal.Content>
+ <Alert variant="warning" withIcon title={alert} />
+ </Modal.Content>
+ )}
+ <Modal.Content>
+ <Typography.Text className="block">
+ <p className="mb-2 text-sm">{text}</p>
+ </Typography.Text>
+ </Modal.Content>
+ <Modal.Seperator />
+ <Modal.Content>
+ <Input
+ id="confirmValue"
+ label={
+ <span>
+ Type <span className="text-scale-1200">{confirmString}</span> to confirm.
+ </span>
+ }
+ placeholder={confirmPlaceholder}
+ />
+ </Modal.Content>
+ <Modal.Seperator />
+ <Modal.Content>
+ <Button
+ block
+ type="danger"
+ size="medium"
+ htmlType="submit"
+ loading={isSubmitting}
+ disabled={isSubmitting}
+ onClick={() => onSubmit()}
+ >
+ {confirmLabel}
+ </Button>
+ </Modal.Content>
+ </div>
</div>
- </>
- }
- onConfirm={onConfirm}
- onCancel={onCancel}
- closable
- >
- <div className="w-full py-4">
- <div className="space-y-4">
- {alert && (
- <Modal.Content>
- <Alert variant="warning" withIcon title={alert} />
- </Modal.Content>
- )}
- <Modal.Content>
- <Typography.Text className="block">
- <p className="mb-2 text-sm">{text}</p>
- </Typography.Text>
- </Modal.Content>
- <Modal.Seperator />
- <Modal.Content>
- <Input
- label={
- <>
- <span>
- Type <span className="text-scale-1200">{confirmString}</span> to confirm.
- </span>
- </>
- }
- value={confirmValue}
- onChange={(e) => setConfirmValue(e.target.value)}
- placeholder={confirmPlaceholder}
- />
- </Modal.Content>
- </div>
- </div>
+ )}
+ </Form>
</Modal>
)
}
diff --git a/studio/components/ui/Modals/TextConfirmModal.tsx b/studio/components/ui/Modals/TextConfirmModal.tsx
new file mode 100644
index 00000000000..cdd83cf52f3
--- /dev/null
+++ b/studio/components/ui/Modals/TextConfirmModal.tsx
@@ -0,0 +1,98 @@
+import { FC, ReactNode } from 'react'
+import { Modal, Button, Typography, Input, Alert, Form } from '@supabase/ui'
+
+interface Props {
+ loading: boolean
+ visible: boolean
+ title: string
+ confirmLabel: string
+ confirmPlaceholder: string
+ confirmString: string
+ alert: string
+ text: string | ReactNode
+ onConfirm: () => void
+ onCancel: () => void
+}
+
+const TextConfirmModal: FC<Props> = ({
+ title,
+ onConfirm,
+ visible,
+ onCancel,
+ loading,
+ confirmLabel,
+ confirmPlaceholder,
+ confirmString,
+ alert,
+ text,
+}) => {
+ // [Joshen] Have to keep the loading prop here as this component itself doesn't
+ // directly trigger any potential async job that follows onConfirm. It only triggers
+ // the onConfirm callback function, and hence if anything fails in the callback,
+ // have to depend on loading prop to unfreeze the UI state
+
+ const validate = (values: any) => {
+ const errors: any = {}
+ if (values.confirmValue.length === 0) {
+ errors.confirmValue = 'Enter the required value.'
+ } else if (values.confirmValue !== confirmString) {
+ errors.confirmValue = 'Value entered does not match.'
+ }
+ return errors
+ }
+
+ return (
+ <Modal hideFooter closable size="small" visible={visible} header={title} onCancel={onCancel}>
+ <Form
+ validateOnBlur
+ initialValues={{ confirmValue: '' }}
+ validate={validate}
+ onSubmit={onConfirm}
+ >
+ {({ isSubmitting }: { isSubmitting: boolean }) => (
+ <div className="w-full py-4">
+ <div className="space-y-4">
+ {alert && (
+ <Modal.Content>
+ <Alert variant="warning" withIcon title={alert} />
+ </Modal.Content>
+ )}
+ <Modal.Content>
+ <Typography.Text className="block">
+ <p className="mb-2 text-sm">{text}</p>
+ </Typography.Text>
+ </Modal.Content>
+ <Modal.Seperator />
+ <Modal.Content>
+ <Input
+ id="confirmValue"
+ label={
+ <span>
+ Type <span className="text-scale-1200">{confirmString}</span> to confirm.
+ </span>
+ }
+ placeholder={confirmPlaceholder}
+ />
+ </Modal.Content>
+ <Modal.Seperator />
+ <Modal.Content>
+ <Button
+ block
+ type="danger"
+ size="medium"
+ htmlType="submit"
+ loading={loading}
+ disabled={loading}
+ >
+ {confirmLabel}
+ </Button>
+ </Modal.Content>
+ </div>
+ </div>
+ )}
+ </Form>
+ </Modal>
+ )
+}
+
+export default TextConfirmModal
diff --git a/studio/pages/index.tsx b/studio/pages/index.tsx
index 88a174efc0f..c913013180d 100644
--- a/studio/pages/index.tsx
+++ b/studio/pages/index.tsx
@@ -1,4 +1,3 @@
-import { NextPage } from 'next'
import { useEffect, useState } from 'react'
import { observer } from 'mobx-react-lite'
import { NextRouter, useRouter } from 'next/router'
@@ -16,7 +15,7 @@ import { AccountLayoutWithoutAuth } from 'components/layouts'
import Landing from 'components/interfaces/Home/Landing'
import ProjectList from 'components/interfaces/Home/ProjectList'
import OrganizationDropdown from 'components/to-be-cleaned/Dropdown/OrganizationDropdown'
-import TextConfirmModal from 'components/to-be-cleaned/ModalsDeprecated/TextConfirmModal'
+import TextConfirmModal from 'components/ui/Modals/TextConfirmModal'
const Home: NextPageWithLayout = () => {
const { app, ui } = useStore()
@@ -93,7 +92,7 @@ const Home: NextPageWithLayout = () => {
loading={isDeletingProject}
confirmLabel="Confirm delete"
confirmPlaceholder="Type in name of project"
- confirmString={selectedProjectToDelete?.name}
+ confirmString={selectedProjectToDelete?.name ?? ''}
onCancel={() => setSelectedProjectToDelete(undefined)}
onConfirm={() => onDeleteProject(selectedProjectToDelete)}
/>
diff --git a/studio/pages/org/[slug]/settings.tsx b/studio/pages/org/[slug]/settings.tsx
index a83fbad4326..5c25e938271 100644
--- a/studio/pages/org/[slug]/settings.tsx
+++ b/studio/pages/org/[slug]/settings.tsx
@@ -30,7 +30,7 @@ import Table from 'components/to-be-cleaned/Table'
import Panel from 'components/to-be-cleaned/Panel'
import { confirmAlert } from 'components/to-be-cleaned/ModalsDeprecated/ConfirmModal'
import InviteMemberModal from 'components/to-be-cleaned/ModalsDeprecated/InviteMemberModal'
-import TextConfirmModal from 'components/to-be-cleaned/ModalsDeprecated/TextConfirmModal'
+import TextConfirmModal from 'components/ui/Modals/TextConfirmModal'
import SchemaFormPanel from 'components/to-be-cleaned/forms/SchemaFormPanel'
import { NextPageWithLayout, Project } from 'types'
diff --git a/studio/pages/project/[ref]/settings/general.tsx b/studio/pages/project/[ref]/settings/general.tsx
index c6f4978d7d0..bf0e3bb4572 100644
--- a/studio/pages/project/[ref]/settings/general.tsx
+++ b/studio/pages/project/[ref]/settings/general.tsx
@@ -14,12 +14,12 @@ import { SettingsLayout } from 'components/layouts'
import Panel from 'components/to-be-cleaned/Panel'
import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
import SchemaFormPanel from 'components/to-be-cleaned/forms/SchemaFormPanel'
-import TextConfirmModal from 'components/to-be-cleaned/ModalsDeprecated/TextConfirmModal'
+import TextConfirmModal from 'components/ui/Modals/TextConfirmModal'
const ProjectSettings = () => {
return (
<SettingsLayout title="General">
- <div className="content w-full h-full overflow-y-auto">
+ <div className="content h-full w-full overflow-y-auto">
<div className="mx-auto w-full">
<GeneralSettings />
</div>
@@ -97,7 +97,7 @@ const GeneralSettings = observer(() => {
}
return (
- <article className="p-4 max-w-4xl">
+ <article className="max-w-4xl p-4">
<section>
<SchemaFormPanel
title="General"
@@ -112,7 +112,7 @@ const GeneralSettings = observer(() => {
<section>
<Panel
title={
- <h5 key="panel-title" className="text-base mb-0">
+ <h5 key="panel-title" className="mb-0 text-base">
Infrastructure
</h5>
}
@@ -126,15 +126,15 @@ const GeneralSettings = observer(() => {
layout="horizontal"
/>
</Panel.Content>
- <Panel.Content className="border-t border-panel-border-interior-light dark:border-panel-border-interior-dark">
+ <Panel.Content className="border-panel-border-interior-light dark:border-panel-border-interior-dark border-t">
<Input readOnly disabled value={project?.region} label="Region" layout="horizontal" />
</Panel.Content>
- <Panel.Content className="border-t border-panel-border-interior-light dark:border-panel-border-interior-dark">
- <div className="w-full flex items-center justify-between">
+ <Panel.Content className="border-panel-border-interior-light dark:border-panel-border-interior-dark border-t">
+ <div className="flex w-full items-center justify-between">
<div>
<p>Restart server</p>
<div style={{ maxWidth: '420px' }}>
- <p className="opacity-50 text-sm">
+ <p className="text-sm opacity-50">
Your project will not be available for a few minutes.
</p>
</div>
@@ -154,7 +154,7 @@ const GeneralSettings = observer(() => {
title="Deleting this project will also remove your database."
>
<div className="flex flex-col">
- <p className="block mb-4">
+ <p className="mb-4 block">
Make sure you have made a backup if you want to keep your data.
</p>
<ProjectDeleteModal project={project} />