ReactJS – Creating Dashboard Page after login
To add a Main Page with a navigation menu that includes Dashboard (displays the welcome message) and Customer (takes customer details for insurance), follow these steps:
Steps to Implement
- Update Folder Structure
Update your folder structure to organize components:
src/
├── components/
│ ├── LoginPage.js # Login page component
│ ├── MainPage.js # Main page with navigation menu
│ ├── Dashboard.js # Dashboard (displays welcome message)
│ ├── CustomerForm.js # Customer form for insurance details
├── App.js # Main application file
└── index.js # Entry point
- Create Components
a. Dashboard Component
This component will display the welcome message.
Create Dashboard.js:
import React from 'react';
const Dashboard = () => {
return (
<div style={styles.container}>
<h1 style={styles.heading}>Welcome to Insurance Portal</h1>
<p style={styles.paragraph}>Your trusted partner for all insurance needs!</p>
</div>
);
};
const styles = {
container: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
backgroundColor: '#f0f8ff',
color: '#333',
fontFamily: 'Arial, sans-serif',
},
heading: {
fontSize: '3rem',
marginBottom: '1rem',
},
paragraph: {
fontSize: '1.2rem',
textAlign: 'center',
},
};
export default Dashboard;
b. CustomerForm Component
This component will take customer details.
Create CustomerForm.js:
import React, { useState } from 'react';
const CustomerForm = () => {
const [formData, setFormData] = useState({
name: '',
email: '',
phone: '',
insuranceType: '',
});
const handleChange = (e) => {
const { name, value } = e.target;
setFormData({ ...formData, [name]: value });
};
const handleSubmit = (e) => {
e.preventDefault();
alert(`Customer Details Submitted:\n${JSON.stringify(formData, null, 2)}`);
setFormData({ name: '', email: '', phone: '', insuranceType: '' }); // Reset form
};
return (
<div style={styles.container}>
<h1 style={styles.heading}>Customer Insurance Form</h1>
<form onSubmit={handleSubmit} style={styles.form}>
<input
type="text"
name="name"
placeholder="Customer Name"
value={formData.name}
onChange={handleChange}
style={styles.input}
required
/>
<input
type="email"
name="email"
placeholder="Email"
value={formData.email}
onChange={handleChange}
style={styles.input}
required
/>
<input
type="tel"
name="phone"
placeholder="Phone Number"
value={formData.phone}
onChange={handleChange}
style={styles.input}
required
/>
<select
name="insuranceType"
value={formData.insuranceType}
onChange={handleChange}
style={styles.input}
required
>
<option value="" disabled>Select Insurance Type</option>
<option value="health">Health Insurance</option>
<option value="life">Life Insurance</option>
<option value="vehicle">Vehicle Insurance</option>
</select>
<button type="submit" style={styles.button}>Submit</button>
</form>
</div>
);
};
const styles = {
container: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
backgroundColor: '#f9f9f9',
},
heading: {
fontSize: '2rem',
marginBottom: '1.5rem',
},
form: {
display: 'flex',
flexDirection: 'column',
width: '300px',
},
input: {
marginBottom: '1rem',
padding: '10px',
fontSize: '1rem',
},
button: {
padding: '10px',
fontSize: '1rem',
backgroundColor: '#007bff',
color: '#fff',
border: 'none',
cursor: 'pointer',
},
};
export default CustomerForm;
c. MainPage Component
This will include the navigation menu.
Create MainPage.js:
import React from 'react';
import { Link, Outlet } from 'react-router-dom';
const MainPage = () => {
return (
<div style={styles.container}>
<nav style={styles.navbar}>
<Link to="dashboard" style={styles.link}>Dashboard</Link>
<Link to="customer" style={styles.link}>Customer</Link>
</nav>
<div style={styles.content}>
<Outlet /> {/* Placeholder for nested routes */}
</div>
</div>
);
};
const styles = {
container: {
display: 'flex',
flexDirection: 'column',
height: '100vh',
},
navbar: {
display: 'flex',
justifyContent: 'space-around',
padding: '1rem',
backgroundColor: '#333',
},
link: {
color: '#fff',
textDecoration: 'none',
fontSize: '1.2rem',
},
content: {
flex: 1,
padding: '1rem',
},
};
export default MainPage;
d. Update LoginPage componet
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
const LoginPage = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const navigate = useNavigate();
const handleLogin = (e) => {
e.preventDefault();
// Simple mock authentication logic
if (username === 'admin' && password === 'password') {
alert('Login Successful!');
navigate('/main'); // Navigate to the Main Page and Dashboard by default
} else {
alert('Invalid credentials. Please try again!');
}
};
return (
<div style={styles.container}>
<h1 style={styles.heading}>Login to Insurance Portal</h1>
<form onSubmit={handleLogin} style={styles.form}>
<input
type="text"
name="username"
placeholder="Username"
value={username}
onChange={(e) => setUsername(e.target.value)}
style={styles.input}
required
/>
<input
type="password"
name="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
style={styles.input}
required
/>
<button type="submit" style={styles.button}>Login</button>
</form>
</div>
);
};
const styles = {
container: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
backgroundColor: '#f7f9fc',
},
heading: {
fontSize: '2rem',
marginBottom: '1.5rem',
},
form: {
display: 'flex',
flexDirection: 'column',
width: '300px',
},
input: {
marginBottom: '1rem',
padding: '10px',
fontSize: '1rem',
},
button: {
padding: '10px',
fontSize: '1rem',
backgroundColor: '#007bff',
color: '#fff',
border: 'none',
cursor: 'pointer',
},
};
export default LoginPage;
- Update App.js
Modify App.js to handle the new routes:
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import LoginPage from './components/LoginPage';
import MainPage from './components/MainPage';
import Dashboard from './components/Dashboard';
import CustomerForm from './components/CustomerForm';
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<LoginPage />} />
<Route path="/main" element={<MainPage />}>
<Route path="dashboard" element={<Dashboard />} />
<Route path="customer" element={<CustomerForm />} />
</Route>
</Routes>
</Router>
);
}
export default App;
- Run the Application
Start the development server:
npm start
Test the workflow:
- Go to http://localhost:3000 to see the Login Page.
- Log in with valid credentials and navigate to /main.
- Use the Dashboard menu to view the welcome message.
- Use the Customer menu to fill out the insurance form.
Customer Form
Recent Comments