-
Notifications
You must be signed in to change notification settings - Fork 0
/
57_modal_passdata_and_function_to_modal.js
117 lines (93 loc) · 2.48 KB
/
57_modal_passdata_and_function_to_modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React,{useEffect,useState} from 'react';
import {Platform,Button, TextInput,Text,StatusBar, View,ActivityIndicator,StyleSheet,Modal,Pressable,ScrollView,FlatList } from 'react-native';
import axios from 'axios';
const App = () => {
const [data,setData]=useState([])
const [showModal,setShowModal]=useState(false);
const [selectedUser,setSelectedUser]=useState(undefined)
const getAPIData = async ()=>{
const url = "http://10.0.2.2:3000/users";
let result = await fetch(url);
result = await result.json();
console.warn(result);
if(result){
setData(result);
}
}
const deleteUser = async (id)=>{
const url = "http://10.0.2.2:3000/users/"+id;
let result = await fetch(url,{
method:'DELETE'
});
result = await result.json();
console.warn(result);
if(result){
console.warn('deleted');
getAPIData();
}
}
const updateUser = (data)=>{
setShowModal(true);
setSelectedUser(data);
}
useEffect(()=>{
getAPIData();
},[])
return (
<View style={styles.container}>
{
data.length?
data.map((item)=>{
return (
<View style={styles.dataWrapper}>
<View style={{flex:1}}><Text>{item.name}</Text></View>
<View style={{flex:1}}><Text>{item.age}</Text></View>
<View style={{flex:1}}> <Button title='Delete' onPress={()=>deleteUser(item.id)} ></Button></View>
<View style={{flex:1}}> <Button title='Update' onPress={()=>updateUser(item)}></Button></View>
</View>
)
}):null
}
<Modal visible={showModal} transparent={true}>
<UserModal setShowModal={setShowModal} selectedUser={selectedUser}/>
</Modal>
</View>
);
};
const UserModal = (props)=>{
console.warn(props.selectedUser);
return(
<View style={styles.centredView}>
<View style={styles.modalView}>
<Text>Do you really want to delete +{props.selectedUser.name}</Text>
<Button title="CLOSE" onPress={()=>props.setShowModal(false)}></Button>
</View>
</View>)
}
const styles=StyleSheet.create({
container:{
flex:1,
},
dataWrapper:{
flexDirection:'row',
justifyContent:'space-around',
backgroundColor:'lightgrey',
margin:10,
padding:5
},
centredView:{
flex:1,
justifyContent:'center',
alignItems:'center',
},
modalView:{
margin:20,
backgroundColor:"#fff",
padding:60,
borderRadius:10,
shadowColor:'#000',
shadowOpacity:0.75,
elevation:5,
}
})
export default App;