-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangesToAddAdmin.txt
More file actions
175 lines (145 loc) · 4.86 KB
/
ChangesToAddAdmin.txt
File metadata and controls
175 lines (145 loc) · 4.86 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
utils->api.js::
paste at last
export const deleteResidency = async (residencyId) => {
try {
const response = await api.delete(`/residency/delete/${residencyId}`
// , {
// headers: {
// Authorization: `Bearer ${token}`,
// },
);
if (response.status === 400 || response.status === 500) {
throw response.data;
}
return response.data;
} catch (error) {
toast.error("Something went wrong while deleting residency");
throw error;
}
}
App.jsx::
import AdminLogin from "./components/Admin/AdminLogin"
import AdminPage from "./components/Admin/AdminPage"
<Route path="/adminLogin" element={<AdminLogin/>} />
<Route path="/admin" element={<AdminPage/>} />
Above booking
components->Admin
AdminLogin.jsx::
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import Admin from './AdminPage'; // Import the Admin component
const AdminLogin = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const navigate = useNavigate();
const handleLogin = async (e) => {
e.preventDefault();
// Simulated authentication logic
const isAdminAuthenticated = username === 'admin' && password === 'adminpassword';
if (isAdminAuthenticated) {
// Redirect to the Admin component after successful login
navigate('/admin');
} else {
console.log('Admin login failed. Invalid credentials.');
}
};
return (
<div className="flex items-center justify-center h-screen">
<div className="bg-white p-8 shadow-md rounded-md w-96">
<h2 className="text-2xl font-bold mb-4">Admin Login</h2>
<form onSubmit={handleLogin}>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-600">Username:</label>
<input
type="text"
className="mt-1 p-2 border rounded-md w-full"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-600">Password:</label>
<input
type="password"
className="mt-1 p-2 border rounded-md w-full"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<button
type="submit"
className="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600"
>
Login
</button>
</form>
</div>
</div>
);
};
export default AdminLogin;
AdminPage.jsx::
// AdminPage.js
import React, { useState, useEffect } from 'react';
import { getAllProperties, deleteResidency } from '../../utils/api';
const AdminPage = () => {
const [residencies, setResidencies] = useState([]);
const fetchData = async () => {
try {
const data = await getAllProperties();
setResidencies(data);
} catch (error) {
console.error('Error fetching residencies:', error);
}
};
const handleDeleteResidency = async (id) => {
try {
await deleteResidency(id);
alert('Residency deleted successfully');
fetchData();
} catch (error) {
console.error('Error deleting residency:', error);
alert('Error deleting residency');
}
};
useEffect(() => {
fetchData();
}, []);
return (
<div className="container mx-auto my-8">
<h1 className="text-3xl font-semibold mb-4 text-white">Admin Page</h1>
<table className="min-w-full bg-white border border-gray-300 shadow-md">
<thead>
<tr>
<th className="py-2 px-4 border-b">Title</th>
<th className="py-2 px-4 border-b">Description</th>
<th className="py-2 px-4 border-b">Price</th>
<th className="py-2 px-4 border-b">Actions</th>
</tr>
</thead>
<tbody>
{residencies.map((residency) => (
<tr key={residency.id}>
<td className="py-2 px-4 border-b">{residency.title}</td>
<td className="py-2 px-4 border-b">{residency.description}</td>
<td className="py-2 px-4 border-b">{residency.price}</td>
<td className="py-2 px-4 border-b">
<button
onClick={() => handleDeleteResidency(residency.id)}
className="bg-red-500 text-white py-1 px-2 rounded"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
};
export default AdminPage;
Component->Header
Header.jsx::
<Link to="/adminLogin">Admin</Link>
before navlink