loginPage.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. View,
  3. Text,
  4. StyleSheet,
  5. Pressable,
  6. ScrollView,
  7. Alert
  8. } from 'react-native';
  9. import NormalButton from '../../../global/normal_button';
  10. import Logo from '../../../global/logo';
  11. import PhoneInput from '../../../global/phone_input';
  12. import NormalInput from '../../../global/normal_input';
  13. import { useState } from 'react';
  14. import { SafeAreaView } from 'react-native-safe-area-context';
  15. import { useAuth } from '../../../../context/AuthProvider';
  16. type LoginPageProps = {
  17. goToNextPage: () => void;
  18. goToForgetPassWordPage: () => void;
  19. };
  20. const LoginPage: React.FC<LoginPageProps> = ({
  21. goToNextPage,
  22. goToForgetPassWordPage
  23. }) => {
  24. // const [loginPhone, setLoginPhone] = useState(''); //This loginPhone useState is only a placeholder, will be updated in the future
  25. const [loginEmail, setLoginEmail] = useState('');
  26. const [loginPassword, setLoginPassword] = useState('');
  27. const [isLoading, setIsLoading] = useState(false);
  28. const { login } = useAuth();
  29. const _login = async (username: string, password: string) => {
  30. setIsLoading(true);
  31. if (username === '' || password === '') {
  32. Alert.alert('Error', 'Please enter a username and password');
  33. } else {
  34. const success = await login(username, password);
  35. if (!success) {
  36. Alert.alert('Error', 'Invalid username or password');
  37. }
  38. }
  39. setIsLoading(false);
  40. };
  41. return (
  42. <ScrollView>
  43. <SafeAreaView style={styles.container}>
  44. <View style={styles.topContainerForLogo}>
  45. <Logo />
  46. </View>
  47. <View style={styles.bottomContainer}>
  48. <NormalInput
  49. placeholder="電子郵件"
  50. onChangeText={(email) => setLoginEmail(email)}
  51. extendedStyle={{ borderRadius: 12, padding: 20 }}
  52. />
  53. <NormalInput
  54. placeholder="密碼"
  55. onChangeText={(password) => setLoginPassword(password)}
  56. secureTextEntry={true}
  57. extendedStyle={{ borderRadius: 12, padding: 20 }}
  58. />
  59. <Pressable onPress={() => goToForgetPassWordPage()}>
  60. <Text style={styles.text}>忘記密碼</Text>
  61. </Pressable>
  62. <NormalButton
  63. onPress={() => _login(loginEmail, loginPassword)}
  64. title={
  65. isLoading ? (
  66. <Text
  67. style={{
  68. fontWeight: '700',
  69. fontSize: 20,
  70. color: '#fff'
  71. }}
  72. >
  73. Loading ...{' '}
  74. </Text>
  75. ) : (
  76. <Text
  77. style={{
  78. fontWeight: '700',
  79. fontSize: 20,
  80. color: '#fff'
  81. }}
  82. >
  83. 登入
  84. </Text>
  85. )
  86. }
  87. />
  88. <Pressable onPress={goToNextPage}>
  89. <Text style={styles.text}>註冊會員</Text>
  90. </Pressable>
  91. </View>
  92. </SafeAreaView>
  93. </ScrollView>
  94. );
  95. };
  96. const styles = StyleSheet.create({
  97. container: {
  98. flex: 1,
  99. gap: 50,
  100. marginHorizontal: 20
  101. },
  102. topContainerForLogo: {
  103. flex: 1 / 2
  104. },
  105. bottomContainer: {
  106. flex: 1,
  107. gap: 10
  108. },
  109. text: {
  110. color: '#02677D',
  111. fontSize: 16,
  112. paddingVertical: 5
  113. }
  114. });
  115. export default LoginPage;