Viktoriia 8 mesi fa
parent
commit
e4716312d4

+ 26 - 9
src/components/Button/index.tsx

@@ -1,5 +1,5 @@
 import React, { CSSProperties, FC, ReactNode } from 'react';
-import { Text, TouchableOpacity } from 'react-native';
+import { Text, TouchableOpacity, ActivityIndicator } from 'react-native';
 
 import { styles } from './styles';
 import { ButtonVariants } from '../../types/components';
@@ -13,7 +13,14 @@ type Props = {
   disabled?: boolean;
 };
 
-export const Button: FC<Props> = ({ children, variant, onPress, containerStyles, textStyles, disabled }) => {
+export const Button: FC<Props> = ({
+  children,
+  variant,
+  onPress,
+  containerStyles,
+  textStyles,
+  disabled
+}) => {
   return (
     <>
       {variant === ButtonVariants.OPACITY ? (
@@ -30,11 +37,12 @@ export const Button: FC<Props> = ({ children, variant, onPress, containerStyles,
           children={children}
           containerStyles={containerStyles}
           textStyles={textStyles}
+          disabled={disabled}
         />
       ) : variant == ButtonVariants.TEXT ? (
-        <TextButton children={children} onPress={onPress} />
+        <TextButton children={children} onPress={onPress} disabled={disabled} />
       ) : (
-        <FillButton onPress={onPress} children={children} />
+        <FillButton onPress={onPress} children={children} disabled={disabled} />
       )}
     </>
   );
@@ -68,15 +76,24 @@ const FillButton: FC<VariantProps> = ({
   onPress,
   children,
   containerStyles = {},
-  textStyles = {}
+  textStyles = {},
+  disabled = false
 }) => (
-  <TouchableOpacity style={[styles.button, styles.fillButton, containerStyles]} onPress={onPress}>
-    <Text style={[styles.text, styles.fillText, textStyles]}>{children}</Text>
+  <TouchableOpacity
+    style={[styles.button, styles.fillButton, containerStyles]}
+    onPress={onPress}
+    disabled={disabled}
+  >
+    {disabled ? (
+      <ActivityIndicator animating={disabled} color={'#fff'} />
+    ) : (
+      <Text style={[styles.text, styles.fillText, textStyles]}>{children}</Text>
+    )}
   </TouchableOpacity>
 );
 
-const TextButton: FC<VariantProps> = ({ onPress, children }) => (
-  <TouchableOpacity style={styles.button} onPress={onPress}>
+const TextButton: FC<VariantProps> = ({ onPress, children, disabled = false }) => (
+  <TouchableOpacity style={styles.button} onPress={onPress} disabled={disabled}>
     <Text style={[styles.text, styles.textButtonText]}>{children}</Text>
   </TouchableOpacity>
 );

+ 3 - 3
src/screens/InAppScreens/MapScreen/RegionViewScreen/index.tsx

@@ -44,7 +44,7 @@ const RegionViewScreen: FC<Props> = ({ navigation, route }) => {
   const regionId = route.params?.regionId;
   const type = route.params?.type;
   const disabled = route.params?.disabled;
-  const token = storage.get('token', StoreType.STRING) as string;
+  const token = (storage.get('token', StoreType.STRING) as string) ?? null;
   const [isLoading, setIsLoading] = useState(true);
   const [isModalVisible, setModalVisible] = useState(false);
   const [currentImageIndex, setCurrentImageIndex] = useState(0);
@@ -457,7 +457,7 @@ const RegionViewScreen: FC<Props> = ({ navigation, route }) => {
           <View style={styles.divider} />
 
           <View style={styles.stats}>
-            {data?.data.users_from_region_count ?? 0 > 0 ? (
+            {(data?.data.users_from_region_count ?? 0 > 0) ? (
               <TouchableOpacity
                 style={[styles.statItem, { justifyContent: 'flex-start' }]}
                 onPress={() =>
@@ -505,7 +505,7 @@ const RegionViewScreen: FC<Props> = ({ navigation, route }) => {
               <View style={[styles.statItem, { justifyContent: 'flex-start' }]} />
             )}
 
-            {data?.data.users_who_visited_region_count ?? 0 > 0 ? (
+            {(data?.data.users_who_visited_region_count ?? 0 > 0) ? (
               <TouchableOpacity
                 style={[styles.statItem, { justifyContent: 'flex-end' }]}
                 onPress={() =>

+ 13 - 2
src/screens/InAppScreens/MapScreen/index.tsx

@@ -215,10 +215,10 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
   }, [dareData]);
 
   useEffect(() => {
-    if (route.params?.id && route.params?.type && dareData) {
+    if (route.params?.id && route.params?.type && dareData && db1 && db2 && db3) {
       handleFindRegion(route.params?.id, route.params?.type);
     }
-  }, [route, dareData]);
+  }, [route, dareData, db1, db2, db3]);
 
   useEffect(() => {
     if (userInfo) {
@@ -493,6 +493,8 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
               refreshDatabases();
             });
 
+          setUserData({ type: 'countries', id: +countryId });
+
           token &&
             (await mutateCountriesData(
               { id: +countryId, token },
@@ -553,6 +555,7 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
       zoomLevel < 7 && mapRef.current?.animateToRegion(region, 1000);
 
       if (tableName === 'regions') {
+        setUserData({ type: 'nm', id: +id });
         token &&
           (await mutateUserData(
             { region_id: +id, token: String(token) },
@@ -575,6 +578,7 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
             }
           ));
       } else {
+        setUserData({ type: 'dare', id: +id });
         token &&
           (await mutateUserDataDare(
             { dare_id: +id, token: String(token) },
@@ -593,6 +597,9 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
 
   const handleFindRegion = async (id: number, type: string) => {
     cancelTokenRef.current = true;
+    if (!db1 || !db2 || !db3) {
+      return;
+    }
     const db = type === 'regions' ? db1 : type === 'countries' ? db3 : db2;
 
     if (type === 'countries') {
@@ -609,6 +616,8 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
           refreshDatabases();
         });
 
+      setUserData({ type: 'countries', id: +id });
+
       token &&
         (await mutateCountriesData(
           { id: +id, token },
@@ -663,6 +672,7 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
       mapRef.current?.animateToRegion(region, 1000);
 
       if (type === 'regions') {
+        setUserData({ type: 'nm', id: +id });
         token &&
           (await mutateUserData(
             { region_id: +id, token: String(token) },
@@ -686,6 +696,7 @@ const MapScreen: React.FC<MapScreenProps> = ({ navigation, route }) => {
             }
           ));
       } else {
+        setUserData({ type: 'dare', id: +id });
         token &&
           (await mutateUserDataDare(
             { dare_id: +id, token: String(token) },

+ 23 - 6
src/screens/LoginScreen/index.tsx

@@ -36,6 +36,7 @@ const LoginScreen: FC<Props> = ({ navigation }) => {
     data?.uid ? +data.uid : 0,
     data?.token ? true : false
   );
+  const [isSubmitting, setIsSubmitting] = useState(false);
 
   const updateLocalData = async (token: string) => {
     await fetchAndSaveStatistics(token);
@@ -63,6 +64,7 @@ const LoginScreen: FC<Props> = ({ navigation }) => {
         homebase_flag: profileData?.data?.user_data.flag1
       };
       storage.set('currentUserData', JSON.stringify(userInfo));
+      setIsSubmitting(false);
 
       isFirstLaunch
         ? dispatch(
@@ -88,12 +90,25 @@ const LoginScreen: FC<Props> = ({ navigation }) => {
           login: '',
           pass: ''
         }}
-        onSubmit={({ login, pass }) => {
+        onSubmit={async ({ login, pass }) => {
+          setIsSubmitting(true);
           if (netInfo?.isInternetReachable || netInfo?.isConnected) {
-            userLogin({
-              login,
-              pass
-            });
+            await userLogin(
+              {
+                login,
+                pass
+              },
+              {
+                onSuccess: (res) => {
+                  if (res && !res?.token) {
+                    setIsSubmitting(false);
+                  }
+                },
+                onError: () => {
+                  setIsSubmitting(false);
+                }
+              }
+            );
           } else {
             setIsWarningModalVisible(true);
           }
@@ -132,7 +147,9 @@ const LoginScreen: FC<Props> = ({ navigation }) => {
               >
                 Forgot password
               </Button>
-              <Button onPress={props.handleSubmit}>Login</Button>
+              <Button onPress={props.handleSubmit} disabled={isSubmitting}>
+                Login
+              </Button>
             </View>
           </>
         )}

+ 13 - 3
src/screens/RegisterScreen/EditAccount/index.tsx

@@ -40,6 +40,7 @@ const EditAccount = () => {
     data?.token ? true : false
   );
   const [isLoading, setIsLoading] = useState(true);
+  const [isSubmitting, setIsSubmitting] = useState(false);
 
   useFocusEffect(
     useCallback(() => {
@@ -60,6 +61,7 @@ const EditAccount = () => {
         homebase_flag: profileData?.data?.user_data.flag1
       };
       storage.set('currentUserData', JSON.stringify(userInfo));
+      setIsSubmitting(false);
 
       dispatch(
         CommonActions.reset({
@@ -99,7 +101,8 @@ const EditAccount = () => {
                 homebase2: undefined
               }}
               validationSchema={SignUpSchema}
-              onSubmit={(values) => {
+              onSubmit={async (values) => {
+                setIsSubmitting(true);
                 const data = {
                   user: {
                     ...user,
@@ -122,13 +125,18 @@ const EditAccount = () => {
                       }
                 };
 
-                userRegister(data, {
+                await userRegister(data, {
                   onSuccess: (data) => {
                     if (data && data.token) {
                       storage.set('token', data.token);
                       storage.set('uid', data.uid.toString());
                       updateLocalData(data.token);
+                    } else {
+                      setIsSubmitting(false);
                     }
+                  },
+                  onError: () => {
+                    setIsSubmitting(false);
                   }
                 });
               }}
@@ -180,7 +188,9 @@ const EditAccount = () => {
                     selectedObject={(data) => props.setFieldValue('homebase2', data.id)}
                   />
                   <View style={{ marginTop: 10 }}>
-                    <Button onPress={props.handleSubmit}>Sign up</Button>
+                    <Button onPress={props.handleSubmit} disabled={isSubmitting}>
+                      Sign up
+                    </Button>
                   </View>
                 </View>
               )}