keepyourmouthshut/Customer App/src/screens/CreateAccount/CreateAccount.js

269 lines
7.9 KiB
JavaScript
Raw Normal View History

2023-03-13 11:03:39 +00:00
import { useMutation } from "@apollo/react-hooks";
import { FontAwesome } from "@expo/vector-icons";
import { useNavigation, useTheme } from "@react-navigation/native";
2023-03-16 14:33:00 +00:00
2023-03-13 11:03:39 +00:00
import * as AppleAuthentication from "expo-apple-authentication";
import Constants from "expo-constants";
2023-03-16 14:33:00 +00:00
2023-03-13 11:03:39 +00:00
//import * as Google from 'expo-google-app-auth'
import * as Google from "expo-auth-session/providers/google";
import * as Notifications from "expo-notifications";
import gql from "graphql-tag";
import React, { useContext, useEffect, useState } from "react";
import { Platform, TouchableOpacity, View } from "react-native";
import getEnvVars from "../../../environment";
import { login } from "../../apollo/server";
2022-12-21 09:56:47 +00:00
import {
EnategaImage,
FdEmailBtn,
FdGoogleBtn,
FlashMessage,
RegistrationHeader,
Spinner,
TextDefault,
2023-03-13 11:03:39 +00:00
WrapperView,
} from "../../components";
import UserContext from "../../context/User";
import { alignment } from "../../utils/alignment";
import Analytics from "../../utils/analytics";
import { NAVIGATION_SCREEN } from "../../utils/constant";
import { scale } from "../../utils/scaling";
import useStyle from "./styles";
2023-03-16 14:33:00 +00:00
import ApolloClient from "apollo-client";
2022-12-21 09:56:47 +00:00
2023-03-13 11:03:39 +00:00
const {
IOS_CLIENT_ID_GOOGLE,
ANDROID_CLIENT_ID_GOOGLE,
Expo_CLIENT_ID_GOOGLE,
} = getEnvVars();
2022-12-21 09:56:47 +00:00
const LOGIN = gql`
${login}
2023-03-13 11:03:39 +00:00
`;
2022-12-21 09:56:47 +00:00
2023-03-13 11:03:39 +00:00
const Logo = require("../../../assets/logo.png");
2022-12-21 09:56:47 +00:00
const CreateAccount = () => {
2023-03-13 11:03:39 +00:00
const styles = useStyle();
const { colors } = useTheme();
const navigation = useNavigation();
const [enableApple, setEnableApple] = useState(false);
const [loginButton, loginButtonSetter] = useState(null);
const [loading, setLoading] = useState(false);
2022-12-21 09:56:47 +00:00
2023-03-13 11:03:39 +00:00
const { setTokenAsync } = useContext(UserContext);
2022-12-21 09:56:47 +00:00
useEffect(() => {
2023-03-13 11:03:39 +00:00
checkIfSupportsAppleAuthentication();
}, []);
2022-12-21 09:56:47 +00:00
2023-03-13 11:03:39 +00:00
const [mutate] = useMutation(LOGIN, { onCompleted, onError });
2022-12-21 09:56:47 +00:00
async function checkIfSupportsAppleAuthentication() {
2023-03-13 11:03:39 +00:00
setEnableApple(await AppleAuthentication.isAvailableAsync());
2022-12-21 09:56:47 +00:00
}
async function onCompleted(data) {
try {
const trackingOpts = {
id: data.login.userId,
2023-03-13 11:03:39 +00:00
usernameOrEmail: data.login.email,
};
Analytics.identify(data.login.userId, trackingOpts);
Analytics.track(Analytics.events.USER_CREATED_ACCOUNT, trackingOpts);
setTokenAsync(data.login.token);
navigation.navigate(NAVIGATION_SCREEN.Menu);
2022-12-21 09:56:47 +00:00
} catch (e) {
2023-03-13 11:03:39 +00:00
console.log(e);
2022-12-21 09:56:47 +00:00
} finally {
2023-03-13 11:03:39 +00:00
setLoading(false);
2022-12-21 09:56:47 +00:00
}
}
function onError(error) {
try {
2023-03-13 11:03:39 +00:00
console.log(JSON.stringify(error));
2022-12-21 09:56:47 +00:00
FlashMessage({
2023-03-13 11:03:39 +00:00
message: error.graphQLErrors[0].message,
});
loginButtonSetter(null);
2022-12-21 09:56:47 +00:00
} catch (e) {
2023-03-13 11:03:39 +00:00
console.log(e);
2022-12-21 09:56:47 +00:00
} finally {
2023-03-13 11:03:39 +00:00
setLoading(false);
2022-12-21 09:56:47 +00:00
}
}
async function mutateLogin(user) {
2023-03-13 11:03:39 +00:00
setLoading(true);
let notificationToken = null;
2022-12-21 09:56:47 +00:00
if (Constants.isDevice) {
const { status: existingStatus } =
2023-03-13 11:03:39 +00:00
await Notifications.getPermissionsAsync();
if (existingStatus === "granted") {
notificationToken = (await Notifications.getExpoPushTokenAsync()).data;
2022-12-21 09:56:47 +00:00
}
}
2023-03-13 11:03:39 +00:00
mutate({ variables: { ...user, notificationToken } });
2022-12-21 09:56:47 +00:00
}
function renderAppleAction() {
2023-03-13 11:03:39 +00:00
if (loading && loginButton === "Apple") {
2022-12-21 09:56:47 +00:00
return (
<View style={styles.buttonBackground}>
<Spinner backColor="rgba(0,0,0,0.1)" spinnerColor={colors.tagColor} />
</View>
2023-03-13 11:03:39 +00:00
);
2022-12-21 09:56:47 +00:00
}
return (
<TouchableOpacity
style={styles.appleBtn}
2023-03-13 11:03:39 +00:00
onPress={async () => {
2022-12-21 09:56:47 +00:00
try {
const credential = await AppleAuthentication.signInAsync({
requestedScopes: [
AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
2023-03-13 11:03:39 +00:00
AppleAuthentication.AppleAuthenticationScope.EMAIL,
],
});
2023-03-16 14:33:00 +00:00
console.log(credential);
2022-12-21 09:56:47 +00:00
if (credential) {
const user = {
appleId: credential.user,
2023-03-13 11:03:39 +00:00
phone: "",
2022-12-21 09:56:47 +00:00
email: credential.email,
2023-03-13 11:03:39 +00:00
password: "",
2022-12-21 09:56:47 +00:00
name:
credential.fullName.givenName +
2023-03-13 11:03:39 +00:00
" " +
2022-12-21 09:56:47 +00:00
credential.fullName.familyName,
2023-03-13 11:03:39 +00:00
picture: "",
type: "apple",
};
mutateLogin(user);
2022-12-21 09:56:47 +00:00
}
2023-03-13 11:03:39 +00:00
loginButtonSetter("Apple");
2022-12-21 09:56:47 +00:00
// signed in
} catch (e) {
2023-03-13 11:03:39 +00:00
if (e.code === "ERR_CANCELLED") {
2022-12-21 09:56:47 +00:00
// handle that the user canceled the sign-in flow
2023-03-13 11:03:39 +00:00
loginButtonSetter(null);
2022-12-21 09:56:47 +00:00
} else {
// handle other errors
2023-03-13 11:03:39 +00:00
loginButtonSetter(null);
2022-12-21 09:56:47 +00:00
}
}
2023-03-13 11:03:39 +00:00
}}
>
2022-12-21 09:56:47 +00:00
<FontAwesome
style={styles.marginLeft5}
name="apple"
size={scale(19)}
color="#000"
/>
<TextDefault style={alignment.MLsmall} bold>
Signup with Apple
</TextDefault>
</TouchableOpacity>
2023-03-13 11:03:39 +00:00
);
2022-12-21 09:56:47 +00:00
}
2023-03-13 11:03:39 +00:00
const [googleRequest, googleResponse, googlePromptAsync] =
Google.useAuthRequest({
expoClientId: Expo_CLIENT_ID_GOOGLE,
iosClientId: IOS_CLIENT_ID_GOOGLE,
iosStandaloneAppClientId: IOS_CLIENT_ID_GOOGLE,
androidClientId: ANDROID_CLIENT_ID_GOOGLE,
androidStandaloneAppClientId: ANDROID_CLIENT_ID_GOOGLE,
//redirectUrl: `${AuthSession.OAuthRedirect}:/oauth2redirect/google`,
scopes: ["profile", "email"],
...{ useProxy: true },
});
const googleSignUp = () => {
if (googleResponse?.type === "success") {
const { authentication } = googleResponse;
console.log(authentication.accessToken);
(async () => {
const userInfoResponse = await fetch(
"https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
{
headers: { Authorization: `Bearer ${authentication.accessToken}` },
}
);
const googleUser = await userInfoResponse.json();
const user = {
phone: "",
email: googleUser.email,
password: "",
name: googleUser.name,
picture: googleUser.picture,
type: "google",
};
mutateLogin(user);
})();
2022-12-21 09:56:47 +00:00
}
2023-03-13 11:03:39 +00:00
};
useEffect(() => {
googleSignUp();
}, [googleResponse]);
2022-12-21 09:56:47 +00:00
function renderGoogleAction() {
return (
<FdGoogleBtn
2023-03-13 11:03:39 +00:00
loadingIcon={loading && loginButton === "Google"}
2022-12-21 09:56:47 +00:00
onPressIn={() => {
2023-03-13 11:03:39 +00:00
loginButtonSetter("Google");
2022-12-21 09:56:47 +00:00
}}
2023-03-13 11:03:39 +00:00
disabled={!googleRequest}
onPress={() => googlePromptAsync()}
2022-12-21 09:56:47 +00:00
/>
2023-03-13 11:03:39 +00:00
);
2022-12-21 09:56:47 +00:00
}
function renderEmailAction() {
return (
<FdEmailBtn
2023-03-13 11:03:39 +00:00
loadingIcon={loading && loginButton === "Email"}
2022-12-21 09:56:47 +00:00
onPress={() => {
2023-03-13 11:03:39 +00:00
loginButtonSetter("Email");
navigation.navigate(NAVIGATION_SCREEN.Register);
2022-12-21 09:56:47 +00:00
}}
/>
2023-03-13 11:03:39 +00:00
);
2022-12-21 09:56:47 +00:00
}
return (
<WrapperView>
<View style={[styles.mainContainer, styles.flex]}>
2023-03-13 11:03:39 +00:00
<RegistrationHeader title={"Get Started"} />
2022-12-21 09:56:47 +00:00
<View style={styles.subContainer}>
<View style={[styles.flex, styles.upperContainer]}>
<EnategaImage
imgStyle={styles.imgResponsive}
imgSource={Logo}
spinnerProps={{ style: styles.loadingView }}
/>
</View>
<View style={styles.width100}>
<View style={alignment.MTmedium}>{renderGoogleAction()}</View>
{enableApple && (
<View style={alignment.MTmedium}>{renderAppleAction()}</View>
)}
<View style={alignment.MTmedium}>{renderEmailAction()}</View>
<TouchableOpacity
activeOpacity={0.7}
style={styles.alreadyBtn}
2023-03-13 11:03:39 +00:00
onPress={() => navigation.navigate(NAVIGATION_SCREEN.Login)}
>
2022-12-21 09:56:47 +00:00
<TextDefault style={[alignment.MLsmall]} bold>
Already a member? Log in
</TextDefault>
</TouchableOpacity>
</View>
</View>
</View>
</WrapperView>
2023-03-13 11:03:39 +00:00
);
};
export default CreateAccount;