forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sign-up): add sign up screen & form
- Loading branch information
Showing
11 changed files
with
8,223 additions
and
11,284 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,48 @@ | ||
import { createMutation } from 'react-query-kit'; | ||
|
||
import { client } from '../common'; | ||
|
||
type Variables = { | ||
email: string; | ||
name: string; | ||
password: string; | ||
passwordConfirmation: string; | ||
}; | ||
|
||
type Response = { | ||
status: string; | ||
data: { | ||
id: string; | ||
email: string; | ||
name: string; | ||
provider: string; | ||
uid: string; | ||
allowPasswordChange: boolean; | ||
createdAt: string; | ||
updatedAt: string; | ||
nickname?: string; | ||
image?: string; | ||
birthday?: string; | ||
}; | ||
}; | ||
|
||
const signUp = async (variables: Variables) => { | ||
const { data } = await client({ | ||
url: '/v1/users', | ||
method: 'POST', | ||
data: { | ||
user: { | ||
email: variables.email, | ||
password: variables.password, | ||
password_confirmation: variables.passwordConfirmation, | ||
name: variables.name, | ||
}, | ||
}, | ||
}); | ||
|
||
return data; | ||
}; | ||
|
||
export const useSignUp = createMutation<Response, Variables>({ | ||
mutationFn: (variables) => signUp(variables), | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useRouter } from 'expo-router'; | ||
import React from 'react'; | ||
import { showMessage } from 'react-native-flash-message'; | ||
|
||
import { useSignUp } from '@/api/auth/use-sign-up'; | ||
import type { SignUpFormProps } from '@/components/sign-up-form'; | ||
import { SignUpForm } from '@/components/sign-up-form'; | ||
import { FocusAwareStatusBar } from '@/ui'; | ||
|
||
export default function SignIn() { | ||
const router = useRouter(); | ||
|
||
const { mutate: signUp, isPending } = useSignUp({ | ||
onSuccess: () => { | ||
router.push('/'); | ||
}, | ||
onError: (error) => showMessage({ message: error.message, type: 'danger' }), | ||
}); | ||
|
||
const onSubmit: SignUpFormProps['onSubmit'] = (data) => { | ||
signUp(data); | ||
}; | ||
|
||
return ( | ||
<> | ||
<FocusAwareStatusBar /> | ||
<SignUpForm onSubmit={onSubmit} isPending={isPending} /> | ||
</> | ||
); | ||
} |
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,97 @@ | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { type SubmitHandler, useForm } from 'react-hook-form'; | ||
import { KeyboardAvoidingView } from 'react-native'; | ||
import z from 'zod'; | ||
|
||
import { Button, ControlledInput,Text, View } from '@/ui'; | ||
|
||
const MIN_PASSWORD_LENGTH = 6; | ||
|
||
const passwordSchema = z | ||
.string({ required_error: 'Password is required' }) | ||
.min(MIN_PASSWORD_LENGTH, 'Password must be at least 6 characters'); | ||
|
||
const schema = z | ||
.object({ | ||
email: z | ||
.string({ required_error: 'Email is required' }) | ||
.email('Invalid email format'), | ||
name: z.string({ required_error: 'Name is required' }), | ||
password: passwordSchema, | ||
passwordConfirmation: z.string({ | ||
required_error: 'Password confirmation is required', | ||
}), | ||
}) | ||
.refine((data) => data.password === data.passwordConfirmation, { | ||
message: 'Passwords do not match', | ||
path: ['passwordConfirmation'], | ||
}); | ||
|
||
export type FormType = z.infer<typeof schema>; | ||
|
||
export type SignUpFormProps = { | ||
onSubmit?: SubmitHandler<FormType>; | ||
isPending?: boolean; | ||
}; | ||
|
||
export const SignUpForm = ({ | ||
onSubmit = () => {}, | ||
isPending = false, | ||
}: SignUpFormProps) => { | ||
const { handleSubmit, control } = useForm<FormType>({ | ||
resolver: zodResolver(schema), | ||
}); | ||
|
||
return ( | ||
<KeyboardAvoidingView | ||
className="flex-1" | ||
behavior="padding" | ||
keyboardVerticalOffset={10} | ||
> | ||
<View className="flex-1 justify-center p-4"> | ||
<Text testID="form-title" className="pb-6 text-center text-2xl"> | ||
Sign Up | ||
</Text> | ||
|
||
<ControlledInput | ||
testID="email-input" | ||
autoCapitalize="none" | ||
autoComplete="email" | ||
control={control} | ||
name="email" | ||
label="Email" | ||
/> | ||
<ControlledInput | ||
testID="name-input" | ||
control={control} | ||
name="name" | ||
label="Name" | ||
/> | ||
<ControlledInput | ||
testID="password-input" | ||
control={control} | ||
name="password" | ||
label="Password" | ||
placeholder="***" | ||
secureTextEntry={true} | ||
/> | ||
<ControlledInput | ||
testID="password-confirmation-input" | ||
control={control} | ||
name="passwordConfirmation" | ||
label="Password Confirmation" | ||
placeholder="***" | ||
secureTextEntry={true} | ||
/> | ||
|
||
<Button | ||
testID="sign-up-button" | ||
label="Sign Up" | ||
onPress={handleSubmit(onSubmit)} | ||
loading={isPending} | ||
disabled={isPending} | ||
/> | ||
</View> | ||
</KeyboardAvoidingView> | ||
); | ||
}; |
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