Advertisement
tchicotti

ju-react

Nov 21st, 2024
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react';
  2. import { View, Text, TextInput, Button, Alert, FlatList, Image, TouchableOpacity, StyleSheet } from 'react-native';
  3.  
  4. export default function App() {
  5.   const [screen, setScreen] = useState('login'); // Define qual tela será exibida
  6.   const [username, setUsername] = useState('jose.silva');
  7.   const [password, setPassword] = useState('123456');
  8.   const [userName, setUserName] = useState('');
  9.   const [userEmail, setUserEmail] = useState([]);
  10.   const [userAddress, setUserAddress] = useState('');
  11.   const [userPhone, setUserPhone] = useState('');
  12.   const [movies, setMovies] = useState([]);
  13.  
  14.  
  15.   const handleLogin = () => {
  16.     if (username === 'jose.silva' && password === '123456') {
  17.       setScreen('main');
  18.     } else {
  19.       Alert.alert('Erro', 'Usuário ou senha incorretos!');
  20.     }
  21.   };
  22.  
  23.   const handleFetchMovies = () => {
  24.     fetch('https://reactnative.dev/movies.json')
  25.       .then(response => response.json())
  26.       .then(data => setMovies(data.movies))
  27.       .catch(() => Alert.alert('Erro', 'Não foi possível carregar os filmes.'));
  28.   };
  29.  
  30.   // Tela de Login
  31.   if (screen === 'login') {
  32.     return (
  33.       <View style={styles.container}>
  34.         <Text style={styles.title}>Login</Text>
  35.         <TextInput
  36.           placeholder="Usuário"
  37.           style={styles.input}
  38.           value={username}
  39.           onChangeText={setUsername}
  40.         />
  41.         <TextInput
  42.           placeholder="Senha"
  43.           style={styles.input}
  44.           secureTextEntry
  45.           value={password}
  46.           onChangeText={setPassword}
  47.         />
  48.         <Button title="Logar" onPress={handleLogin} />
  49.       </View>
  50.     );
  51.   }
  52.  
  53.   // Tela Principal
  54.   if (screen === 'main') {
  55.     return (
  56.       <View style={styles.container}>
  57.         <Button title="Cadastro de Usuários" onPress={() => setScreen('cadastro')} />
  58.         <Button title="Visualizar Filmes" onPress={() => {
  59.           handleFetchMovies();
  60.           setScreen('filmes');
  61.         }} />
  62.       </View>
  63.     );
  64.   }
  65.  
  66.   // Tela de Cadastro
  67.   if (screen === 'cadastro') {
  68.     // Função para aplicar a máscara de telefone com DDD
  69.     const handlePhoneChange = (text) => {
  70.       // Remove todos os caracteres não numéricos
  71.       let phone = text.replace(/\D/g, '');
  72.  
  73.       // Limita o número de caracteres a 11 (considerando o formato "(XX) XXXXX-XXXX")
  74.       if (phone.length > 11) {
  75.         phone = phone.substring(0, 11);
  76.       }
  77.  
  78.       // Adiciona a máscara
  79.       if (phone.length <= 2) {
  80.         phone = phone.replace(/^(\d{0,2})/, '($1');
  81.       }
  82.       if (phone.length > 2 && phone.length <= 6) {
  83.         phone = phone.replace(/^(\d{2})(\d{0,5})/, '($1) $2');
  84.       }
  85.       if (phone.length > 6) {
  86.         phone = phone.replace(/^(\d{2})(\d{5})(\d{0,4})/, '($1) $2-$3');
  87.       }
  88.  
  89.       // Atualiza o estado com o telefone formatado
  90.       setUserPhone(phone);
  91.     };
  92.    
  93.     return (
  94.       <View style={styles.container}>
  95.         <Text style={styles.title}>Cadastro de Usuários</Text>
  96.         <TextInput
  97.           placeholder="Nome"
  98.           style={styles.input}
  99.           value={userName}
  100.           onChangeText={setUserName}
  101.         />
  102.         <TextInput
  103.           placeholder="Endereço"
  104.           style={styles.input}
  105.           value={userAddress}
  106.           onChangeText={setUserAddress}
  107.         />
  108.         <TextInput
  109.           placeholder="Email"
  110.           style={styles.input}
  111.           value={userEmail}
  112.           onChangeText={setUserEmail}
  113.         />
  114.         <TextInput
  115.           placeholder="Telefone"
  116.           style={styles.input}
  117.           value={userPhone}
  118.           maxLength={15}
  119.           onChangeText={handlePhoneChange}
  120.           keyboardType="phone-pad"
  121.         />
  122.  
  123.         <Button
  124.           title="Salvar"
  125.           onPress={() => {
  126.             Alert.alert('Sucesso', `Usuário ${userName} cadastrado!`);
  127.             setScreen('main');
  128.           }}
  129.         />
  130.       </View>
  131.     );
  132.   }
  133.  
  134.   // Tela de Filmes
  135.   if (screen === 'filmes') {
  136.     return (
  137.       <FlatList
  138.         data={movies}
  139.         keyExtractor={item => item.id}
  140.         renderItem={({ item }) => (
  141.           <TouchableOpacity
  142.             onPress={() => Alert.alert('Filme selecionado', item.title)}
  143.             style={styles.item}
  144.           >
  145.             <Image
  146.               source={{ uri: `https://via.placeholder.com/100x150.png?text=${item.title}`}}
  147.               style={styles.image}
  148.             />
  149.             <View>
  150.               <Text style={styles.title}>{item.title}</Text>
  151.               <Text>{item.releaseYear}</Text>
  152.             </View>
  153.           </TouchableOpacity>
  154.         )}
  155.         ListHeaderComponent={
  156.           <View style={{ padding: 16 }}>
  157.             <Button title="Voltar" onPress={() => setScreen('main')} />
  158.           </View>
  159.         }
  160.       />
  161.     );
  162.   }
  163.  
  164.   return null;
  165. }
  166.  
  167. const styles = StyleSheet.create({
  168.   container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16 },
  169.   title: { fontSize: 20, fontWeight: 'bold', marginBottom: 16 },
  170.   input: { width: '80%', height: 40, borderColor: 'gray', borderWidth: 1, marginBottom: 12, paddingHorizontal: 8 },
  171.   item: { flexDirection: 'row', marginBottom: 16, padding: 8, backgroundColor: '#f9f9f9' },
  172.   image: { width: 100, height: 150, marginRight: 16 },
  173. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement