XRay876

Untitled

Nov 18th, 2025
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useState } from 'react';
  2. import { StyleSheet, Text, View, SafeAreaView, Alert, TextInput, Button, Modal, TouchableOpacity, Platform, ToastAndroid } from 'react-native';
  3.  
  4.  
  5. export default function App() {
  6.  
  7.   const [showModal, setShowModal] = useState(false);
  8.   const [response, setResponse] = useState(null);
  9.   const [name, setName] = useState("");
  10.  
  11.   const fetchApi = async () => {
  12.     try {
  13.       const res = await fetch(`https://api.agify.io?name=${name}`);
  14.       const data = await res.json();
  15.  
  16.       if (!data.age) {
  17.         Alert.alert("No Prediction Found", "Unable to predict age for this name.");
  18.         return;
  19.       }
  20.  
  21.       setResponse(data);
  22.       if (Platform.OS === "android") {
  23.         ToastAndroid.show("Prediction was found!", ToastAndroid.SHORT);
  24.       } else {
  25.         Alert.alert("Prediction Found");
  26.       }
  27.       setShowModal(true);
  28.      
  29.     } catch (e) {
  30.       Alert.alert("error", e);
  31.     }
  32.   }
  33.  
  34.   const hadleGetAge = () => {
  35.     if (!name.trim()) {
  36.       Alert.alert("Validation error", "Enter a name");
  37.       return;
  38.     }
  39.  
  40.     Alert.alert("Confirm", `Fetch age prediction for "${name}"`, [{text:"Cancel", style:"cancel"}, {text: "Ok", onPress: fetchApi}]);
  41.   }
  42.  
  43.   return (
  44.     <SafeAreaView style={styles.container}>
  45.       <Text style={styles.lable}>Predict Your Age</Text>
  46.  
  47.       <TextInput
  48.         placeholder="Enter a name"
  49.         value={name}
  50.         onChangeText={setName}
  51.         style={styles.input}
  52.       />
  53.  
  54.       <Button title="Predict Age" onPress={hadleGetAge}>
  55.  
  56.       </Button>
  57.  
  58.       <Modal visible={showModal} animationType="fade">
  59.         <View style={styles.modalContainer}>
  60.           <View style={styles.modalContent}>
  61.             {response && (
  62.               <View>
  63.                 <Text style={styles.modalTitle}>Age Prediction</Text>
  64.  
  65.                 <View style={{ marginTop: 10 }}>
  66.                   <Text style={styles.modalText}>Name: {response.name}</Text>
  67.                   <Text style={styles.modalText}>Age: {response.age}</Text>
  68.                   <Text style={styles.modalText}>Count: {response.count}</Text>
  69.                 </View>
  70.               </View>
  71.             )}
  72.  
  73.             <TouchableOpacity
  74.               onPress={() => setShowModal(false)}
  75.               style={styles.closeButton}
  76.             >
  77.               <Text style={styles.closeButtonText}>Close</Text>
  78.             </TouchableOpacity>
  79.           </View>
  80.         </View>
  81.       </Modal>
  82.     </SafeAreaView>
  83.   );
  84. }
  85.  
  86. const styles = StyleSheet.create({
  87.   container: {
  88.     flex: 1,
  89.     justifyContent: "center",
  90.     alignItems: "center",
  91.     padding: 20,
  92.   },
  93.   input: {
  94.     width: "80%",
  95.     padding: 12,
  96.     borderWidth: 1,
  97.     borderRadius: 6,
  98.     marginBottom: 12,
  99.     fontSize: 16,
  100.   },
  101.   modalContainer: {
  102.     flex: 1,
  103.     justifyContent: "center",
  104.     alignItems: "center",
  105.     backgroundColor: "rgba(0,0,0,0.5)",
  106.   },
  107.   modalContent: {
  108.     width: "80%",
  109.     backgroundColor: "white",
  110.     padding: 20,
  111.     borderRadius: 10,
  112.     elevation: 10,
  113.   },
  114.   modalTitle: {
  115.     fontSize: 22,
  116.     fontWeight: "bold",
  117.     marginBottom: 15,
  118.   },
  119.   modalText: {
  120.     fontSize: 18,
  121.     marginVertical: 4,
  122.   },
  123.   closeButton: {
  124.     backgroundColor: "#2196F3",
  125.     marginTop: 20,
  126.     paddingVertical: 12,
  127.     borderRadius: 8,
  128.   },
  129.   closeButtonText: {
  130.     color: "white",
  131.     textAlign: "center",
  132.     fontSize: 18,
  133.     fontWeight: "600",
  134.   },
  135.   lable: {
  136.     fontWeight: 700,
  137.     paddingBottom: 65,
  138.     fontSize: 30,
  139.   }
  140. });
Advertisement
Add Comment
Please, Sign In to add comment