feat: bulk create user

This commit is contained in:
Malopieds 2024-10-14 21:50:03 +02:00
parent f61d0a111a
commit 8a48d3b6c6
3 changed files with 263 additions and 152 deletions

View File

@ -9,35 +9,38 @@ import Practical from "./pages/Practical";
import LoginPage from "./pages/Login";
import PageTest from "./pages/PageTest";
import CreateTp from "./pages/admin/CreateTp";
import BulkUsers from "./pages/admin/BulkCreateUser";
function App() {
const [showNotif, setShowNotif] = useState(false);
return (
<BrowserRouter>
<Navigation>
<Routes>
<Route path="/" element={<Dashboard />} />
<Route path="/tps" element={<Practicals />} />
<Route path="/tps/:id" element={<Practical />} />
<Route path="/instances" element={<Instances />} />
<Route path="/profile" element={<PageTest />} />
<Route path="/login" element={<LoginPage />} />
<Route path="/admin/tps" element={<CreateTp />} />
</Routes>
</Navigation>
const [showNotif, setShowNotif] = useState(false);
return (
<BrowserRouter>
<Navigation>
<Routes>
<Route path="/" element={<Dashboard />} />
<Route path="/tps" element={<Practicals />} />
<Route path="/tps/:id" element={<Practical />} />
<Route path="/instances" element={<Instances />} />
<Route path="/profile" element={<PageTest />} />
<Route path="/login" element={<LoginPage />} />
<Route path="/admin" element={<BulkUsers />} />
<Route path="/admin/tps" element={<CreateTp />} />
<Route path="/settings" element={<CreateTp />} />
</Routes>
</Navigation>
{showNotif && (
<div className="toast toast-top toast-end">
<div className="alert alert-success">
<span>Message sent successfully.</span>
<button className="btn btn-sm" onClick={() => setShowNotif(false)}>
close
</button>
</div>
</div>
)}
</BrowserRouter>
);
{showNotif && (
<div className="toast toast-top toast-end">
<div className="alert alert-success">
<span>Message sent successfully.</span>
<button className="btn btn-sm" onClick={() => setShowNotif(false)}>
close
</button>
</div>
</div>
)}
</BrowserRouter>
);
}
export default App;

View File

