import { JSX } from "react"; import { getUsers, addUser, editUser, removeUser } from "../actions/UserAction"; import { hashPassword } from "../lib/auth"; export default async function UsersPage(): Promise { const users = await getUsers(); // fetches all users from database, returns array of records return (
{ // during build time, next generates this func on the server "use server"; /// when submit is pressed it sends formdata and func reference to server and next executes it const name = formData.get("name") as string; const email = formData.get("email") as string; const pass = formData.get("pass") as string; const hashedPass = await hashPassword(pass); if (email && pass) { // if all required fields present then add user const result = await addUser({ name, email, pass:hashedPass }); } }} className="grid grid-cols-1 md:grid-cols-4 gap-3 items-end mt-2">
{users.map((user) => ( // maps each record in user array to a table row, performs func on every obj/record where user refers to current obj ))} {users.length === 0 && ( // if length of array is 0 (no users in db) )}
ID Name Email Actions
{user.id} {user.name} {user.email} {/*
{ "use server"; await editUser(user.id, { name: `${user.name} (Edited)`, }); }} >
*/}
{ "use server"; await removeUser(user.id); }} >
No users found in database.
); }