-
Notifications
You must be signed in to change notification settings - Fork 0
/
58_populate_data_in_modal.js
140 lines (112 loc) · 3.04 KB
/
58_populate_data_in_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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.0.1: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);
const [name,setName]=useState(undefined);
const [age,setAge]=useState(undefined);
const [email,setEmail]=useState(undefined);
useEffect((props)=>{
setName(props.selectedUser.name);
setAge(props.selectedUser.age);
setEmail(props.selectedUser.email);
},[props.setSelectedUser])
return(
<View style={styles.centredView}>
<View style={styles.modalView}>
<TextInput style={styles.input} value={name}/>
<TextInput style={styles.input} value={age}/>
<TextInput style={styles.input} value={email}/>
<View style={{marginBottom:15}}>
<Button title="UPDATE" />
</View>
<Button title="CLOSE" onPress={()=>props.setShowModal(false)} />
</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:20,
borderRadius:10,
shadowColor:'#000',
shadowOpacity:0.75,
elevation:5,
},
input:{
backgroundColor:'lightgrey',
borderWidth:1,
borderColor:'grey',
marginBottom:10,
}
})
export default App;