Skip to content

Commit

Permalink
CHE-39 Set up routing and basic RegistrationPage component
Browse files Browse the repository at this point in the history
  • Loading branch information
brok3turtl3 committed Mar 27, 2024
1 parent cecb9e5 commit eab47e1
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 4 deletions.
4 changes: 3 additions & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import LandingPage from "./pages/LandingPage";
import NotFoundPage from "./pages/NotFoundPage/NotFoundPage";
import AuthenticatedApp from "./AuthenticatedApp"
import AuthenticatedApp from "./AuthenticatedApp";
import RegistrationPage from "./pages/RegistrationPage/RegistrationPage";

const App = (): JSX.Element => {
return (
<Router>
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="/registration/" element={<RegistrationPage />} />
<Route path="/app/*" element={<AuthenticatedApp />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
Expand Down
138 changes: 135 additions & 3 deletions client/src/pages/RegistrationPage/RegistrationPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,142 @@
import React from "react";
import React, { useState, useEffect } from "react";
import { useLocation } from "react-router-dom";

const RegistrationPage: React.FC = () => {
const [formData, setFormData] = useState({
firstName: "",
lastName: "",
email: "",
password: "",
});

const location = useLocation();

const query = new URLSearchParams(location.search);
const token = query.get("token");

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setFormData({
...formData,
[e.target.name]: e.target.value,
});
};

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!token) {
console.error("Token is missing.");
return; //TODO Display error feedback for user
}
try {
const response = await fetch(`/api/users/register?token=${token}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
});
const data = await response.json();
if (!response.ok) {
throw new Error(
data.message || "An error occurred during registration."
);
}
console.log("Registration successful", data);
//TODO Handle redirect here
} catch (error) {
//TODO Needs better error handling
console.error("Registration error:", error);
}
};

return (
<div className="min-h-screen bg-gray-100 flex flex-col items-center justify-center">
<h1 className="text-4xl font-extrabold mb-4">Main Page</h1>
<h1>Registration Form Goes Here!</h1>
<div className="w-full max-w-xs">
<h1 className="text-4xl font-extrabold mb-4 text-center">
Registration Page
</h1>
<form
className="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4"
onSubmit={handleSubmit}
>
<div className="mb-4">
<label
className="block text-gray-700 text-sm font-bold mb-2"
htmlFor="firstName"
>
First Name
</label>
<input
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
id="firstName"
name="firstName"
type="text"
value={formData.firstName}
onChange={handleChange}
required
/>
</div>
<div className="mb-4">
<label
className="block text-gray-700 text-sm font-bold mb-2"
htmlFor="lastName"
>
Last Name
</label>
<input
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
id="lastName"
name="lastName"
type="text"
value={formData.lastName}
onChange={handleChange}
required
/>
</div>
<div className="mb-4">
<label
className="block text-gray-700 text-sm font-bold mb-2"
htmlFor="email"
>
Email
</label>
<input
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
id="email"
name="email"
type="email"
value={formData.email}
onChange={handleChange}
required
/>
</div>
<div className="mb-6">
<label
className="block text-gray-700 text-sm font-bold mb-2"
htmlFor="password"
>
Password
</label>
<input
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline"
id="password"
name="password"
type="password"
value={formData.password}
onChange={handleChange}
required
/>
</div>
<div className="flex items-center justify-between">
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
type="submit"
>
Register
</button>
</div>
</form>
</div>
</div>
);
};
Expand Down

0 comments on commit eab47e1

Please sign in to comment.