-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontEnd.Client
More file actions
344 lines (286 loc) · 8 KB
/
FrontEnd.Client
File metadata and controls
344 lines (286 loc) · 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
**Part 2: Frontend Development with React**
Now let's move forward with setting up the **frontend** of the **Job Connect Platform** using **React.js**. We'll be using **Axios** for making HTTP requests to the backend, **React Router** for navigation, and **React Context API** for managing the state.
---
**Step 1: Setting Up the Frontend Environment**
1. **Create a new React app**:
Open a terminal and navigate to the root of your project directory, then create a new React app:
```bash
npx create-react-app client
cd client
```
2. **Install required dependencies**:
In the `client` folder, install the following dependencies:
```bash
npm install axios react-router-dom react-toastify
```
---
### **Step 2: Folder Structure and Initial Setup**
1. **Create the following folder structure**:
```bash
src/
├── components/
│ ├── Auth/
│ ├── Job/
├── context/
├── pages/
├── App.js
├── index.js
```
2. **Set up React Router**:
In `src/App.js`, import and set up **React Router**:
```js
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Home from "./pages/Home";
import Register from "./pages/Register";
import Login from "./pages/Login";
import JobListing from "./pages/JobListing";
import CreateJob from "./pages/CreateJob";
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/register" element={<Register />} />
<Route path="/login" element={<Login />} />
<Route path="/jobs" element={<JobListing />} />
<Route path="/create-job" element={<CreateJob />} />
</Routes>
</Router>
);
}
export default App;
```
3. **Set up the Entry Point** in `src/index.js`:
```js
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
```
---
**Step 3: Creating the Authentication Pages**
1. **Create Register Page** (`src/pages/Register.js`):
In this file, users can register with their details (username, email, password, and role):
```js
import React, { useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";
const Register = () => {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [role, setRole] = useState("job_seeker");
const navigate = useNavigate();
const handleSubmit = async (e) => {
e.preventDefault();
try {
await axios.post("http://localhost:5000/api/auth/register", {
username,
email,
password,
role,
});
navigate("/login");
} catch (error) {
console.error(error);
}
};
return (
<div>
<h2>Register</h2>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="Username"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<input
type="email"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<select
value={role}
onChange={(e) => setRole(e.target.value)}
>
<option value="job_seeker">Job Seeker</option>
<option value="recruiter">Recruiter</option>
</select>
<button type="submit">Register</button>
</form>
</div>
);
};
export default Register;
```
2. **Create Login Page** (`src/pages/Login.js`):
Users will log in here, providing their email and password to get the **JWT token**:
```js
import React, { useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";
const Login = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const navigate = useNavigate();
const handleSubmit = async (e) => {
e.preventDefault();
try {
const { data } = await axios.post("http://localhost:5000/api/auth/login", {
email,
password,
});
localStorage.setItem("authToken", data.token); // Save token to localStorage
navigate("/jobs");
} catch (error) {
console.error(error);
}
};
return (
<div>
<h2>Login</h2>
<form onSubmit={handleSubmit}>
<input
type="email"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button type="submit">Login</button>
</form>
</div>
);
};
export default Login;
```
---
**Step 4: Creating Job Listing and Job Posting Pages**
1. **Create Job Listing Page** (`src/pages/JobListing.js`):
This page will display the list of available jobs, fetched from the backend:
```js
import React, { useEffect, useState } from "react";
import axios from "axios";
const JobListing = () => {
const [jobs, setJobs] = useState([]);
useEffect(() => {
const fetchJobs = async () => {
try {
const { data } = await axios.get("http://localhost:5000/api/jobs");
setJobs(data);
} catch (error) {
console.error(error);
}
};
fetchJobs();
}, []);
return (
<div>
<h2>Available Jobs</h2>
{jobs.map((job) => (
<div key={job._id}>
<h3>{job.title}</h3>
<p>{job.companyName}</p>
<p>{job.location}</p>
<p>{job.jobDescription}</p>
</div>
))}
</div>
);
};
export default JobListing;
```
2. **Create Job Posting Page** (`src/pages/CreateJob.js`):
Only **recruiters** can post jobs:
```js
import React, { useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";
const CreateJob = () => {
const [title, setTitle] = useState("");
const [companyName, setCompanyName] = useState("");
const [location, setLocation] = useState("");
const [jobDescription, setJobDescription] = useState("");
const navigate = useNavigate();
const handleSubmit = async (e) => {
e.preventDefault();
const token = localStorage.getItem("authToken");
try {
await axios.post(
"http://localhost:5000/api/jobs",
{ title, companyName, location, jobDescription },
{
headers: { Authorization: `Bearer ${token}` },
}
);
navigate("/jobs");
} catch (error) {
console.error(error);
}
};
return (
<div>
<h2>Create Job</h2>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="Job Title"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<input
type="text"
placeholder="Company Name"
value={companyName}
onChange={(e) => setCompanyName(e.target.value)}
/>
<input
type="text"
placeholder="Location"
value={location}
onChange={(e) => setLocation(e.target.value)}
/>
<textarea
placeholder="Job Description"
value={jobDescription}
onChange={(e) => setJobDescription(e.target.value)}
/>
<button type="submit">Post Job</button>
</form>
</div>
);
};
export default CreateJob;
```
---
**Step 5: Running the Application**
1. **Run the Backend**:
In the `server` directory, start the backend:
```bash
npm run dev
```
2. **Run the Frontend**:
In the `client` directory, start the frontend:
```bash
npm start
```
---