Skip to content

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

Swaps mobx for <Form> component

Terry Sutton25fead2c008b
diff --git a/studio/components/to-be-cleaned/ModalsDeprecated/InviteMemberModal.tsx b/studio/components/to-be-cleaned/ModalsDeprecated/InviteMemberModal.tsx
index 561215fe7be..d44bc81f348 100644
--- a/studio/components/to-be-cleaned/ModalsDeprecated/InviteMemberModal.tsx
+++ b/studio/components/to-be-cleaned/ModalsDeprecated/InviteMemberModal.tsx
@@ -1,10 +1,7 @@
-import { createContext, useState, useEffect } from 'react'
+import { useState, useEffect } from 'react'
import { isNil } from 'lodash'
-import { observer, useLocalObservable } from 'mobx-react-lite'
import { post } from 'lib/common/fetch'
-import { Button, IconKey, IconMail, Input } from '@supabase/ui'
-import { Modal } from '@supabase/ui'
-
+import { Button, IconKey, IconMail, Form, Input, Modal } from '@supabase/ui'
import { API_URL } from 'lib/constants'
import { useOrganizationDetail, useStore } from 'hooks'
import { toJS } from 'mobx'
@@ -27,54 +24,38 @@ import { Member } from 'types'
*
*/
-/**
- * Modal to invite member to Organization
- *
- * @param {Object} organization // organization detail
- * @param {Array} members // organization members list
- * @param {Object} user // current user detail
- */
-
-const PageContext = createContext(null)
-
function InviteMemberModal({ organization, members = [], user }: any) {
- const PageState = useLocalObservable(() => ({
- members: [],
- addMemberLoading: false,
- emailAddress: '',
- emailIsValid() {
- return String(this.emailAddress)
- .toLowerCase()
- .match(
- /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
- )
- },
- }))
+ const initialValues = { email: '' }
const { ui } = useStore()
const { mutateOrgMembers } = useOrganizationDetail(ui.selectedOrganization?.slug || '')
const [isOpen, setIsOpen] = useState(false)
- const { id: orgId, slug: orgSlug } = organization
+ const [emailAddress, setEmailAddress] = useState('')
+ const [memberList, setMemberList] = useState([])
+ const [addMemberLoading, setAddMemberLoading] = useState(false)
+
+ const { slug: orgSlug } = organization
useEffect(() => {
- PageState.members = members
+ setMemberList(members)
}, [members])
function toggle() {
// reset data before showing modal again
if (!isOpen) {
- PageState.addMemberLoading = false
- PageState.emailAddress = ''
+ setAddMemberLoading(false)
+ setEmailAddress('')
}
setIsOpen(!isOpen)
}
async function addMember() {
- PageState.addMemberLoading = true
+ setAddMemberLoading(true)
const response = await post(`${API_URL}/organizations/${orgSlug}/members/invite`, {
- invited_email: PageState.emailAddress,
+ invited_email: emailAddress,
owner_id: toJS(user.id),
})
+
if (isNil(response)) {
ui.setNotification({ category: 'error', message: 'Failed to add member' })
} else if (response?.error) {
@@ -82,7 +63,7 @@ function InviteMemberModal({ organization, members = [], user }: any) {
category: 'error',
message: `Failed to add member: ${response.error.message}`,
})
- PageState.addMemberLoading = false
+ setAddMemberLoading(false)
} else {
const newMember: Member = {
// [Joshen] Setting a random id for now to fit the Member interface
@@ -95,19 +76,23 @@ function InviteMemberModal({ organization, members = [], user }: any) {
username: response.invited_email[0],
},
}
- mutateOrgMembers([...PageState.members, newMember])
- ui.setNotification({ category: 'success', message: 'Successfully added new member.' })
+ mutateOrgMembers([...memberList, newMember])
+
+ ui.setNotification({
+ category: 'success',
+ message: 'Successfully added new member.',
+ })
toggle()
}
}
function onEmailInputChange(e: any) {
- PageState.emailAddress = e.target.value
+ setEmailAddress(e.target.value)
}
return (
- <PageContext.Provider value={PageState}>
+ <>
<Button onClick={toggle}>Invite</Button>
<Modal
size="small"
@@ -123,33 +108,45 @@ function InviteMemberModal({ organization, members = [], user }: any) {
<div className="w-full space-y-4 py-4">
<Modal.Content>
<div className="text-center">
- <Input
- icon={<IconMail />}
- autoFocus
- id="email"
- placeholder="Enter email address"
- onChange={onEmailInputChange}
- value={PageState.emailAddress}
- className="w-full"
- />
- </div>
- </Modal.Content>
- <Modal.Seperator />
- <Modal.Content>
- <div className="">
- <Button
- onClick={addMember}
- loading={PageState.addMemberLoading}
- disabled={!PageState.emailIsValid()}
- block
+ <Form
+ validate={(values) => {
+ const errors: any = {}
+ if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
+ errors.email = 'Please enter a valid email address'
+ }
+ return errors
+ }}
+ initialValues={initialValues}
+ onSubmit={addMember}
>
- Invite new member
- </Button>
+ {() => (
+ <div className="w-full py-4">
+ <div className="space-y-4">
+ <Modal.Content>
+ <Input
+ id="email"
+ icon={<IconMail />}
+ autoFocus
+ placeholder="Enter email address"
+ onChange={onEmailInputChange}
+ label="Email address"
+ />
+ </Modal.Content>
+
+ <Modal.Content>
+ <Button block size="medium" htmlType="submit" loading={addMemberLoading}>
+ Invite new member
+ </Button>
+ </Modal.Content>
+ </div>
+ </div>
+ )}
+ </Form>
</div>
</Modal.Content>
</div>
</Modal>
- </PageContext.Provider>
+ </>
)
}
-export default observer(InviteMemberModal)
+export default InviteMemberModal