Compare commits
1 commit
main
...
Devlopment
Author | SHA1 | Date | |
---|---|---|---|
a42c9f5875 |
71
README.md
71
README.md
|
@ -1,70 +1,3 @@
|
||||||
# Getting Started with Create React App
|
# Assigment4
|
||||||
|
|
||||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
Random User Directory
|
||||||
|
|
||||||
## Available Scripts
|
|
||||||
|
|
||||||
In the project directory, you can run:
|
|
||||||
|
|
||||||
### `npm start`
|
|
||||||
|
|
||||||
Runs the app in the development mode.\
|
|
||||||
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
|
|
||||||
|
|
||||||
The page will reload when you make changes.\
|
|
||||||
You may also see any lint errors in the console.
|
|
||||||
|
|
||||||
### `npm test`
|
|
||||||
|
|
||||||
Launches the test runner in the interactive watch mode.\
|
|
||||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
|
||||||
|
|
||||||
### `npm run build`
|
|
||||||
|
|
||||||
Builds the app for production to the `build` folder.\
|
|
||||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
|
||||||
|
|
||||||
The build is minified and the filenames include the hashes.\
|
|
||||||
Your app is ready to be deployed!
|
|
||||||
|
|
||||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
|
||||||
|
|
||||||
### `npm run eject`
|
|
||||||
|
|
||||||
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
|
|
||||||
|
|
||||||
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
|
||||||
|
|
||||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
|
|
||||||
|
|
||||||
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
|
|
||||||
|
|
||||||
## Learn More
|
|
||||||
|
|
||||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
|
||||||
|
|
||||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
|
||||||
|
|
||||||
### Code Splitting
|
|
||||||
|
|
||||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
|
|
||||||
|
|
||||||
### Analyzing the Bundle Size
|
|
||||||
|
|
||||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
|
|
||||||
|
|
||||||
### Making a Progressive Web App
|
|
||||||
|
|
||||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
|
|
||||||
|
|
||||||
### Advanced Configuration
|
|
||||||
|
|
||||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
|
|
||||||
|
|
||||||
### Deployment
|
|
||||||
|
|
||||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
|
|
||||||
|
|
||||||
### `npm run build` fails to minify
|
|
||||||
|
|
||||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
|
|
38
src/App.css
38
src/App.css
|
@ -4,7 +4,6 @@ body {
|
||||||
background-color: #f4f7fc;
|
background-color: #f4f7fc;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
line-height: 1.6;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.App {
|
.App {
|
||||||
|
@ -17,7 +16,6 @@ body {
|
||||||
padding: 40px 20px;
|
padding: 40px 20px;
|
||||||
color: white;
|
color: white;
|
||||||
border-bottom: 4px solid #0056b3;
|
border-bottom: 4px solid #0056b3;
|
||||||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.App-header h1 {
|
.App-header h1 {
|
||||||
|
@ -52,10 +50,9 @@ body {
|
||||||
|
|
||||||
.search-filter-container input:focus,
|
.search-filter-container input:focus,
|
||||||
.search-filter-container select:focus,
|
.search-filter-container select:focus,
|
||||||
.sort-options select:focus {
|
.sort-options select:focus{
|
||||||
border-color: #007bff;
|
border-color: #007bff;
|
||||||
outline: none;
|
outline: none;
|
||||||
background-color: #e9f5ff;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* User Grid Styles */
|
/* User Grid Styles */
|
||||||
|
@ -112,12 +109,11 @@ body {
|
||||||
border-radius: 25px;
|
border-radius: 25px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
transition: background-color 0.3s ease, transform 0.2s ease;
|
transition: background-color 0.3s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
.view-details-btn:hover {
|
.view-details-btn:hover {
|
||||||
background-color: #0056b3;
|
background-color: #0056b3;
|
||||||
transform: translateY(-2px);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pagination Styles */
|
/* Pagination Styles */
|
||||||
|
@ -202,9 +198,9 @@ body {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Back to User List Button */
|
/* Back to User List Button */
|
||||||
.back-btn,
|
.back-btn ,.export-btn{
|
||||||
.export-btn {
|
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
|
|
||||||
margin-top: 30px;
|
margin-top: 30px;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
background-color: #4CAF50;
|
background-color: #4CAF50;
|
||||||
|
@ -213,44 +209,42 @@ body {
|
||||||
border-radius: 30px;
|
border-radius: 30px;
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
transition: background-color 0.3s ease, transform 0.2s ease;
|
transition: background-color 0.3s ease;
|
||||||
border-width: 0;
|
border-width: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.back-btn:hover,
|
.back-btn:hover,.export-btn:hover {
|
||||||
.export-btn:hover {
|
|
||||||
background-color: #45a049;
|
background-color: #45a049;
|
||||||
transform: translateY(-2px);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Enhanced Buttons */
|
|
||||||
.view-detail {
|
.view-detail
|
||||||
|
{
|
||||||
background-color: #007bff;
|
background-color: #007bff;
|
||||||
width: 120px;
|
width: 120px;
|
||||||
margin-left: 50px;
|
margin-left: 50px;
|
||||||
padding: 8px 20px;
|
padding: 8px 20px;
|
||||||
border-radius: 40px;
|
border-radius: 40px;
|
||||||
transition: background-color 0.3s ease, transform 0.2s ease;
|
|
||||||
}
|
}
|
||||||
|
.view-detail:hover
|
||||||
.view-detail:hover {
|
{
|
||||||
color: #fdfbfb;
|
color: #fdfbfb;
|
||||||
background-color: #0056b3;
|
background-color: #0056b3;
|
||||||
transform: translateY(-2px);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.hero-section button {
|
.hero-section button{
|
||||||
background-color: #6aabf1;
|
background-color: #6aabf1;
|
||||||
width: 120px;
|
width: 120px;
|
||||||
margin-left: 50px;
|
margin-left: 50px;
|
||||||
padding: 8px 20px;
|
padding: 8px 20px;
|
||||||
border-radius: 40px;
|
border-radius: 40px;
|
||||||
border-width: 0;
|
border-width: 0;
|
||||||
transition: background-color 0.3s ease, transform 0.2s ease;
|
|
||||||
}
|
}
|
||||||
|
.hero-section button:hover
|
||||||
.hero-section button:hover {
|
{
|
||||||
color: #fdfbfb;
|
color: #fdfbfb;
|
||||||
background-color: #0056b3;
|
background-color: #0056b3;
|
||||||
transform: translateY(-3px);
|
transform: translateY(-3px);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
25
src/App.js
25
src/App.js
|
@ -1,33 +1,26 @@
|
||||||
// src/App.js
|
// src/App.js
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { BrowserRouter as Router, Routes, Route,useNavigate } from 'react-router-dom';
|
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
import Home from './Home'; // Import the Home component
|
import Home from './Home'; // Import the Home component
|
||||||
import UserDetails from './UserDetail';
|
import UserDetails from './UserDetail';
|
||||||
|
|
||||||
const Header = () =>{
|
|
||||||
const navigate = useNavigate();
|
|
||||||
return (
|
|
||||||
<header className="App-header">
|
|
||||||
<h1>Random Users</h1>
|
|
||||||
<p>Explore a diverse set of random users from all over the world!</p>
|
|
||||||
<div className="hero-section">
|
|
||||||
<button onClick={() => navigate('/home')}>View Users</button>
|
|
||||||
</div>
|
|
||||||
</header>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<Router>
|
<Router>
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<Header/>
|
<header className="App-header">
|
||||||
|
<h1>Random Users</h1>
|
||||||
|
<p>Explore a diverse set of random users from all over the world!</p>
|
||||||
|
<div className="hero-section">
|
||||||
|
<button onClick={() => window.location.href = '/home'}>View Users</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<div>Landing Page</div>} />
|
<Route path="/" element={<div>Landing Page</div>} />
|
||||||
<Route path="/home" element={<Home />} />
|
<Route path="/home" element={<Home />} />
|
||||||
<Route path="/user-details/:username" element={<UserDetails />} />
|
<Route path="/user-details" element={<UserDetails />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</div>
|
||||||
</Router>
|
</Router>
|
||||||
|
|
70
src/Home.js
70
src/Home.js
|
@ -14,27 +14,18 @@ function Home() {
|
||||||
const [selectedNationality, setSelectedNationality] = useState('');
|
const [selectedNationality, setSelectedNationality] = useState('');
|
||||||
const [sortBy, setSortBy] = useState('name'); // Default sort by name
|
const [sortBy, setSortBy] = useState('name'); // Default sort by name
|
||||||
const [sortOrder, setSortOrder] = useState('asc'); // Default sort order: ascending
|
const [sortOrder, setSortOrder] = useState('asc'); // Default sort order: ascending
|
||||||
const [error, setError] = useState(null); // State to hold error messages
|
const [setUserDetail] = useState(null);
|
||||||
const usersPerPage = 12;
|
const usersPerPage = 12;
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
// Fetch data from the API
|
// Fetch data from the API
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
fetch('https://randomuser.me/api/?results=200')
|
||||||
try {
|
.then(response => response.json())
|
||||||
const response = await fetch('https://randomuser.me/api/?results=200');
|
.then(data => {
|
||||||
if (!response.ok) {
|
setData(data.results);
|
||||||
throw new Error('Network response was not ok');
|
setFilteredData(data.results); // Set initial filtered data as the full list
|
||||||
}
|
});
|
||||||
const result = await response.json();
|
|
||||||
setData(result.results);
|
|
||||||
setFilteredData(result.results); // Set initial filtered data as the full list
|
|
||||||
} catch (error) {
|
|
||||||
setError(error.message); // Set the error message
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fetchData();
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Filter users based on search query and selected filters
|
// Filter users based on search query and selected filters
|
||||||
|
@ -44,10 +35,10 @@ function Home() {
|
||||||
// Search by name, email, or location
|
// Search by name, email, or location
|
||||||
if (searchQuery) {
|
if (searchQuery) {
|
||||||
filtered = filtered.filter(user =>
|
filtered = filtered.filter(user =>
|
||||||
user.name?.first?.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
user.name.first.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||||
user.name?.last?.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
user.name.last.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||||
user.email?.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
user.email.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||||
`${user.location?.city} ${user.location?.country}`.toLowerCase().includes(searchQuery.toLowerCase())
|
`${user.location.city} ${user.location.country}`.toLowerCase().includes(searchQuery.toLowerCase())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +51,7 @@ function Home() {
|
||||||
if (selectedAgeRange) {
|
if (selectedAgeRange) {
|
||||||
const [minAge, maxAge] = selectedAgeRange.split('-').map(Number);
|
const [minAge, maxAge] = selectedAgeRange.split('-').map(Number);
|
||||||
filtered = filtered.filter(user => {
|
filtered = filtered.filter(user => {
|
||||||
const userAge = new Date().getFullYear() - new Date(user.dob?.date).getFullYear();
|
const userAge = new Date().getFullYear() - new Date(user.dob.date).getFullYear();
|
||||||
return userAge >= minAge && userAge <= maxAge;
|
return userAge >= minAge && userAge <= maxAge;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -71,21 +62,27 @@ function Home() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sorting logic
|
// Sorting logic
|
||||||
// Sorting logic
|
if (sortBy === 'name') {
|
||||||
if (sortBy === 'name') {
|
filtered = filtered.sort((a, b) => {
|
||||||
filtered = filtered.sort((personA, personB) => {
|
const nameA = `${a.name.first} ${a.name.last}`.toLowerCase();
|
||||||
const fullNameA = `${personA.name?.first} ${personA.name?.last}`.toLowerCase();
|
const nameB = `${b.name.first} ${b.name.last}`.toLowerCase();
|
||||||
const fullNameB = `${personB.name?.first} ${personB.name?.last}`.toLowerCase();
|
if (sortOrder === 'asc') {
|
||||||
return sortOrder === 'asc' ? fullNameA.localeCompare(fullNameB) : fullNameB.localeCompare(fullNameA);
|
return nameA.localeCompare(nameB);
|
||||||
|
} else {
|
||||||
|
return nameB.localeCompare(nameA);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else if (sortBy === 'age') {
|
} else if (sortBy === 'age') {
|
||||||
filtered = filtered.sort((personA, personB) => {
|
filtered = filtered.sort((a, b) => {
|
||||||
const currentYear = new Date().getFullYear();
|
const ageA = new Date().getFullYear() - new Date(a.dob.date).getFullYear();
|
||||||
const ageA = currentYear - new Date(personA.dob?.date).getFullYear();
|
const ageB = new Date().getFullYear() - new Date(b.dob.date).getFullYear();
|
||||||
const ageB = currentYear - new Date(personB.dob?.date).getFullYear();
|
if (sortOrder === 'asc') {
|
||||||
return sortOrder === 'asc' ? ageA - ageB : ageB - ageA;
|
return ageA - ageB;
|
||||||
|
} else {
|
||||||
|
return ageB - ageA;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setFilteredData(filtered);
|
setFilteredData(filtered);
|
||||||
setCurrentPage(1); // Reset to first page whenever filters or sorting change
|
setCurrentPage(1); // Reset to first page whenever filters or sorting change
|
||||||
|
@ -109,12 +106,12 @@ if (sortBy === 'name') {
|
||||||
|
|
||||||
// Function to handle clicking on More Info
|
// Function to handle clicking on More Info
|
||||||
const handleMoreInfo = (user) => {
|
const handleMoreInfo = (user) => {
|
||||||
navigate(`/user-details`,{state:{user}});
|
setUserDetail(user);
|
||||||
|
navigate(`/user-details`);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="Home">
|
<div className="Home">
|
||||||
{error && <div className="error">Error: {error}</div>} {/* Display error message if exists */}
|
|
||||||
<SearchFilter
|
<SearchFilter
|
||||||
searchQuery={searchQuery}
|
searchQuery={searchQuery}
|
||||||
setSearchQuery={setSearchQuery}
|
setSearchQuery={setSearchQuery}
|
||||||
|
@ -166,4 +163,5 @@ if (sortBy === 'name') {
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Home;
|
export default Home;
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
|
// src/UserCard.js
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
const UserCard = ({ user }) => {
|
const UserCard = ({ user }) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
console.log('usersss', user);
|
console.log('usersss', user)
|
||||||
|
|
||||||
const onClickHandler = () => {
|
const onClickHandler = () => {
|
||||||
// Navigate to UserDetails with username in the URL
|
navigate(`/user-details`, { state: user});
|
||||||
navigate(`/user-details/${user.login.username}`, { state: user });
|
}
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="user-card" key={user.email}>
|
<div className="user-card" key={user.email}>
|
||||||
|
@ -16,7 +16,8 @@ const UserCard = ({ user }) => {
|
||||||
<div className="user-info">
|
<div className="user-info">
|
||||||
<h3>{user.name.first} {user.name.last}</h3>
|
<h3>{user.name.first} {user.name.last}</h3>
|
||||||
<p>{user.email}</p>
|
<p>{user.email}</p>
|
||||||
<p className='view-detail' onClick={onClickHandler}>
|
{/* Link to the UserDetails page, passing the user data via state */}
|
||||||
|
<p className='view-detail' onClick={() => onClickHandler()}>
|
||||||
View Details
|
View Details
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,43 +1,50 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useLocation, Link } from 'react-router-dom';
|
import { useLocation, Link } from 'react-router-dom'; // useLocation to access state passed through the router
|
||||||
import './App.css';
|
import './App.css'; // Import the CSS for styling
|
||||||
|
|
||||||
const UserDetails = () => {
|
const UserDetails = () => {
|
||||||
|
// Use useLocation to access the user data passed from UserCard
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
if (!location.state) {
|
if (!location.state) {
|
||||||
return <p>No user data available!</p>;
|
return <p>No user data available!</p>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = location.state;
|
const user = location.state;
|
||||||
const userLocation = user.location || {};
|
|
||||||
const userName = user.name || {};
|
|
||||||
|
|
||||||
|
// Function to export user data to CSV
|
||||||
const exportToCSV = () => {
|
const exportToCSV = () => {
|
||||||
|
// Create a CSV string with the user details
|
||||||
const userData = [
|
const userData = [
|
||||||
['First Name', 'Last Name', 'Email', 'Phone', 'Cell', 'Address', 'Date of Birth', 'Nationality', 'Timezone', 'Coordinates', 'Username'],
|
['First Name', 'Last Name', 'Email', 'Phone', 'Cell', 'Address', 'Date of Birth', 'Nationality', 'Timezone', 'Coordinates', 'Username'],
|
||||||
[
|
[
|
||||||
userName.first || 'N/A',
|
user.name.first,
|
||||||
userName.last || 'N/A',
|
user.name.last,
|
||||||
user.email || 'N/A',
|
user.email,
|
||||||
user.phone || 'N/A',
|
user.phone,
|
||||||
user.cell || 'N/A',
|
user.cell,
|
||||||
`${userLocation.street?.number || ''} ${userLocation.street?.name || ''}, ${userLocation.city || 'N/A'}, ${userLocation.state || 'N/A'}, ${userLocation.country || 'N/A'} - ${userLocation.postcode || 'N/A'}`,
|
`${user.location.street.number} ${user.location.street.name}, ${user.location.city}, ${user.location.state}, ${user.location.country} - ${user.location.postcode}`,
|
||||||
new Date(user.dob?.date).toLocaleDateString('en-US') || 'N/A',
|
new Date(user.dob.date).toLocaleDateString(),
|
||||||
user.nat || 'N/A',
|
user.nat,
|
||||||
userLocation.timezone?.description || 'N/A',
|
user.location.timezone.description,
|
||||||
`${userLocation.coordinates?.latitude || 'N/A'}, ${userLocation.coordinates?.longitude || 'N/A'}`,
|
`${user.location.coordinates.latitude}, ${user.location.coordinates.longitude}`,
|
||||||
user.login?.username || 'N/A'
|
user.login.username
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Convert to CSV format
|
||||||
const csvContent = userData.map(row => row.join(',')).join('\n');
|
const csvContent = userData.map(row => row.join(',')).join('\n');
|
||||||
|
|
||||||
|
// Create a Blob from the CSV data
|
||||||
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
||||||
|
|
||||||
|
// Create a link to download the CSV file
|
||||||
const link = document.createElement('a');
|
const link = document.createElement('a');
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
link.setAttribute('href', url);
|
link.setAttribute('href', url);
|
||||||
link.setAttribute('download', `${userName.first || 'User'}_${userName.last || 'Details'}.csv`);
|
link.setAttribute('download', `${user.name.first}_${user.name.last}_details.csv`);
|
||||||
link.style.visibility = 'hidden';
|
link.style.visibility = 'hidden';
|
||||||
|
|
||||||
|
// Append the link to the document body and trigger the download
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
document.body.removeChild(link);
|
document.body.removeChild(link);
|
||||||
|
@ -45,26 +52,30 @@ const UserDetails = () => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="user-details">
|
<div className="user-details">
|
||||||
<h2>{userName.first || 'Unknown'} {userName.last || 'User'}</h2>
|
<h2>{user.name.first} {user.name.last}</h2>
|
||||||
<img src={user.picture?.large} alt={userName.first || 'User'} className="user-image" />
|
<img src={user.picture.large} alt={user.name.first} className="user-image" />
|
||||||
|
|
||||||
|
{/* Display user details */}
|
||||||
<div className="user-info">
|
<div className="user-info">
|
||||||
<div><strong>Email:</strong> {user.email || 'N/A'}</div>
|
<div><strong>Email:</strong> {user.email}</div>
|
||||||
<div><strong>Phone:</strong> {user.phone || 'N/A'}</div>
|
<div><strong>Phone:</strong> {user.phone}</div>
|
||||||
<div><strong>Cell:</strong> {user.cell || 'N/A'}</div>
|
<div><strong>Cell:</strong> {user.cell}</div>
|
||||||
<div>
|
<div>
|
||||||
<strong>Location:</strong> {`${userLocation.street?.number || ''} ${userLocation.street?.name || ''}, ${userLocation.city || 'N/A'}, ${userLocation.state || 'N/A'}, ${userLocation.country || 'N/A'} - ${userLocation.postcode || 'N/A'}`}
|
<strong>Location:</strong> {user.location.street.number} {user.location.street.name}, {user.location.city}, {user.location.state}, {user.location.country} - {user.location.postcode}
|
||||||
</div>
|
</div>
|
||||||
<div><strong>Date of Birth:</strong> {new Date(user.dob?.date).toLocaleDateString('en-US') || 'N/A'}</div>
|
<div><strong>Date of Birth:</strong> {new Date(user.dob.date).toLocaleDateString()}</div>
|
||||||
<div><strong>Nationality:</strong> {user.nat || 'N/A'}</div>
|
<div><strong>Nationality:</strong> {user.nat}</div>
|
||||||
<div><strong>Timezone:</strong> {userLocation.timezone?.description || 'N/A'}</div>
|
<div><strong>Timezone:</strong> {user.location.timezone.description}</div>
|
||||||
<div>
|
<div>
|
||||||
<strong>Coordinates:</strong> {`${userLocation.coordinates?.latitude || 'N/A'}, ${userLocation.coordinates?.longitude || 'N/A'}`}
|
<strong>Coordinates:</strong> {user.location.coordinates.latitude}, {user.location.coordinates.longitude}
|
||||||
</div>
|
</div>
|
||||||
<div><strong>Username:</strong> {user.login?.username || 'N/A'}</div>
|
<div><strong>Username:</strong> {user.login.username}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Link to go back to the user list */}
|
||||||
<Link to="/home" className="back-btn">Back to User List</Link>
|
<Link to="/home" className="back-btn">Back to User List</Link>
|
||||||
|
|
||||||
|
{/* Export Button to download user data as CSV */}
|
||||||
<button onClick={exportToCSV} className="export-btn">Export to CSV</button>
|
<button onClick={exportToCSV} className="export-btn">Export to CSV</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue