From f3e075b2938a8bee9a47f2d944db4828201fd148 Mon Sep 17 00:00:00 2001 From: Tin Pham <72054441+tinpham5614@users.noreply.github.com> Date: Thu, 22 Feb 2024 19:32:51 -0800 Subject: [PATCH] belinda-nextjs-03-228-add-edit-user-role-page (#229) * add edit user role page * update code formatter --- app/edit-user-role-page/page.tsx | 83 ++++++++++++++ components/EditUserRoleDialog.tsx | 174 ++++++++++++++++++++++++++++++ components/UserCard.tsx | 75 +++++++++++++ 3 files changed, 332 insertions(+) create mode 100644 app/edit-user-role-page/page.tsx create mode 100644 components/EditUserRoleDialog.tsx create mode 100644 components/UserCard.tsx diff --git a/app/edit-user-role-page/page.tsx b/app/edit-user-role-page/page.tsx new file mode 100644 index 00000000..b8dcef20 --- /dev/null +++ b/app/edit-user-role-page/page.tsx @@ -0,0 +1,83 @@ +"use client"; +import React, { useState, useEffect } from "react"; +import UserCard from "../../components/UserCard"; +import { Box, Container, Typography } from "@mui/material"; + +/** + * Represents a user. + */ +interface User { + id: string; + firstName: string; + lastName: string; + email: string; + role: string; +} +/** + * fetch user info from the server + * @param setUserInfo + */ +async function fetchUser(setUserInfo: (userInfo: User[]) => void) { + const apiUrl = "http://localhost:3000/api/user"; + try { + const res = await fetch(apiUrl, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + if (!res.ok) { + throw new Error(res.statusText); + } else { + const data = await res.json(); + setUserInfo(data); + console.log(data); + } + } catch (error) { + console.error("Error getting user info:", error); + } +} + +/** + * Edit user role page + * @returns + */ +const EditUserRolePage = () => { + const [userInfo, setUserInfo] = useState([]); + + useEffect(() => { + fetchUser(setUserInfo); + }, []); + + return ( + + + + User Management + + {userInfo.map((user, index) => ( + + ))} + + + ); +}; + +export default EditUserRolePage; diff --git a/components/EditUserRoleDialog.tsx b/components/EditUserRoleDialog.tsx new file mode 100644 index 00000000..5360f7e5 --- /dev/null +++ b/components/EditUserRoleDialog.tsx @@ -0,0 +1,174 @@ +import Box from "@mui/material/Box"; +import Button from "@mui/material/Button"; +import List from "@mui/material/List"; +import ListItemButton from "@mui/material/ListItemButton"; +import ListItemText from "@mui/material/ListItemText"; +import DialogTitle from "@mui/material/DialogTitle"; +import DialogContent from "@mui/material/DialogContent"; +import DialogActions from "@mui/material/DialogActions"; +import Dialog from "@mui/material/Dialog"; +import RadioGroup from "@mui/material/RadioGroup"; +import Radio from "@mui/material/Radio"; +import FormControlLabel from "@mui/material/FormControlLabel"; +import React, { useEffect, useRef, useState } from "react"; +import { UserCardProps } from "./UserCard"; + +const options = ["admin", "creator", "user"]; + +/** + * Props for the ConfirmationDialogRaw component. + */ +interface ConfirmationDialogRawProps { + id: string; + keepMounted: boolean; + value: string; + open: boolean; + onClose: (value?: string) => void; +} +/** + * Renders a confirmation dialog for editing user roles. + * + * @param props - The component props. + * @returns The rendered ConfirmationDialogRaw component. + */ +export function ConfirmationDialogRaw(props: ConfirmationDialogRawProps) { + const { onClose, value: valueProp, open, ...other } = props; + const [value, setValue] = useState(valueProp); + const radioGroupRef = useRef(null); + + useEffect(() => { + if (!open) { + setValue(valueProp); + } + }, [valueProp, open]); + + /** + * Handles the entering event of the dialog. + */ + const handleEntering = () => { + if (radioGroupRef.current != null) { + radioGroupRef.current.focus(); + } + }; + + /** + * Handles the cancel action. + */ + const handleCancel = () => { + onClose(); + }; + + /** + * Handles the click event when the user confirms the role update. + * @returns {void} + */ + const handleOk = () => { + onClose(value); + // TODO: Update user role in the database + }; + + /** + * Handles the change event of the input element. + * @param event - The change event object. + */ + const handleChange = (event: React.ChangeEvent) => { + setValue((event.target as HTMLInputElement).value); + }; + + return ( + + User Role + + + {options.map((option) => ( + } + label={option} + /> + ))} + + + + + + + + ); +} + +/** + * Renders a dialog for editing the user role. + * + * @component + * @param {Object} props - The component props. + * @param {UserCardProps} props.user - The user object containing user information. + * @param {Function} props.onClose - The function to be called when the dialog is closed. + * @returns {JSX.Element} The rendered EditUserRoleDialog component. + */ +export default function EditUserRoleDialog({ + user, +}: { + user: UserCardProps; + onClose: () => void; +}) { + const [open, setOpen] = useState(false); + const [value, setValue] = useState(user.role); + + const handleClickListItem = () => { + setOpen(true); + }; + + const handleClose = (newValue?: string) => { + setOpen(false); + + if (newValue) { + setValue(newValue); + } + }; + + return ( + + + + + + + + + ); +} diff --git a/components/UserCard.tsx b/components/UserCard.tsx new file mode 100644 index 00000000..cb010db7 --- /dev/null +++ b/components/UserCard.tsx @@ -0,0 +1,75 @@ +import React, { useState } from "react"; +import { Box, Button, Container, Stack, Typography } from "@mui/material"; +import EditIcon from "@mui/icons-material/Edit"; +import EditUserRoleDialog from "./EditUserRoleDialog"; + +export interface UserCardProps { + id: string; + firstName: string; + lastName: string; + email: string; + role: string; +} + +function UserCard({ user }: { user: UserCardProps }) { + const [openDialog, setOpenDialog] = useState(false); + + const handleEditClick = () => { + setOpenDialog(true); + }; + + const handleCloseDialog = () => { + setOpenDialog(false); + }; + return ( + + + + User ID: {user.id} + + + Full Name: {user.firstName} {user.lastName} + + + Email: {user.email} + + + Current Role: {user.role} + + {openDialog && ( + + + + )} + + + + + + ); +} +export default UserCard;