Updated sign up interface

This commit is contained in:
Dmitriy Shishkov 2023-08-08 00:59:26 +03:00
parent 2b5a917107
commit e1e1244b3a
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
5 changed files with 12 additions and 54 deletions

View File

@ -1,18 +1,10 @@
import { API_URL } from '../../config'
import { fallbackTo, isString } from '../../utils/types'
import { SignUp, SignUpBody, SignUpResponse } from './types'
import { SignUp, SignUpResponse } from './types'
const composeSignUpURL = () => (
API_URL + '/signup?'
)
const composeSignUpBody = (formData: FormData): SignUpBody => ({
email: fallbackTo(formData.get('email'), isString, ''),
password: fallbackTo(formData.get('password'), isString, ''),
name: fallbackTo(formData.get('name'), isString, ''),
surname: fallbackTo(formData.get('surname'), isString, ''),
})
const processSignUp = (data: SignUpResponse): SignUp => {
if (!data.Success) {
throw new Error(data.Message)
@ -21,4 +13,4 @@ const processSignUp = (data: SignUpResponse): SignUp => {
return true
}
export { composeSignUpURL, composeSignUpBody, processSignUp }
export { composeSignUpURL, processSignUp }

View File

@ -1,12 +1,5 @@
import { isConst, isObject } from '../../utils/types'
type SignUpBody = {
email: string,
password: string,
name: string,
surname: string,
}
type SignUpResponse = {
Success: true,
} | {
@ -25,6 +18,6 @@ const isSignUpResponse = (obj: unknown): obj is SignUpResponse => (
type SignUp = boolean
export type { SignUpBody, SignUpResponse, SignUp }
export type { SignUpResponse, SignUp }
export { isSignUpResponse }

View File

@ -1,22 +1,12 @@
import { API_URL } from '../../config'
import { fallbackTo, isString } from '../../utils/types'
import { Token, TokenResponse } from './types'
const composeTokenURL = () => (
API_URL + '/token?'
)
const composeSignInBody = (formData: FormData) => {
const resFD = new FormData()
resFD.append('username', fallbackTo(formData.get('email'), isString, ''))
resFD.append('password', fallbackTo(formData.get('password'), isString, ''))
return resFD
}
const processToken = (data: TokenResponse): Token => {
return data.access_token
}
export { composeTokenURL, composeSignInBody, processToken }
export { composeTokenURL, processToken }

View File

@ -2,8 +2,6 @@ import { FormEventHandler, useCallback } from 'react'
import { Button, Form } from 'react-bootstrap'
import { useSignIn, useSignUp } from '../hooks/api'
import { composeSignUpBody } from '../api/signup'
import { composeSignInBody } from '../api/token'
type AuthFormProps = {
register: boolean,
@ -23,11 +21,11 @@ function AuthForm({ goBack, register }: AuthFormProps) {
void (async () => {
const accountCreated = register ? (
await handleSignUp(composeSignUpBody(formData))
await handleSignUp(formData)
) : true
if (accountCreated) {
if (await handleSignIn(composeSignInBody(formData))) {
if (await handleSignIn(formData)) {
goBack()
}
}
@ -37,23 +35,11 @@ function AuthForm({ goBack, register }: AuthFormProps) {
return (
<Form onSubmit={handleAuth}>
<Form.Group className='mb-3' controlId='email'>
<Form.Label>Почта</Form.Label>
<Form.Control name='email' type='email' required />
<Form.Group className='mb-3' controlId='username'>
<Form.Label>Как вас называть?</Form.Label>
<Form.Control name='username' type='text' required />
</Form.Group>
{register && <>
<Form.Group className='mb-3' controlId='name'>
<Form.Label>Имя</Form.Label>
<Form.Control name='name' type='text' required />
</Form.Group>
<Form.Group className='mb-3' controlId='surname'>
<Form.Label>Фамилия</Form.Label>
<Form.Control name='surname' type='text' required />
</Form.Group>
</>}
<Form.Group className='mb-3' controlId='password'>
<Form.Label>Пароль</Form.Label>
<Form.Control name='password' type='password' required />

View File

@ -1,6 +1,6 @@
import { useSendWithButton } from '..'
import { composeSignUpURL, processSignUp } from '../../api/signup'
import { SignUpBody, isSignUpResponse } from '../../api/signup/types'
import { isSignUpResponse } from '../../api/signup/types'
function useSignUp() {
const { doSend, button } = useSendWithButton(
@ -14,12 +14,9 @@ function useSignUp() {
processSignUp,
)
async function handleSignUp(data: SignUpBody) {
async function handleSignUp(formData: FormData) {
const res = await doSend({}, {
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
body: formData,
})
return res ?? false