You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegisterScreen.jsx 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import React, { useEffect, useState } from "react";
  2. import {
  3. ScrollView,
  4. View,
  5. Text,
  6. TouchableOpacity,
  7. StyleSheet,
  8. Alert,
  9. } from "react-native";
  10. import InputField from "@components/InputField";
  11. import MaterialIcons from "@expo/vector-icons/MaterialIcons";
  12. import Ionicons from "@expo/vector-icons/Ionicons";
  13. import RegistrationSVG from "@assets/images/registration.svg";
  14. import GoogleSVG from "@assets/images/google.svg";
  15. import FacebookSVG from "@assets/images/facebook.svg";
  16. import TwitterSVG from "@assets/images/twitter.svg";
  17. import CustomButton from "@components/Buttons/CustomButton";
  18. import { globalStyles } from "@styles/global";
  19. import Loader from "@components/Loader";
  20. import { Formik } from "formik";
  21. import { registerSchema } from "@schemas/registerSchema";
  22. import { useDispatch } from "react-redux";
  23. import useAuthHook from "../hooks/useAuthHook";
  24. import Layout from "@components/Layout/Layout";
  25. import { useTheme } from "@styles";
  26. import { useTranslation } from "react-i18next";
  27. import {
  28. useAuthProviderMutation,
  29. useRegisterMutation,
  30. } from "@features/auth/authApiSlice";
  31. import { setCredentials } from "@features/auth/authSlice";
  32. import { storeData } from "@service/asyncStorage";
  33. import { ACCESS_TOKEN } from "@constants/localStorage";
  34. const RegisterScreen = ({ navigation }) => {
  35. const [authProvider, { isLoading: isLoadingProvider }] =
  36. useAuthProviderMutation();
  37. const [currentProvider, setCurrentProvider] = useState("");
  38. const { colors } = useTheme();
  39. const { t } = useTranslation();
  40. const [register, { isLoading, error }] = useRegisterMutation();
  41. const { response, promptAsync } = useAuthHook();
  42. const dispatch = useDispatch();
  43. const handleApiResponseSuccess = () => {
  44. Alert.alert(t("common.success"), t("register.successRegisterAccount"), [
  45. {
  46. text: "OK",
  47. onPress: () => navigation.navigate("Login"),
  48. },
  49. ]);
  50. };
  51. const authProviderHandler = async (response) => {
  52. if (response?.type === "success") {
  53. const accessToken = response.authentication.accessToken;
  54. if (accessToken) {
  55. await storeData(ACCESS_TOKEN, accessToken);
  56. try {
  57. const userResponse = await authProvider({
  58. provider: currentProvider,
  59. accessToken,
  60. }).unwrap();
  61. if (userResponse) {
  62. dispatch(setCredentials(userResponse));
  63. }
  64. } catch (e) {
  65. console.log(e);
  66. }
  67. }
  68. }
  69. };
  70. useEffect(() => {
  71. authProviderHandler(response);
  72. }, [response, currentProvider]);
  73. const handleSignup = async (values) => {
  74. const { username, email, password } = values;
  75. try {
  76. const userData = await register({ username, email, password }).unwrap();
  77. if (userData) {
  78. handleApiResponseSuccess();
  79. }
  80. } catch (e) {
  81. console.log(e?.data);
  82. }
  83. };
  84. const handleGoogleAuth = () => {
  85. promptAsync();
  86. };
  87. return (
  88. <Layout>
  89. <Loader visible={isLoading} />
  90. <ScrollView
  91. showsVerticalScrollIndicator={false}
  92. style={{ paddingHorizontal: 25 }}
  93. >
  94. <View style={{ alignItems: "center" }}>
  95. <RegistrationSVG width={300} height={300} />
  96. </View>
  97. <Text style={[globalStyles.boldText, { color: colors.textPrimary }]}>
  98. {t("register.register")}
  99. </Text>
  100. <View style={styles.providersContainer}>
  101. <TouchableOpacity
  102. onPress={() => {
  103. setCurrentProvider("google");
  104. handleGoogleAuth();
  105. }}
  106. style={globalStyles.iconButton}
  107. >
  108. <GoogleSVG height={24} width={24} />
  109. </TouchableOpacity>
  110. <TouchableOpacity onPress={() => {}} style={globalStyles.iconButton}>
  111. <FacebookSVG height={24} width={24} />
  112. </TouchableOpacity>
  113. <TouchableOpacity onPress={() => {}} style={globalStyles.iconButton}>
  114. <TwitterSVG height={24} width={24} />
  115. </TouchableOpacity>
  116. </View>
  117. <Text
  118. style={[
  119. globalStyles.regularCenteredText,
  120. { color: colors.textPrimary },
  121. ]}
  122. >
  123. {t("register.orSignUpWithMail")}
  124. </Text>
  125. <Formik
  126. initialValues={{
  127. username: "",
  128. email: "",
  129. password: "",
  130. confirmPassword: "",
  131. }}
  132. validationSchema={registerSchema}
  133. onSubmit={handleSignup}
  134. validateOnChange={false}
  135. validateOnBlur={false}
  136. >
  137. {({
  138. handleChange,
  139. handleBlur,
  140. handleSubmit,
  141. values,
  142. isValid,
  143. errors,
  144. }) => (
  145. <>
  146. <InputField
  147. name={"username"}
  148. text={values.username}
  149. onChangeText={handleChange("username")}
  150. label={t("register.username")}
  151. handleBlur={handleBlur("username")}
  152. icon={
  153. <Ionicons
  154. name="person-outline"
  155. size={20}
  156. color="#666"
  157. style={{ marginRight: 5 }}
  158. />
  159. }
  160. />
  161. {errors.username && (
  162. <Text style={styles.errorMessage}>{errors.username}</Text>
  163. )}
  164. <InputField
  165. name={"email"}
  166. text={values.email}
  167. onChangeText={handleChange("email")}
  168. label={t("register.email")}
  169. handleBlur={handleBlur("email")}
  170. icon={
  171. <MaterialIcons
  172. name="alternate-email"
  173. size={20}
  174. color="#666"
  175. style={{ marginRight: 5 }}
  176. />
  177. }
  178. keyboardType="email-address"
  179. />
  180. {errors.email && (
  181. <Text style={styles.errorMessage}>{errors.email}</Text>
  182. )}
  183. <InputField
  184. name={"password"}
  185. text={values.password}
  186. onChangeText={handleChange("password")}
  187. label={t("register.password")}
  188. handleBlur={handleBlur("password")}
  189. icon={
  190. <Ionicons
  191. name="ios-lock-closed-outline"
  192. size={20}
  193. color="#666"
  194. style={{ marginRight: 5 }}
  195. />
  196. }
  197. inputType="password"
  198. />
  199. {errors.password && (
  200. <Text style={styles.errorMessage}>{errors.password}</Text>
  201. )}
  202. <InputField
  203. name={"confirmPassword"}
  204. text={values.confirmPassword}
  205. onChangeText={handleChange("confirmPassword")}
  206. label={t("register.confirmPassword")}
  207. handleBlur={handleBlur("confirmPassword")}
  208. icon={
  209. <Ionicons
  210. name="ios-lock-closed-outline"
  211. size={20}
  212. color="#666"
  213. style={{ marginRight: 5 }}
  214. />
  215. }
  216. inputType="password"
  217. />
  218. {errors.confirmPassword && (
  219. <Text style={styles.errorMessage}>
  220. {errors.confirmPassword}
  221. </Text>
  222. )}
  223. {error && (
  224. <Text style={styles.errorMessage}>
  225. {error?.data?.error?.message}
  226. </Text>
  227. )}
  228. <CustomButton
  229. label={t("register.signUp")}
  230. onPress={handleSubmit}
  231. />
  232. </>
  233. )}
  234. </Formik>
  235. <View style={styles.alreadyRegistered}>
  236. <Text
  237. style={[globalStyles.regularText, { color: colors.textPrimary }]}
  238. >
  239. {t("register.alreadyRegistered")}{" "}
  240. </Text>
  241. <TouchableOpacity onPress={() => navigation.goBack()}>
  242. <Text style={globalStyles.primaryBold}>{t("register.signIn")}</Text>
  243. </TouchableOpacity>
  244. </View>
  245. </ScrollView>
  246. </Layout>
  247. );
  248. };
  249. const styles = StyleSheet.create({
  250. providersContainer: {
  251. flexDirection: "row",
  252. justifyContent: "space-between",
  253. marginBottom: 30,
  254. },
  255. dateOfBirthContainer: {
  256. flexDirection: "row",
  257. borderBottomColor: "#ccc",
  258. borderBottomWidth: 1,
  259. paddingBottom: 8,
  260. marginBottom: 30,
  261. },
  262. dateOfBirthLabel: {
  263. color: "#666",
  264. marginLeft: 5,
  265. marginTop: 5,
  266. },
  267. alreadyRegistered: {
  268. flexDirection: "row",
  269. justifyContent: "center",
  270. marginBottom: 30,
  271. },
  272. errorMessage: {
  273. marginBottom: 30,
  274. color: "red",
  275. },
  276. });
  277. export default RegisterScreen;