-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
242 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React from 'react'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import Cookies from 'js-cookie'; | ||
|
||
import UserForm from '../lib/SimpleForm'; | ||
import { Attribute, AttributeType } from '../lib/SimpleForm/types'; | ||
import { useNileConfig } from '../context'; | ||
|
||
import { Props } from './types'; | ||
|
||
export default function SignUpForm(props: Props) { | ||
const { buttonText = 'Sign up', onSuccess, onError, attributes } = props; | ||
const { workspace, database, basePath, allowClientCookies } = useNileConfig(); | ||
const fetchPath = `${basePath}/workspaces/${workspace}/databases/${database}/users`; | ||
|
||
const mutation = useMutation( | ||
async (data: { email: string; password: string }) => { | ||
const { email, password, ...metadata } = data; | ||
if (Object.keys(metadata).length > 0) { | ||
// eslint-disable-next-line no-console | ||
console.warn('additional metadata not supported yet.'); | ||
} | ||
|
||
const res = await fetch(fetchPath, { | ||
method: 'POST', | ||
body: JSON.stringify({ email, password }), | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}).catch((e) => e); | ||
try { | ||
if (res) { | ||
if (allowClientCookies) { | ||
Cookies.set('token', res.token.token, { | ||
'max-age': res.token.maxAge, | ||
}); | ||
} | ||
return await res.json(); | ||
} | ||
} catch (e) { | ||
return e; | ||
} | ||
}, | ||
{ | ||
onSuccess: (_, data) => { | ||
onSuccess && onSuccess(data); | ||
}, | ||
onError: (error, data) => { | ||
onError && onError(error as Error, data); | ||
}, | ||
} | ||
); | ||
|
||
const completeAttributes = React.useMemo(() => { | ||
const mainAttributes: Attribute[] = [ | ||
{ | ||
name: 'email', | ||
label: 'Email', | ||
type: AttributeType.Text, | ||
defaultValue: '', | ||
required: true, | ||
}, | ||
{ | ||
name: 'password', | ||
label: 'Password', | ||
type: AttributeType.Password, | ||
defaultValue: '', | ||
required: true, | ||
}, | ||
]; | ||
if (attributes && attributes.length > 0) { | ||
return mainAttributes.concat(attributes); | ||
} | ||
return mainAttributes; | ||
}, [attributes]); | ||
|
||
return ( | ||
<UserForm | ||
mutation={mutation} | ||
buttonText={buttonText} | ||
attributes={completeAttributes} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './SignUpForm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Attribute } from '../lib/SimpleForm/types'; | ||
|
||
type LoginInfo = { email: string; password: string }; | ||
type SignInSuccess = (loginInfo: LoginInfo) => void; | ||
|
||
export interface Props { | ||
onSuccess: SignInSuccess; | ||
onError?: (e: Error, info: LoginInfo) => void; | ||
attributes?: Attribute[]; | ||
buttonText?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/react/stories/SignUpForm/NewUserSignUp.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { Meta, Story } from '@storybook/react'; | ||
|
||
import { NileProvider } from '../../src/context'; | ||
import SignUpForm from '../../src/SignUpForm'; | ||
|
||
const meta: Meta = { | ||
component: SignUpForm, | ||
parameters: { | ||
controls: { expanded: false }, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
const Template: Story<null> = () => ( | ||
<NileProvider workspace="workspace" database="database"> | ||
<div style={{ maxWidth: '20rem', margin: '0 auto' }}> | ||
<SignUpForm onSuccess={() => alert('success!')} /> | ||
</div> | ||
</NileProvider> | ||
); | ||
|
||
// By passing using the Args format for exported stories, you can control the props for a component for reuse in a test | ||
// https://storybook.js.org/docs/react/workflows/unit-testing | ||
export const Default = Template.bind({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.