From ed221704cfd589644be2afa85f46426a32fb5ac3 Mon Sep 17 00:00:00 2001 From: evan-scales Date: Sun, 21 Jan 2024 17:05:44 -0500 Subject: [PATCH] userPage: Created user page --- FU.API/FU.API/Hubs/ChatHub.cs | 27 ++-- FU.SPA/src/components/pages/UserProfile.css | 47 +++++++ FU.SPA/src/components/pages/UserProfile.jsx | 132 ++++++++++++++++++-- 3 files changed, 182 insertions(+), 24 deletions(-) diff --git a/FU.API/FU.API/Hubs/ChatHub.cs b/FU.API/FU.API/Hubs/ChatHub.cs index 4b187c08..a2f6d22f 100644 --- a/FU.API/FU.API/Hubs/ChatHub.cs +++ b/FU.API/FU.API/Hubs/ChatHub.cs @@ -16,7 +16,7 @@ public class ChatHub : Hub /// /// List of the connected users names. /// - public static readonly List ConnectedUsers = new (); + private static List connectedUsers; /// /// The app db context. @@ -30,33 +30,34 @@ public class ChatHub : Hub public ChatHub(AppDbContext context) { _context = context; + connectedUsers = new List(); } /// /// Connect the user to the hub. /// /// Task. - public override Task OnConnectedAsync() + public async override Task OnConnectedAsync() { var userId = UserId; var user = _context.Users.Find(userId); if (user is null || user.Username is null) { - return Task.CompletedTask; + return; } - if (!ConnectedUsers.Contains(user.Username)) + if (!connectedUsers.Contains(user.Username)) { - ConnectedUsers.Add(user.Username); + connectedUsers.Add(user.Username); } // Update the online status of the user user.IsOnline = true; _context.Users.Update(user); - _context.SaveChanges(); + await _context.SaveChangesAsync(); - return base.OnConnectedAsync(); + await base.OnConnectedAsync(); } /// @@ -64,27 +65,27 @@ public override Task OnConnectedAsync() /// /// Exception. /// Task. - public override Task OnDisconnectedAsync(Exception? exception) + public override async Task OnDisconnectedAsync(Exception? exception) { var userId = UserId; var user = _context.Users.Find(userId); if (user is null || user.Username is null) { - return Task.CompletedTask; + return; } - if (ConnectedUsers.Contains(user.Username)) + if (connectedUsers.Contains(user.Username)) { - ConnectedUsers.Remove(user.Username); + connectedUsers.Remove(user.Username); } // Update the online status of the user user.IsOnline = false; _context.Users.Update(user); - _context.SaveChanges(); + await _context.SaveChangesAsync(); - return base.OnDisconnectedAsync(exception); + base.OnDisconnectedAsync(exception); } /// diff --git a/FU.SPA/src/components/pages/UserProfile.css b/FU.SPA/src/components/pages/UserProfile.css index a1ab3f30..8e5f50a9 100644 --- a/FU.SPA/src/components/pages/UserProfile.css +++ b/FU.SPA/src/components/pages/UserProfile.css @@ -3,6 +3,8 @@ height: 155px; display: flex; align-items: center; + justify-content: space-between; + flex-direction: row; .userImage { width: 100px; @@ -32,7 +34,52 @@ font-weight: 700; } } + + .buttons { + display: flex; + flex-direction: column; + margin-right: 75px; + height: 100px; + vertical-align: middle; + + button { + width: 233px; + background-color: #E340DCDC; + color: #fff; + padding: 15px; + border: none; + margin: 5px; + /* height: 55px; */ + } + + button:hover { + background-color: #E340DC; + } + } } .body { + display: flex; + flex: row; + + .bio { + margin-top: 20px; + background-color: #31084a; + color: #fff; + display: flex; + flex-direction: column; + align-items: flex-start; + min-width: 300px; + padding-left: 20px; + padding-bottom: 20px; + + h1 { + margin: 0%; + } + + p { + margin: 0%; + padding-top: 10px; + } + } } diff --git a/FU.SPA/src/components/pages/UserProfile.jsx b/FU.SPA/src/components/pages/UserProfile.jsx index 44f7110c..5f1cbedd 100644 --- a/FU.SPA/src/components/pages/UserProfile.jsx +++ b/FU.SPA/src/components/pages/UserProfile.jsx @@ -4,30 +4,140 @@ import UserContext from '../../context/userContext'; import UserService from '../../services/userService'; import NoPage from './NoPage'; import './UserProfile.css'; +import { getDirectChat } from '../../services/chatService'; +import { useNavigate } from 'react-router-dom'; export default function UserProfile() { + const navigate = useNavigate(); const { username } = useParams(); const { user } = useContext(UserContext); const [userProfile, setUserProfile] = useState(null); + const [defaultPFP, setDefaultPFP] = useState(false); + + const update = useCallback(async () => { + try { + const profile = await UserService.getUserprofile(username); + setUserProfile(profile); + setDefaultPFP(profile.pfpUrl.includes('https://tr.rbxcdn.com/38c6edcb50633730ff4cf39ac8859840/420/420/Hat/Png')); + } catch (error) { + console.error('Error fetching profile:', error); + } + }, [username]); + + useEffect(() => { + update(); + }, [username, update]); + + // convert dob to years old + const dob = new Date(userProfile?.dob); + const today = new Date(); + const age = Math.floor( + (today.getTime() - dob.getTime()) / (1000 * 3600 * 24 * 365) + ); + + const stringToColor = (string) => { + let hash = 0; + let i; + for (i = 0; i < string.length; i += 1) { + hash = string.charCodeAt(i) + ((hash << 5) - hash); + } + let color = '#'; + for (i = 0; i < 3; i += 1) { + const value = (hash >> (i * 8)) & 0xff; + color += `00${value.toString(16)}`.slice(-2); + } + return color; + } + + const initials = (name) => { + // split name by spaces and filter empty entries + let nameParts = name.split(' ').filter(Boolean); + // get the first letter of the first part + let initials = nameParts[0][0]; + // if there is a second part to name + if (nameParts.length > 1) { + initials += nameParts[1][0]; + } + + return initials; + } + + const renderPfp = () => { + return !defaultPFP ? ( + + ) : ( +
+ {initials(userProfile.username)} +
+ ); + } + + const renderBio = () => { + if (!userProfile.bio) { + return; + } - if (user) { + return ( +
+

About Me

+

{userProfile.bio}

+
+ ); + }; + + const renderHeaderButtons = () => { + if (!user || user.username === userProfile.username) { + return; + } + + return ( +
+ + +
+ ); + }; + + const clickSendMessage = async () => { + const chat = await getDirectChat(userProfile.id); + navigate(`/chat/${chat.id}`); + } + + if (userProfile) { return ( <>
- -
-

{username}

-

Online Status

-

Age: 17

+
+ {renderPfp()} +
+

{userProfile.username}

+ {userProfile.dob &&

Age: {age} years old

} +
+
+ {renderHeaderButtons()} +
+
+
+ {renderBio()}
-
); } else { return ; } -} +} \ No newline at end of file