Skip to content

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

chore: email templates form working

Jonathan Summers-Muir20bcc2ea8e76
diff --git a/studio/components/interfaces/Auth/AutoSchemaForm.tsx b/studio/components/interfaces/Auth/AutoSchemaForm.tsx
index 741f850edbf..080e66f5093 100644
--- a/studio/components/interfaces/Auth/AutoSchemaForm.tsx
+++ b/studio/components/interfaces/Auth/AutoSchemaForm.tsx
@@ -1,9 +1,6 @@
-import { Form, Input, InputNumber, Loading, Toggle } from '@supabase/ui'
+import { Form, Input, InputNumber, Toggle } from '@supabase/ui'
import { useStore } from 'hooks'
-import { toJS } from 'mobx'
import { observer } from 'mobx-react-lite'
-import React from 'react'
-import { useState } from 'react'
import { useEffect } from 'react'
import { boolean, number, object, string } from 'yup'
import {
diff --git a/studio/components/interfaces/Auth/EmailTemplates.tsx b/studio/components/interfaces/Auth/EmailTemplates.tsx
index b296715a916..d4b6e5addb9 100644
--- a/studio/components/interfaces/Auth/EmailTemplates.tsx
+++ b/studio/components/interfaces/Auth/EmailTemplates.tsx
@@ -3,7 +3,7 @@ import CodeEditor from 'components/ui/CodeEditor'
import InformationBox from 'components/ui/InformationBox'
import { useStore } from 'hooks'
import { observer } from 'mobx-react-lite'
-import { useEffect } from 'react'
+import { useEffect, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import { TEMPLATES_SCHEMAS } from 'stores/authConfig/schema'
import {
@@ -34,9 +34,13 @@ const EmailTemplates = observer(() => {
size="small"
scrollable
>
- {TEMPLATES_SCHEMAS.map((template) => {
+ {TEMPLATES_SCHEMAS.map((template, i) => {
+ const [bodyValue, setBodyValue] = useState('')
+
const INITIAL_VALUES: { [x: string]: string } = {}
+ // console.log('template.properties', template.properties)
+
/**
* construct values for INITIAL_VALUES
*
@@ -47,18 +51,36 @@ const EmailTemplates = observer(() => {
INITIAL_VALUES[key] = authConfig.config[key] ?? ''
})
+ console.log(`INITIAL_VALUES for ${template.id}`, INITIAL_VALUES)
+
const messageSlug = `MAILER_TEMPLATES_${template.id}_CONTENT`
const messageProperty = template.properties[messageSlug]
+ const panelId = template.title.trim().replace(/\s+/g, '-')
+ const formId = `auth-config-email-templates--${template.id}`
+
+ delete INITIAL_VALUES[messageSlug]
+
return (
- <Tabs.Panel id={template.title.trim().replace(/\s+/g, '-')} label={template.title}>
+ <Tabs.Panel id={panelId} label={template.title} key={panelId}>
<Form
- id="auth-config-email-templates"
+ id={formId}
className="!border-t-0"
initialValues={INITIAL_VALUES}
onSubmit={async (values: any, { setSubmitting, resetForm }: any) => {
+ console.log('hello world')
+
const payload = { ...values }
+ // remove rendundant value
+ delete payload[messageSlug]
+
+ if (messageProperty) {
+ payload[messageSlug] = bodyValue
+ }
+
+ console.log('payload', payload)
+
try {
setSubmitting(true)
await authConfig.update(payload)
@@ -68,6 +90,12 @@ const EmailTemplates = observer(() => {
category: 'success',
message: `Updated settings`,
})
+
+ resetForm({
+ values: values,
+ initialValues: values,
+ })
+ setBodyValue(authConfig?.config?.[messageSlug])
} catch (error) {
ui.setNotification({
category: 'error',
@@ -77,20 +105,33 @@ const EmailTemplates = observer(() => {
}
}}
>
- {({ isSubmitting, handleReset, resetForm, values, initialValues }: any) => {
+ {({ isSubmitting, resetForm, values, initialValues }: any) => {
/**
* Tracks changes in form
*/
- const hasChanges = JSON.stringify(values) !== JSON.stringify(initialValues)
+ const hasChanges = () => {
+ if (JSON.stringify(values) !== JSON.stringify(initialValues)) return true
+
+ if (authConfig?.config?.[messageSlug]) {
+ if (
+ authConfig?.config?.[messageSlug] &&
+ authConfig?.config?.[messageSlug] !== bodyValue
+ )
+ return true
+ }
+
+ return false
+ }
/**
* Form is reset once remote data is loaded in store
*/
useEffect(() => {
resetForm({
- values: authConfig.config,
- initialValues: authConfig.config,
+ values: INITIAL_VALUES,
+ initialValues: INITIAL_VALUES,
})
+ setBodyValue(authConfig?.config?.[messageSlug])
}, [authConfig.isLoaded])
return (
@@ -185,9 +226,10 @@ const EmailTemplates = observer(() => {
loading={!isLoaded}
language="html"
className="!mb-0 h-96 overflow-hidden rounded border"
- defaultValue={authConfig?.config?.[messageSlug] || ''}
- onInputChange={(e) => console.log(e)}
+ // defaultValue={bodyValue}
+ onInputChange={(e) => setBodyValue(e)}
options={{ wordWrap: 'off', contextmenu: false }}
+ value={bodyValue}
/>
</div>
</>
@@ -199,10 +241,11 @@ const EmailTemplates = observer(() => {
values: authConfig.config,
initialValues: authConfig.config,
})
+ setBodyValue(authConfig?.config?.[messageSlug])
}}
+ form={formId}
isSubmitting={isSubmitting}
- hasChanges={hasChanges}
- helper={'Learn more about global Auth settings'}
+ hasChanges={hasChanges()}
/>
</div>
</FormSectionContent>
diff --git a/studio/components/interfaces/Auth/SmtpForm.tsx b/studio/components/interfaces/Auth/SmtpForm.tsx
index ed7df1185e6..e7d145711af 100644
--- a/studio/components/interfaces/Auth/SmtpForm.tsx
+++ b/studio/components/interfaces/Auth/SmtpForm.tsx
@@ -20,12 +20,6 @@ const SmtpForm = observer(() => {
const { authConfig, ui } = useStore()
const [enableSmtp, setEnableSmtp] = useState(false)
- // let enableSmtp = false
-
- // function setEnableSmtp(value: boolean) {
- // enableSmtp = value
- // }
-
function generateInitialValues(config: any) {
return {
ENABLE_SMTP: enableSmtp,
@@ -39,8 +33,6 @@ const SmtpForm = observer(() => {
}
}
- // const INITIAL_VALUES = generateInitialValues(authConfig.config)
-
function checkSmtpState(config: any, resetForm: () => void) {
if (
config.SMTP_ADMIN_EMAIL &&
@@ -51,11 +43,9 @@ const SmtpForm = observer(() => {
config.SMTP_PORT &&
config.SMTP_MAX_FREQUENCY > 0
) {
- console.log('TRUE')
setEnableSmtp(true)
return resetForm
} else {
- console.log('FALSE')
setEnableSmtp(false)
return resetForm
}
@@ -81,7 +71,7 @@ const SmtpForm = observer(() => {
is: () => {
return enableSmtp
},
- then: (schema) => schema.email('Must be a valid email').required('Admin Email is required'),
+ then: (schema) => schema.email('Must be a valid email').required('Sender email is required'),
otherwise: (schema) => schema,
}),
SMTP_SENDER_NAME: string().when([], {
@@ -112,16 +102,17 @@ const SmtpForm = observer(() => {
.max(65535, 'Must be a valid port number no more than 65535'),
otherwise: (schema) => schema,
}),
-
SMTP_MAX_FREQUENCY: number().when([], {
is: () => {
return enableSmtp
},
then: (schema) =>
- schema.min(1, 'Must be more than 0').max(32767, 'Must not be more than 32,767 an hour'),
+ schema
+ .required('Rate limit is required.')
+ .min(1, 'Must be more than 0')
+ .max(32767, 'Must not be more than 32,767 an hour'),
otherwise: (schema) => schema,
}),
-
SMTP_USER: string().when([], {
is: () => {
return enableSmtp
@@ -139,11 +130,12 @@ const SmtpForm = observer(() => {
})
const isLoaded = authConfig.isLoaded
+ const formId = 'auth-config-smtp-form'
return (
<>
<Form
- id="auth-config-general-form"
+ id={formId}
initialValues={generateInitialValues(authConfig.config)}
onSubmit={async (values: any, { setSubmitting, resetForm }: any) => {
const payload = { ...values }
@@ -170,7 +162,6 @@ const SmtpForm = observer(() => {
category: 'success',
message: `Updated settings`,
})
- console.log('payload', payload)
checkSmtpState(
payload,
@@ -189,7 +180,7 @@ const SmtpForm = observer(() => {
}}
validationSchema={schema}
>
- {({ isSubmitting, handleReset, resetForm, values, initialValues }: any) => {
+ {({ isSubmitting, resetForm, values, initialValues }: any) => {
/**
* Tracks changes in form
*/
@@ -225,6 +216,7 @@ const SmtpForm = observer(() => {
})
)
}}
+ form={formId}
isSubmitting={isSubmitting}
hasChanges={hasChanges}
helper={'Learn more about global Auth settings'}
diff --git a/studio/components/ui/CodeEditor/CodeEditor.tsx b/studio/components/ui/CodeEditor/CodeEditor.tsx
index cffc6a5ccfa..253e2ad4775 100644
--- a/studio/components/ui/CodeEditor/CodeEditor.tsx
+++ b/studio/components/ui/CodeEditor/CodeEditor.tsx
@@ -17,6 +17,7 @@ interface Props {
className?: string
loading?: boolean
options?: EditorProps['options']
+ value?: string
}
const CodeEditor: FC<Props> = ({
@@ -30,6 +31,7 @@ const CodeEditor: FC<Props> = ({
className,
loading,
options,
+ value,
}) => {
const editorRef = useRef()
@@ -81,11 +83,12 @@ const CodeEditor: FC<Props> = ({
return (
<Editor
+ value={value ?? undefined}
path={id}
theme="supabase"
className={`monaco-editor ${className}`}
defaultLanguage={language}
- defaultValue={defaultValue}
+ defaultValue={defaultValue ?? undefined}
loading={loading || <Connecting />}
options={optionsMerged}
onMount={onMount}
diff --git a/studio/components/ui/Forms/FormActions.tsx b/studio/components/ui/Forms/FormActions.tsx
index 88b27ee335d..3a3cb5d935f 100644
--- a/studio/components/ui/Forms/FormActions.tsx
+++ b/studio/components/ui/Forms/FormActions.tsx
@@ -1,4 +1,5 @@
import { Button } from '@supabase/ui'
+import { ButtonHTMLAttributes } from 'react'
interface Props {
/**
@@ -17,13 +18,26 @@ interface Props {
* Helper text to show alongside actions
*/
helper?: React.ReactNode
+ form: React.HTMLProps<HTMLButtonElement>['form']
}
-const FormActions = ({ isSubmitting, handleReset, hasChanges = undefined, helper }: Props) => {
+const FormActions = ({
+ isSubmitting,
+ handleReset,
+ hasChanges = undefined,
+ helper,
+ form,
+}: Props) => {
return (
- <div className="flex w-full items-center justify-between gap-2">
+ <div
+ className={[
+ 'flex w-full items-center gap-2',
+ // justify actions to right if no helper text
+ helper ? 'justify-between' : 'justify-end',
+ ].join(' ')}
+ >
{helper && <span className="text-scale-900 text-xs">{helper}</span>}
- <div className="flex justify-between gap-2">
+ <div className="flex gap-2">
<Button
disabled={!hasChanges && hasChanges !== undefined}
type="default"
@@ -37,7 +51,7 @@ const FormActions = ({ isSubmitting, handleReset, hasChanges = undefined, helper
loading={isSubmitting}
type="primary"
htmlType="submit"
- form="auth-config-general-form"
+ form={form}
>
Save
</Button>
diff --git a/studio/stores/authConfig/ProjectAuthConfigStore.ts b/studio/stores/authConfig/ProjectAuthConfigStore.ts
index 34ed236dde4..4dfe3ffd29c 100644
--- a/studio/stores/authConfig/ProjectAuthConfigStore.ts
+++ b/studio/stores/authConfig/ProjectAuthConfigStore.ts
@@ -68,9 +68,7 @@ export default class ProjectAuthConfigStore implements IProjectAuthConfigStore {
const headers = {
'Content-Type': 'application/json',
}
- console.log('this.baseUrl', this.baseUrl)
const response = await get(this.baseUrl, { headers })
- console.log('response in authCOnfig store', response)
if (response.error) {
throw response.error
}
diff --git a/studio/stores/authConfig/schema/AuthProviders/AuthTemplatesValidation.tsx b/studio/stores/authConfig/schema/AuthProviders/AuthTemplatesValidation.tsx
index c452fe7b4bd..6caef62e8a6 100644
--- a/studio/stores/authConfig/schema/AuthProviders/AuthTemplatesValidation.tsx
+++ b/studio/stores/authConfig/schema/AuthProviders/AuthTemplatesValidation.tsx
@@ -1,5 +1,5 @@
import { FormSchema } from 'types'
-import { boolean, number, object, string } from 'yup'
+import { object, string } from 'yup'
const JSON_SCHEMA_VERSION = 'http://json-schema.org/draft-07/schema#'