loginPage.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { View, Text, Image, StyleSheet, Pressable, Alert, Dimensions, ActivityIndicator } from 'react-native';
  2. import Logo from '../../../global/logo';
  3. import NormalButton from '../../../global/normal_button';
  4. import NormalInput from '../../../global/normal_input';
  5. import { useState } from 'react';
  6. import { useAuth } from '../../../../context/AuthProvider';
  7. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  8. type LoginPageProps = {
  9. goToNextPage: () => void;
  10. goToForgetPassWordPage: () => void;
  11. };
  12. const screenHeight = Dimensions.get('window').height;
  13. console.log(screenHeight);
  14. const LoginPage: React.FC<LoginPageProps> = ({ goToNextPage, goToForgetPassWordPage }) => {
  15. const [loginEmail, setLoginEmail] = useState('');
  16. const [loginPassword, setLoginPassword] = useState('');
  17. const [isLoading, setIsLoading] = useState(false);
  18. const { login } = useAuth();
  19. const _login = async (username: string, password: string) => {
  20. setIsLoading(true);
  21. if (username === '' || password === '') {
  22. Alert.alert('請輸入資料', '請輸入電子郵件和密碼');
  23. } else {
  24. const lowerCaseUsername = username.toLowerCase();
  25. const success = await login(lowerCaseUsername, password);
  26. if (!success) {
  27. Alert.alert('登入失敗', '請檢查您的電子郵件和密碼');
  28. }
  29. }
  30. setIsLoading(false);
  31. };
  32. const insets = useSafeAreaInsets();
  33. return (
  34. <View className={`flex-1 justify-center ${screenHeight < 750 ? 'h-screen' : 'h-[80vh] space-y-8'}`}>
  35. <View className="flex-3 items-center justify-end" style={{}}>
  36. <Image
  37. source={require('../../../../assets/ccLogo.png')}
  38. resizeMode="contain"
  39. style={{
  40. width: screenHeight > 750 ? 250 : 200,
  41. height: screenHeight > 750 ? 250 : 200
  42. }}
  43. />
  44. </View>
  45. <View
  46. style={{
  47. gap: 10
  48. // paddingBottom: Math.max(insets.bottom, 20)
  49. // marginTop: screenHeight > 750 ? 40 : 0
  50. }}
  51. className="mx-[5%] flex-2 "
  52. >
  53. <NormalInput
  54. placeholder="電子郵件"
  55. onChangeText={(email) => setLoginEmail(email)}
  56. extendedStyle={{ borderRadius: 12, padding: 20 }}
  57. textContentType="username"
  58. autoComplete="username"
  59. keyboardType="email-address"
  60. autoCapitalize="none"
  61. />
  62. <NormalInput
  63. placeholder="密碼"
  64. onChangeText={(password) => setLoginPassword(password)}
  65. secureTextEntry={true}
  66. extendedStyle={{ borderRadius: 12, padding: 20 }}
  67. textContentType="password"
  68. autoComplete="password"
  69. />
  70. <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
  71. <Text style={styles.text}>忘記密碼</Text>
  72. </Pressable>
  73. <NormalButton
  74. extendedStyle={{ padding: 20 }}
  75. onPress={() => _login(loginEmail, loginPassword)}
  76. title={
  77. isLoading ? (
  78. <Text
  79. style={{
  80. fontWeight: '700',
  81. fontSize: 20,
  82. color: '#fff'
  83. }}
  84. >
  85. <ActivityIndicator />
  86. </Text>
  87. ) : (
  88. <Text
  89. style={{
  90. fontWeight: '700',
  91. fontSize: 20,
  92. color: '#fff'
  93. }}
  94. >
  95. 登入
  96. </Text>
  97. )
  98. }
  99. />
  100. <Pressable className="self-start" onPress={goToNextPage}>
  101. <Text style={styles.text}>註冊會員</Text>
  102. </Pressable>
  103. </View>
  104. </View>
  105. );
  106. };
  107. const styles = StyleSheet.create({
  108. container: {
  109. flex: 1,
  110. height: Dimensions.get('window').height,
  111. justifyContent: 'center',
  112. marginHorizontal: 20
  113. },
  114. topContainerForLogo: {},
  115. text: {
  116. color: '#02677D',
  117. fontSize: 16,
  118. paddingVertical: 5
  119. }
  120. });
  121. export default LoginPage;