From d46407cda93c0bedf58efda734a3774125e138bf Mon Sep 17 00:00:00 2001 From: intisarosman1 <77607212+intisarosman1@users.noreply.github.com> Date: Wed, 3 Jul 2024 21:05:20 -0700 Subject: [PATCH] nsc-events-nextjs_9_493_change-user-details (#497) * Separate first name and last name fields * Add edit user details dialog * Added functionality * Small changes to UI --- app/profile/page.tsx | 26 ++++-- components/CurrentUserCard.tsx | 7 +- components/EditUserDetailsDialog.tsx | 116 +++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 components/EditUserDetailsDialog.tsx diff --git a/app/profile/page.tsx b/app/profile/page.tsx index 5dcbab46..8604c388 100644 --- a/app/profile/page.tsx +++ b/app/profile/page.tsx @@ -5,17 +5,14 @@ import { Box, Button, Stack, Typography } from "@mui/material"; import React, { useEffect, useState } from "react"; import CurrentUserCard from "@/components/CurrentUserCard"; import { useRouter } from "next/navigation"; +import EditUserDetailsDialog, { User } from "@/components/EditUserDetailsDialog"; -interface User { - firstName: string; - lastName: string; - email: string; - pronouns: string -} const URL = process.env.NSC_EVENTS_PUBLIC_API_URL || "http://localhost:3000/api"; + const Profile = () => { const [user, setUser] = useState(); const [token, setToken] = useState(); + const [openEditDialog, setOpenEditDialog] = useState(false); const router = useRouter(); const getUserFromId = async (userId: string) => { const response = await fetch(`${URL}/users/find/${userId}`); @@ -29,6 +26,18 @@ const Profile = () => { const userId = getCurrentUserId(); getUserFromId(userId); },[]); + + const handleEditClick = () => { + setOpenEditDialog(true); + }; + + const handleCloseEditDialog = (updatedUser?: User) => { + setOpenEditDialog(false); + if (updatedUser) { + setUser(updatedUser); + } + }; + if (token === null) { return ( @@ -45,6 +54,7 @@ const Profile = () => { ) } + return ( @@ -52,9 +62,13 @@ const Profile = () => { Welcome, { user?.firstName } + + {openEditDialog && ( + + )} ); diff --git a/components/CurrentUserCard.tsx b/components/CurrentUserCard.tsx index 1577e12a..a9a2a979 100644 --- a/components/CurrentUserCard.tsx +++ b/components/CurrentUserCard.tsx @@ -14,7 +14,7 @@ function CurrentUserCard({ user }: { user: CurrentUserCardProps }) { return ( - Full Name: { user.firstName } { user.lastName } + First Name: { user.firstName } + + + Last Name: { user.lastName } Email: { user.email } diff --git a/components/EditUserDetailsDialog.tsx b/components/EditUserDetailsDialog.tsx new file mode 100644 index 00000000..6ed8c9bd --- /dev/null +++ b/components/EditUserDetailsDialog.tsx @@ -0,0 +1,116 @@ +import React, { useState, useEffect } from "react"; +import { Dialog, DialogTitle, DialogContent, DialogActions, Button, TextField, Box } from "@mui/material"; +import Snackbar from '@mui/material/Snackbar'; +import Alert, { AlertColor } from '@mui/material/Alert'; + +interface EditUserDetailsDialogProps { + open: boolean; + onClose: (updatedUser?: User) => void; + user: User; +} + +export interface User { + id: string; + firstName: string; + lastName: string; + email: string; + pronouns: string; +} + +const EditUserDetailsDialog: React.FC = ({ open, onClose, user }) => { + const [firstName, setFirstName] = useState(user.firstName); + const [lastName, setLastName] = useState(user.lastName); + const [pronouns, setPronouns] = useState(user.pronouns); + const [isUpdated, setIsUpdated] = useState(false); + const [snackbarOpen, setSnackbarOpen] = useState(false); + const [snackbarMessage, setSnackbarMessage] = useState(''); + const [snackbarSeverity, setSnackbarSeverity] = useState('success'); + + useEffect(() => { + setFirstName(user.firstName); + setLastName(user.lastName); + setPronouns(user.pronouns); + setIsUpdated(false); + }, [user]); + + const handleInputChange = (setter: React.Dispatch>, value: string, initialValue: string) => { + setter(value); + if (value !== initialValue) { + setIsUpdated(true); + } else { + setIsUpdated(false); + } + }; + + const handleSave = async () => { + const token = localStorage.getItem('token'); + try { + const apiUrl = process.env.NSC_EVENTS_PUBLIC_API_URL || `http://localhost:3000/api`; + const response = await fetch(`${apiUrl}/users/update/${user.id}`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + }, + body: JSON.stringify({ firstName, lastName, pronouns }) + }); + console.log(response.body) + if (response.ok) { + const updatedUser = await response.json(); + setTimeout(() => onClose(updatedUser), 1000); + setSnackbarSeverity('success'); + setSnackbarMessage('Profile updated successfully!'); + setSnackbarOpen(true); + } else { + console.error('Failed to update profile:', response.statusText); + setSnackbarSeverity('error'); + setSnackbarMessage('Failed to update profile'); + setSnackbarOpen(true); + } + } catch (error) { + console.error('Error updating profile:', error); + setSnackbarSeverity('error'); + setSnackbarMessage('Error updating profile'); + } + setSnackbarOpen(true); + }; + + return ( + <> + onClose()}> + Edit Profile + + + handleInputChange(setFirstName, e.target.value, user.firstName)} + /> + handleInputChange(setLastName, e.target.value, user.lastName)} + /> + handleInputChange(setPronouns, e.target.value, user.pronouns)} + /> + + + + + + + + setSnackbarOpen(false)} + anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}> + setSnackbarOpen(false)} severity={snackbarSeverity} sx={{ width: '100%' }}> + {snackbarMessage} + + + + ); +}; + +export default EditUserDetailsDialog; \ No newline at end of file