| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import React, { useEffect } from "react";
- import {
- SafeAreaView,
- ScrollView,
- View,
- Text,
- TouchableOpacity,
- StyleSheet,
- Alert,
- } from "react-native";
-
- // import DateTimePicker from "@react-native-community/datetimepicker";
- import InputField from "../components/InputField";
-
- import MaterialIcons from "@expo/vector-icons/MaterialIcons";
- import Ionicons from "@expo/vector-icons/Ionicons";
-
- import RegistrationSVG from "../assets/images/registration.svg";
- import GoogleSVG from "../assets/images/google.svg";
- import FacebookSVG from "../assets/images/facebook.svg";
- import TwitterSVG from "../assets/images/twitter.svg";
-
- import CustomButton from "../components/Buttons/CustomButton";
- import { globalStyles } from "../styles/global";
- import Loader from "../components/Loader";
- import { Formik } from "formik";
- import { registerSchema } from "../schemas/registerSchema";
- import { useDispatch, useSelector } from "react-redux";
- import { selectRegisterError } from "../store/selectors/registerSelectors";
- import { selectIsLoadingByActionType } from "../store/selectors/loadingSelectors";
- import { REGISTER_USER_SCOPE } from "../store/actions/register/registerActionConstants";
- import {
- clearRegisterErrors,
- registerUser,
- } from "../store/actions/register/registerActions";
- import useAuthHook from "../hooks/useAuthHook";
- import { fetchAuthProvider } from "../store/actions/authProvider/authProviderActions";
- import { ACCESS_TOKEN } from "../constants/localStorage";
- import { storeData } from "../service/asyncStorage";
-
- const RegisterScreen = ({ navigation }) => {
- const { response, promptAsync } = useAuthHook();
- const dispatch = useDispatch();
- const error = useSelector(selectRegisterError);
-
- const isLoading = useSelector(
- selectIsLoadingByActionType(REGISTER_USER_SCOPE)
- );
-
- const storeToken = async (token) => {
- await storeData(ACCESS_TOKEN, token);
- }
-
- const handleApiResponseSuccess = () => {
- Alert.alert(
- "Success",
- "Successfully registered account, now you can log in",
- [
- {
- text: "OK",
- onPress: () => navigation.navigate("Login"),
- },
- ]
- );
- };
-
- const handleSignup = (values) => {
- const { username, email, password } = values;
- dispatch(clearRegisterErrors());
- dispatch(
- registerUser({ username, email, password, handleApiResponseSuccess })
- );
- };
-
- const handleGoogleAuth = () => {
- promptAsync({ useProxy: true, showInRecents: true });
- };
-
- useEffect(() => {
- if (response?.type === "success") {
- const accessToken = response.authentication.accessToken;
- if (accessToken) {
- storeToken(accessToken);
- dispatch(fetchAuthProvider({ accessToken }));
- }
- }
- }, [response]);
-
- return (
- <SafeAreaView style={globalStyles.safeArea}>
- <Loader visible={isLoading} />
- <ScrollView
- showsVerticalScrollIndicator={false}
- style={{ paddingHorizontal: 25 }}
- >
- <View style={{ alignItems: "center" }}>
- <RegistrationSVG width={300} height={300} />
- </View>
- <Text style={globalStyles.boldText}>Sign Up</Text>
- <View style={styles.providersContainer}>
- <TouchableOpacity
- onPress={handleGoogleAuth}
- style={globalStyles.iconButton}
- >
- <GoogleSVG height={24} width={24} />
- </TouchableOpacity>
- <TouchableOpacity onPress={() => {}} style={globalStyles.iconButton}>
- <FacebookSVG height={24} width={24} />
- </TouchableOpacity>
- <TouchableOpacity onPress={() => {}} style={globalStyles.iconButton}>
- <TwitterSVG height={24} width={24} />
- </TouchableOpacity>
- </View>
- <Text style={globalStyles.regularCenteredText}>
- Or, sign up with email...
- </Text>
- <Formik
- initialValues={{
- username: "",
- email: "",
- password: "",
- confirmPassword: "",
- }}
- validationSchema={registerSchema}
- onSubmit={handleSignup}
- validateOnChange={false}
- validateOnBlur={false}
- >
- {({
- handleChange,
- handleBlur,
- handleSubmit,
- values,
- isValid,
- errors,
- }) => (
- <>
- <InputField
- name={"username"}
- text={values.username}
- onChangeText={handleChange("username")}
- label={"Username"}
- handleBlur={handleBlur("username")}
- icon={
- <Ionicons
- name="person-outline"
- size={20}
- color="#666"
- style={{ marginRight: 5 }}
- />
- }
- />
- {errors.username && (
- <Text style={styles.errorMessage}>{errors.username}</Text>
- )}
- <InputField
- name={"email"}
- text={values.email}
- onChangeText={handleChange("email")}
- label={"Email"}
- handleBlur={handleBlur("email")}
- icon={
- <MaterialIcons
- name="alternate-email"
- size={20}
- color="#666"
- style={{ marginRight: 5 }}
- />
- }
- keyboardType="email-address"
- />
- {errors.email && (
- <Text style={styles.errorMessage}>{errors.email}</Text>
- )}
- <InputField
- name={"password"}
- text={values.password}
- onChangeText={handleChange("password")}
- label={"Password"}
- handleBlur={handleBlur("password")}
- icon={
- <Ionicons
- name="ios-lock-closed-outline"
- size={20}
- color="#666"
- style={{ marginRight: 5 }}
- />
- }
- inputType="password"
- />
- {errors.password && (
- <Text style={styles.errorMessage}>{errors.password}</Text>
- )}
- <InputField
- name={"confirmPassword"}
- text={values.confirmPassword}
- onChangeText={handleChange("confirmPassword")}
- label={"Confirm Password"}
- handleBlur={handleBlur("confirmPassword")}
- icon={
- <Ionicons
- name="ios-lock-closed-outline"
- size={20}
- color="#666"
- style={{ marginRight: 5 }}
- />
- }
- inputType="password"
- />
- {errors.confirmPassword && (
- <Text style={styles.errorMessage}>
- {errors.confirmPassword}
- </Text>
- )}
- {error && <Text style={styles.errorMessage}>{error}</Text>}
- <CustomButton label={"Sign Up"} onPress={handleSubmit} />
- </>
- )}
- </Formik>
- <View style={styles.alreadyRegistered}>
- <Text style={globalStyles.regularText}>Already Registered? </Text>
- <TouchableOpacity onPress={() => navigation.goBack()}>
- <Text style={globalStyles.primaryBold}>Sign In</Text>
- </TouchableOpacity>
- </View>
- </ScrollView>
- </SafeAreaView>
- );
- };
-
- const styles = StyleSheet.create({
- providersContainer: {
- flexDirection: "row",
- justifyContent: "space-between",
- marginBottom: 30,
- },
- dateOfBirthContainer: {
- flexDirection: "row",
- borderBottomColor: "#ccc",
- borderBottomWidth: 1,
- paddingBottom: 8,
- marginBottom: 30,
- },
- dateOfBirthLabel: {
- color: "#666",
- marginLeft: 5,
- marginTop: 5,
- },
- alreadyRegistered: {
- flexDirection: "row",
- justifyContent: "center",
- marginBottom: 30,
- },
- errorMessage: {
- marginBottom: 30,
- color: "red",
- },
- });
-
- export default RegisterScreen;
|