Created
October 29, 2024 14:14
-
-
Save andersonFaro9/660e3d28ec1ee20d66be7f47c49a5939 to your computer and use it in GitHub Desktop.
na lista de ações, cada card deve clicar no coração para favoritar ou não, como tornar o botão de coração de favoritar dinâmico sem que todos sejam selecionados de uma única vez?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { | |
View, | |
ScrollView, | |
StyleSheet, | |
Image, | |
Switch, | |
TouchableOpacity, | |
TouchableHighlight, | |
TouchableWithoutFeedback, | |
Pressable, | |
} from 'react-native' | |
import { Text, Card, Button, Icon, Divider } from '@rneui/themed' | |
import { useEffect, useState } from 'react' | |
import { MaterialCommunityIcons } from '@expo/vector-icons' | |
import Ionicons from '@expo/vector-icons/Ionicons' | |
interface IActions { | |
id: number, | |
name: string, | |
ticker: string, | |
minimumValue: number, | |
profitability:number, | |
heart: string, | |
} | |
const Actions = () => { | |
const [actions,setActions] = useState<IActions[]>([]) | |
const [loading,setLoading] = useState(false) | |
const currencyBRL = (value: number) => value.toLocaleString('pt-BR') | |
const [liked, setLiked] = useState(false) | |
const toggleLiked = () => setLiked((previousState) => !previousState) | |
async function getActions() { | |
setLoading(true) | |
try { | |
const response = await fetch( | |
'https://6266f62263e0f382568936e4.mockapi.io/stocks' | |
) | |
const json = await response.json() | |
setActions(json.data) | |
} catch(error) { | |
console.log(error) | |
} finally { | |
setLoading(false); | |
} | |
} | |
useEffect(() => { | |
getActions() | |
}, []) | |
return ( | |
<ScrollView> | |
<View> | |
{loading ? ( | |
<View style={styles.loading}> | |
<Image source={require('../../assets/images/load.png')} /> | |
</View> | |
) : ( | |
<View> | |
{actions.map((details) => { | |
return ( | |
<TouchableOpacity key={details.id} onPress={toggleLiked}> | |
<View style={styles.success}> | |
<View key={details.id}> | |
<View> | |
{liked ? ( | |
<MaterialCommunityIcons | |
name={'heart'} | |
size={32} | |
color={liked ? 'red' : 'black'} | |
/> | |
) : ( | |
<MaterialCommunityIcons | |
name={'heart-outline'} | |
size={32} | |
color={liked ? 'red' : 'black'} | |
/> | |
)} | |
</View> | |
</View> | |
<View style={styles.details} key={details.id}> | |
<Text style={styles.name}>{details.name}</Text> | |
</View> | |
<View style={styles.ticker}> | |
<Text style={styles.details}>{details.ticker}</Text> | |
</View> | |
<Divider style={styles.divider} /> | |
<View style={styles.minimumValue}> | |
<Text style={styles.minimumValueText}>Valor minimo:</Text> | |
<Text style={styles.details}> | |
R$ {currencyBRL(details.minimumValue)} | |
</Text> | |
</View> | |
<View style={styles.profitability}> | |
<Text>Rentabilidade:</Text> | |
<Text style={styles.profitabilityText}> | |
<Ionicons name='arrow-down' size={15} /> | |
{details.profitability}% | |
</Text> | |
</View> | |
</View> | |
</TouchableOpacity> | |
) | |
})} | |
</View> | |
)} | |
</View> | |
</ScrollView> | |
) | |
} | |
const styles = StyleSheet.create({ | |
success: { | |
flexDirection: 'column', | |
justifyContent: 'space-around', | |
width: 380, | |
paddingTop: 5, | |
paddingLeft: 22, | |
paddingRight: 42, | |
borderRadius: 14, | |
borderColor: '#1111', | |
backgroundColor: 'white', | |
borderWidth: 2, | |
margin: 12, | |
height: 160, | |
}, | |
loading : { | |
flexDirection: 'row', | |
justifyContent: 'center', | |
margin: 100, | |
alignItems: 'center' | |
}, | |
details: { | |
flexDirection: 'row', | |
justifyContent: 'space-between', | |
fontWeight: 'bold', | |
}, | |
name: { | |
fontWeight: 'bold', | |
}, | |
ticker: { | |
marginTop: -12, | |
flexDirection: 'column', | |
justifyContent: 'flex-start', | |
}, | |
divider: { | |
marginTop: 10, | |
}, | |
minimumValue: { | |
marginTop: 2, | |
flexDirection: 'row', | |
justifyContent: 'space-between', | |
}, | |
minimumValueText: { | |
}, | |
profitability: { | |
marginBottom: 15, | |
flexDirection: 'row', | |
justifyContent: 'space-between', | |
}, | |
profitabilityText: { | |
color: '#E85D1F', | |
fontWeight: 'bold', | |
}, | |
}) | |
export default Actions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment