feat: fixes and formatter
This commit is contained in:
+50
-50
@@ -1,83 +1,83 @@
|
||||
import { useRouter, usePathname } from 'expo-router';
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { Platform } from 'react-native';
|
||||
import { useRouter, usePathname } from 'expo-router'
|
||||
import { useCallback, useMemo, useState } from 'react'
|
||||
import { Platform } from 'react-native'
|
||||
|
||||
import useSocialAuth from './useSocialAuth';
|
||||
import { useAuth } from '../providers/auth-context';
|
||||
import { useLoading } from '../providers/loading-context';
|
||||
import { useTooltip } from '../providers/tooltip-context';
|
||||
import useSocialAuth from './useSocialAuth'
|
||||
import { useAuth } from '../providers/auth-context'
|
||||
import { useLoading } from '../providers/loading-context'
|
||||
import { useTooltip } from '../providers/tooltip-context'
|
||||
|
||||
const normalizeEmail = rawEmail =>
|
||||
typeof rawEmail === 'string' ? rawEmail.trim().toLowerCase() : '';
|
||||
const normalizeEmail = (rawEmail) =>
|
||||
typeof rawEmail === 'string' ? rawEmail.trim().toLowerCase() : ''
|
||||
|
||||
const normalizePassword = rawPassword =>
|
||||
typeof rawPassword === 'string' ? rawPassword.trim() : '';
|
||||
const normalizePassword = (rawPassword) =>
|
||||
typeof rawPassword === 'string' ? rawPassword.trim() : ''
|
||||
|
||||
const MIN_PASSWORD_LENGTH = 6;
|
||||
const MIN_PASSWORD_LENGTH = 6
|
||||
|
||||
const useRegisterForm = () => {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const { registerWithEmail, redirectUser } = useAuth();
|
||||
const { withLoading, isLoading } = useLoading();
|
||||
const { showTooltip } = useTooltip();
|
||||
const defaultEmail = __DEV__ ? `test@${Platform.OS}.com` : '';
|
||||
const defaultPassword = __DEV__ ? 'Minuit33' : '';
|
||||
const [email, setEmail] = useState(defaultEmail);
|
||||
const [password, setPassword] = useState(defaultPassword);
|
||||
const [confirmPassword, setConfirmPassword] = useState(defaultPassword);
|
||||
const router = useRouter()
|
||||
const pathname = usePathname()
|
||||
const { registerWithEmail, redirectUser } = useAuth()
|
||||
const { withLoading, isLoading } = useLoading()
|
||||
const { showTooltip } = useTooltip()
|
||||
const defaultEmail = __DEV__ ? `test@${Platform.OS}.com` : ''
|
||||
const defaultPassword = __DEV__ ? 'Minuit33' : ''
|
||||
const [email, setEmail] = useState(defaultEmail)
|
||||
const [password, setPassword] = useState(defaultPassword)
|
||||
const [confirmPassword, setConfirmPassword] = useState(defaultPassword)
|
||||
|
||||
const { signInWithGoogle, signInWithApple, isAppleSignInAvailable } =
|
||||
useSocialAuth({ defaultAction: 'register' });
|
||||
const { signInWithGoogle, signInWithApple, isAppleSignInAvailable } = useSocialAuth({
|
||||
defaultAction: 'register',
|
||||
})
|
||||
|
||||
const handleSubmit = useCallback(async () => {
|
||||
if (isLoading) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
const trimmedEmail = normalizeEmail(email);
|
||||
const trimmedPassword = normalizePassword(password);
|
||||
const trimmedConfirm = normalizePassword(confirmPassword);
|
||||
const trimmedEmail = normalizeEmail(email)
|
||||
const trimmedPassword = normalizePassword(password)
|
||||
const trimmedConfirm = normalizePassword(confirmPassword)
|
||||
|
||||
if (!trimmedEmail || !trimmedPassword || !trimmedConfirm) {
|
||||
showTooltip({
|
||||
message: 'Merci de renseigner tous les champs requis.',
|
||||
type: 'error',
|
||||
});
|
||||
return;
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (trimmedPassword.length < MIN_PASSWORD_LENGTH) {
|
||||
showTooltip({
|
||||
message: `Le mot de passe doit contenir au moins ${MIN_PASSWORD_LENGTH} caractères.`,
|
||||
type: 'error',
|
||||
});
|
||||
return;
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (trimmedPassword !== trimmedConfirm) {
|
||||
showTooltip({
|
||||
message: 'Les mots de passe ne correspondent pas.',
|
||||
type: 'error',
|
||||
});
|
||||
return;
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await withLoading(async () => {
|
||||
await registerWithEmail(trimmedEmail, trimmedPassword);
|
||||
});
|
||||
await redirectUser({ router, pathname, isInitial: false });
|
||||
await registerWithEmail(trimmedEmail, trimmedPassword)
|
||||
})
|
||||
await redirectUser({ router, pathname, isInitial: false })
|
||||
showTooltip({
|
||||
message: 'Compte créé ! Bienvenue chez Minuit.',
|
||||
type: 'success',
|
||||
});
|
||||
})
|
||||
} catch (error) {
|
||||
showTooltip({
|
||||
message:
|
||||
error?.message || 'Impossible de créer votre compte pour le moment.',
|
||||
message: error?.message || 'Impossible de créer votre compte pour le moment.',
|
||||
type: 'error',
|
||||
});
|
||||
})
|
||||
}
|
||||
}, [
|
||||
confirmPassword,
|
||||
@@ -90,20 +90,20 @@ const useRegisterForm = () => {
|
||||
pathname,
|
||||
showTooltip,
|
||||
withLoading,
|
||||
]);
|
||||
])
|
||||
|
||||
const submitDisabled = useMemo(() => {
|
||||
const trimmedEmail = normalizeEmail(email);
|
||||
const trimmedPassword = normalizePassword(password);
|
||||
const trimmedConfirm = normalizePassword(confirmPassword);
|
||||
const trimmedEmail = normalizeEmail(email)
|
||||
const trimmedPassword = normalizePassword(password)
|
||||
const trimmedConfirm = normalizePassword(confirmPassword)
|
||||
return (
|
||||
!trimmedEmail ||
|
||||
!trimmedPassword ||
|
||||
!trimmedConfirm ||
|
||||
trimmedPassword.length < MIN_PASSWORD_LENGTH ||
|
||||
isLoading
|
||||
);
|
||||
}, [confirmPassword, email, isLoading, password]);
|
||||
)
|
||||
}, [confirmPassword, email, isLoading, password])
|
||||
|
||||
return {
|
||||
email,
|
||||
@@ -118,7 +118,7 @@ const useRegisterForm = () => {
|
||||
signInWithGoogle,
|
||||
signInWithApple,
|
||||
isAppleSignInAvailable,
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default useRegisterForm;
|
||||
export default useRegisterForm
|
||||
|
||||
Reference in New Issue
Block a user