@ -1,146 +1,154 @@
import React, { useEffect, useState } from "react";
import {
ChartPieIcon,
DocumentTextIcon,
EnvelopeIcon,
CommandLineIcon,
Bars3Icon,
SunIcon,
MoonIcon,
ChartPieIcon,
DocumentTextIcon,
EnvelopeIcon,
CommandLineIcon,
Bars3Icon,
SunIcon,
MoonIcon,
} from "@heroicons/react/24/outline";
import { Link } from "react-router-dom";
interface NavigationProps {
children: React.ReactElement;
children: React.ReactElement;
}
const Navigation: React.FC<NavigationProps> = ({
children,
children,
}: NavigationProps) => {
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
const toggleDrawer = () => setIsDrawerOpen(!isDrawerOpen);
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
const toggleDrawer = () => setIsDrawerOpen(!isDrawerOpen);
const [theme, setTheme] = useState(
localStorage.getItem("theme") ? localStorage.getItem("theme") : "dark",
);
const [theme, setTheme] = useState(
localStorage.getItem("theme") ? localStorage.getItem("theme") : "dark",
);
// update state on toggle
const handleToggle = (e) => {
if (theme === "light") {
setTheme("dark");
} else {
setTheme("light");
}
};
// update state on toggle
const handleToggle = (e) => {
if (theme === "light") {
setTheme("dark");
} else {
setTheme("light");
}
};
// set theme state in localstorage on mount & also update localstorage on state change
useEffect(() => {
localStorage.setItem("theme", theme);
const localTheme = localStorage.getItem("theme");
// add custom data-theme attribute to html tag required to update theme using DaisyUI
document.querySelector("html").setAttribute("data-theme", localTheme);
}, [theme]);
// set theme state in localstorage on mount & also update localstorage on state change
useEffect(() => {
localStorage.setItem("theme", theme);
const localTheme = localStorage.getItem("theme");
// add custom data-theme attribute to html tag required to update theme using DaisyUI
document.querySelector("html").setAttribute("data-theme", localTheme);
}, [theme]);
return (
<div className="app h-screen">
<div className="drawer">
<input
id="my-drawer"
type="checkbox"
className="drawer-toggle"
checked={isDrawerOpen}
/>
<div className="drawer-content h-full">
<div className="p-6 flex justify-between">
<div className="btn btn-ghost" onClick={toggleDrawer}>
<Bars3Icon className="size-6" />
</div>
<label className="swap swap-rotate">
<input type="checkbox" onChange={handleToggle} />
return (
<div className="app h-screen">
<div className="drawer">
<input
id="my-drawer"
type="checkbox"
className="drawer-toggle"
checked={isDrawerOpen}
/>
<div className="drawer-content h-full">
<div className="p-6 flex justify-between">
<div className="btn btn-ghost" onClick={toggleDrawer}>
<Bars3Icon className="size-6" />
</div>
<label className="swap swap-rotate">
<input type="checkbox" onChange={handleToggle} />
<SunIcon className="size-6 swap-off" />
<MoonIcon className="size-6 swap-on" />
</label>
</div>
{children}
</div>
<div className="drawer-side">
<div className="drawer-overlay" onClick={toggleDrawer}></div>
<div className="h-full menu bg-base-200 text-base-content w-80">
<li>
<div className="dropdown dropdown-bottom h-full">
<div
tabIndex={0}
role="button"
className="flex avatar items-center space-x-4 w-64"
>
<div className="w-10 rounded-full">
<img
alt="Tailwind CSS Navbar component"
src="https://img.daisyui.com/images/stock/photo-1534528741775-53994a69daeb.webp"
/>
</div>
<a>Malopieds</a>
<SunIcon className="size-6 swap-off" />
<MoonIcon className="size-6 swap-on" />
</label>
</div>
{children}
</div>
<ul
tabIndex={0}
className="dropdown-content menu bg-base-100 rounded-box z-[1] w-72 shadow"
>
<li>
<Link to="/profile" onClick={toggleDrawer}>
Profile
</Link>
</li>
<li>
<Link to="/settings" onClick={toggleDrawer}>
Settings
</Link>
</li>
<div className="divider" onClick={toggleDrawer}></div>
<li>
<Link to="login" onClick={toggleDrawer}>
Log Out
</Link>
</li>
</ul>
</div>
</li>
<li>
<div>
<ChartPieIcon className="size-6" />
<Link to="/" onClick={toggleDrawer}>
Dashboard
</Link>
</div>
</li>
<li>
<div>
<DocumentTextIcon className="size-6" />
<Link to="/tps" onClick={toggleDrawer}>
TPs
</Link>
</div>
</li>
<li>
<div>
<CommandLineIcon className="size-6" />
<Link to="/instances" onClick={toggleDrawer}>
Instances
</Link>
</div>
</li>
<li>
<div onClick={toggleDrawer}>
<EnvelopeIcon className="size-6" />
<Link to="/messages">Messages</Link>
</div>
</li>
</div>
<div className="drawer-side">
<div className="drawer-overlay" onClick={toggleDrawer}></div>
<div className="h-full menu bg-base-200 text-base-content w-80">
<li>
<div className="dropdown dropdown-bottom h-full">
<div
tabIndex={0}
role="button"
className="flex avatar items-center space-x-4 w-64"
>
<div className="w-10 rounded-full">
<img
alt="Tailwind CSS Navbar component"
src="https://img.daisyui.com/images/stock/photo-1534528741775-53994a69daeb.webp"
/>
</div>
<a>Malopieds</a>
</div>
<ul
tabIndex={0}
className="dropdown-content menu bg-base-100 rounded-box z-[1] w-72 shadow"
>
<li>
<Link to="/profile" onClick={toggleDrawer}>
Profile
</Link>
</li>
<li>
<Link to="/settings" onClick={toggleDrawer}>
Settings
</Link>
</li>
<div className="divider" onClick={toggleDrawer}></div>
<li>
<Link to="login" onClick={toggleDrawer}>
Log Out
</Link>
</li>
</ul>
</div>
</li>
<li>
<div>
<ChartPieIcon className="size-6" />
<Link to="/admin" onClick={toggleDrawer}>
Admin
</Link>
</div>
</li>
<li>
<div>
<ChartPieIcon className="size-6" />
<Link to="/" onClick={toggleDrawer}>
Dashboard
</Link>
</div>
</li>
<li>
<div>
<DocumentTextIcon className="size-6" />
<Link to="/tps" onClick={toggleDrawer}>
TPs
</Link>
</div>
</li>
<li>
<div>
<CommandLineIcon className="size-6" />
<Link to="/instances" onClick={toggleDrawer}>
Instances
</Link>
</div>
</li>
<li>
<div onClick={toggleDrawer}>
<EnvelopeIcon className="size-6" />
<Link to="/messages">Messages</Link>
</div>
</li>
</div>
</div>
</div>
</div>
</div>
</div>
);
);
};
export default Navigation;

View File

@ -0,0 +1,100 @@
import axios from "axios";
import { useState } from "react";
function BulkUsers() {
const [file, setFile] = useState(null);
const [userData, setUserData] = useState([]);
const [practical, setPractical] = useState("0");
const handlePracticalChange = (e) => {
setPractical(e.target.value);
}
const handleFileChange = (event) => {
const selectedFile = event.target.files[0];
if (selectedFile && selectedFile.type === 'text/csv') {
setFile(selectedFile);
parseCSV(selectedFile);
} else {
alert('Please upload a valid CSV file.');
}
};
const parseCSV = (file) => {
const reader = new FileReader();
reader.onload = (event) => {
const text = event.target.result;
const rows = text.split('\n').map((row) => row.split(','));
const formattedData = rows.map((row) => ({
name: row[0],
email: row[1],
// Add other fields as necessary
}));
setUserData(formattedData);
};
reader.readAsText(file);
};
const handleSubmit = () => {
// Handle submission logic (e.g., sending data to the backend)
console.log('Submitting data:', userData.filter(user => user.name !== ""));
axios.post("/api/users/jdmi", userData.filter(user => user.name !== "")).then(res => console.log(res));
};
return (
<div className="max-w-md mx-auto p-5">
<h2 className="text-2xl font-bold mb-4">Bulk User Creation</h2>
<input
type="file"
accept=".csv"
onChange={handleFileChange}
className="file-input file-input-bordered w-full mb-4"
/>
{userData.length > 0 && (
<div className="overflow-x-auto">
<table className="table w-full">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
{/* Add other headers as necessary */}
</tr>
</thead>
<tbody>
{userData.map((user, index) => (
<tr key={index}>
<td>{user.name}</td>
<td>{user.email}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
<div>
<label className="block text-gray-700 font-semibold mb-2">
Tp:
</label>
<select
name="practical"
value={practical}
onChange={handlePracticalChange}
className="w-full px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="0">Empty</option>
<option value="1">Bot Discord</option>
</select>
</div>
<button
onClick={handleSubmit}
className="btn btn-primary mt-4"
disabled={userData.length === 0}
>
Create Users
</button>
</div>
);
}
export default BulkUsers;