-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
150 lines (140 loc) · 3.8 KB
/
App.js
File metadata and controls
150 lines (140 loc) · 3.8 KB
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
141
142
143
144
145
146
147
148
149
import React, { useState, useEffect } from 'react';
import { View, Text, Button, TextInput, StyleSheet } from 'react-native';
const API_BASE_URL = 'https://dummy.restapiexample.com/api/v1';
const App = () => {
const [employees, setEmployees] = useState([]);
const [employeeName, setEmployeeName] = useState('');
const [employeeSalary, setEmployeeSalary] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
useEffect(() => {
fetchEmployees();
}, []);
const fetchEmployees = async () => {
try {
setIsLoading(true);
const response = await fetch(`${API_BASE_URL}/employees`);
const data = await response.json();
setEmployees(data.data);
} catch (error) {
setErrorMessage('Error fetching employees.');
} finally {
setIsLoading(false);
}
};
const addEmployee = async () => {
try {
setIsLoading(true);
const response = await fetch(`${API_BASE_URL}/create`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: employeeName,
salary: employeeSalary,
}),
});
const data = await response.json();
if (data.status === 'success') {
setEmployeeName('');
setEmployeeSalary('');
fetchEmployees();
} else {
setErrorMessage('Error adding employee.');
}
} catch (error) {
setErrorMessage('Error adding employee.');
} finally {
setIsLoading(false);
}
};
const deleteEmployee = async (id) => {
try {
setIsLoading(true);
const response = await fetch(`${API_BASE_URL}/delete/${id}`, {
method: 'DELETE',
});
const data = await response.json();
if (data.status === 'success') {
fetchEmployees();
} else {
setErrorMessage('Error deleting employee.');
}
} catch (error) {
setErrorMessage('Error deleting employee.');
} finally {
setIsLoading(false);
}
};
return (
<View style={styles.container}>
{isLoading ? (
<Text>Loading...</Text>
) : (
<>
{errorMessage !== '' && <Text style={styles.error}>{errorMessage}</Text>}
<View style={styles.inputContainer}>
<TextInput
style={styles.input}
placeholder="Employee Name"
value={employeeName}
onChangeText={setEmployeeName}
/>
<TextInput
style={styles.input}
placeholder="Employee Salary"
value={employeeSalary}
onChangeText={setEmployeeSalary}
keyboardType="numeric"
/>
<Button title="Add Employee" onPress={addEmployee} />
</View>
<View style={styles.employeeList}>
{employees.map((employee) => (
<View style={styles.employeeItem} key={employee.id}>
<Text>{employee.employee_name}</Text>
<Text>Salary: {employee.employee_salary}</Text>
<Button title="Delete"onPress={() => deleteEmployee(employee.id)} />
</View>
))}
</View>
</>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 16,
},
inputContainer: {
marginBottom: 16,
},
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 4,
padding: 8,
marginBottom: 8,
},
employeeList: {
flex: 1,
width: '100%',
},
employeeItem: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 4,
padding: 8,
marginBottom: 8,
},
error: {
color: 'red',
marginBottom: 16,
},
});
export default App;