/* eslint-disable react/display-name */
import * as AppleAuthentication from 'expo-apple-authentication'
import React, { useCallback, useState } from 'react'
import { ActivityIndicator, Pressable, StyleSheet, Text, View } from 'react-native'
import Svg, { Path } from 'react-native-svg'
import { useGlobal } from 'reactn'
import { handleFirebaseError } from '../actions/signupActions.js'
import { background } from '../assets'
import GradientButton from '../components/GradientButton.js'
import { Input } from '../components/Input.js'
import ItemContainer from '../components/ItemContainer/ItemContainer'
import firebase from '../config/firebase'
import useSocialAuth from '../hooks/useSocialAuth'
import { isWeb } from '../hooks/useLayoutType'
import Page from '../layouts/Page.js'
import { Routes } from '../navigation'
import { navigate } from '../navigation/NavigationService.js'
import { FONT_FAMILY } from '../styles/Fonts.js'
import Palette from '../styles/Palette.js'
export default ({ navigation }) => {
const [, setTooltip] = useGlobal('_tooltip')
const [email, setEmail] = useState(__DEV__ ? 'az@az.az' : '')
const [password, setPassword] = useState(__DEV__ ? 'Minuit33' : '')
const [loading, setLoading] = useState(false)
const afterLoginNavigate = useCallback(async () => {
const uid = firebase.auth().currentUser?.uid
if (!uid) throw new Error('Aucun utilisateur après connexion')
navigation.reset({ index: 0, routes: [{ name: Routes.BottomTab }] })
}, [navigation])
const {
signInWithGoogle,
signInWithApple,
isAppleSignInAvailable,
isLoading: socialLoading,
} = useSocialAuth({
onSuccess: afterLoginNavigate,
})
const isBusy = loading || socialLoading
const onLogin = async () => {
try {
setLoading(true)
await firebase.auth().signInWithEmailAndPassword(email.trim(), password)
await afterLoginNavigate()
} catch (e) {
console.log('Login error', e?.message)
const message =
e?.code && e.code.startsWith('auth/')
? handleFirebaseError(e.code)
: e?.message || 'Connexion impossible'
setTooltip({ text: message, type: 'error' })
} finally {
setLoading(false)
}
}
return (
{'Bonjour !'}
Nous sommes heureux de te revoir parmi nous. Prêt·e à reprendre le rythme ?
navigate(Routes.ForgotPassword)}
style={({ pressed }) => [styles.linkPressable, pressed && styles.linkPressed]}
>
Mot de passe oublié ?
{isAppleSignInAvailable ? (
) : null}
Tu n'as pas de compte ?{' '}
navigate(Routes.Register)}>
Créer un compte
)
}
const AppleAuthButton = ({ onPress, disabled, loading }) => {
if (isWeb) {
return null
}
return (
{loading ? (
) : null}
)
}
const GoogleAuthButton = ({ onPress, disabled, loading }) => (
[
styles.socialButton,
pressed && !disabled ? styles.socialButtonPressed : null,
disabled ? styles.socialButtonDisabled : null,
]}
>
Continuer avec Google
{loading ? (
) : (
)}
)
const styles = StyleSheet.create({
screen: {
flex: 1,
width: '100%',
justifyContent: 'center',
alignItems: 'center',
},
loginCard: {
width: '100%',
maxWidth: 520,
},
cardContent: {
gap: 28,
paddingTop: 8,
paddingHorizontal: 5,
width: '100%',
},
greetingSection: {
gap: 6,
width: '100%',
},
greetingTitle: {
fontSize: 22,
color: Palette.white,
fontFamily: FONT_FAMILY.InterSemiBold,
},
greetingSubtitle: {
fontSize: 14,
color: Palette.gray,
fontFamily: FONT_FAMILY.InterRegular,
lineHeight: 20,
},
formFields: {
gap: 20,
width: '100%',
},
passwordField: {
gap: 8,
width: '100%',
},
linkPressable: {
alignSelf: 'flex-end',
},
linkPressed: {
opacity: 0.6,
},
linkText: {
fontSize: 12,
color: Palette.white,
fontFamily: FONT_FAMILY.InterRegular,
textAlign: 'right',
},
bottomFooter: {
marginTop: 16,
paddingHorizontal: 24,
},
bottomFooterText: {
fontSize: 14,
color: Palette.white,
fontFamily: FONT_FAMILY.HelveticaNeueRegular,
textAlign: 'center',
},
bottomFooterLink: {
fontFamily: FONT_FAMILY.InterSemiBold,
color: Palette.white,
},
socialWrapper: {
width: '100%',
gap: 16,
alignItems: 'center',
},
socialButton: {
alignSelf: 'center',
borderRadius: 12,
width: '90%',
backgroundColor: Palette.white,
borderWidth: 1,
borderColor: '#E3E7EF',
paddingVertical: 12,
paddingHorizontal: 20,
shadowColor: 'rgba(12, 6, 34, 0.08)',
shadowOffset: { width: 0, height: 8 },
shadowOpacity: 0.3,
shadowRadius: 16,
elevation: 2,
},
socialButtonPressed: {
backgroundColor: '#F4F7FF',
},
socialButtonDisabled: {
opacity: 0.7,
},
socialButtonContent: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
gap: 12,
},
socialIconContainer: {
justifyContent: 'center',
alignItems: 'center',
width: 28,
height: 28,
},
socialButtonText: {
flex: 1,
textAlign: 'center',
fontSize: 15,
color: '#202124',
fontFamily: FONT_FAMILY.InterSemiBold,
},
socialRightSpacer: {
width: 24,
height: 24,
},
nativeButtonWrapper: {
alignSelf: 'center',
width: '90%',
height: 50,
justifyContent: 'center',
},
nativeButton: {
width: '100%',
height: 50,
},
nativeLoadingOverlay: {
...StyleSheet.absoluteFillObject,
alignItems: 'center',
justifyContent: 'center',
},
})
const GoogleLogo = () => (
)