Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Context and User Type created and wrapped chat screen #131

Merged
merged 6 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions client/src/app/(home)/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import { Stack } from "expo-router";
import { SettingsProvider } from "../../contexts/SettingsContext";
import { SocketProvider } from "../../contexts/SocketContext";
import { LocationProvider } from "../../contexts/LocationContext";
import { UserProvider } from "../../contexts/UserContext";

const AuthLayout = () => {
return (
<LocationProvider>
<SocketProvider>
<SettingsProvider>
<Stack
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="chatchannel" options={{}} />
</Stack>
</SettingsProvider>
<UserProvider>
<SettingsProvider>
<Stack
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="chatchannel" options={{}} />
</Stack>
</SettingsProvider>
</UserProvider>
</SocketProvider>
</LocationProvider>
);
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/Chat/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import {
import { ChatInput } from "../Common/CustomInputs";
import { ChatSendButton } from "../Common/CustomButtons";
import MessageChannel from "../Common/MessageChannel";
import { LinearGradient } from "expo-linear-gradient";
import { MessageType } from "../../utils/types";
import * as Crypto from "expo-crypto";
import { generateName } from "../../utils/scripts";
import { useSettings } from "../../contexts/SettingsContext";
import { SignOutButton } from "../Common/AuthButtons"
import { useUser } from "../../contexts/UserContext"

const ChatScreen = () => {
const settings = useSettings();
Expand All @@ -31,13 +31,15 @@ const ChatScreen = () => {
const [messages, setMessages] = React.useState<MessageType[]>([]);
const [message, setMessage] = React.useState<string>("");

const user = useUser();

// For when the user sends a message (fired by the send button)
const onHandleSubmit = () => {
if (message.trim() !== "") {
const newMessage: MessageType = {
msgID: Crypto.randomUUID(),
messageContent: message.trim(),
author: generateName(),
author: user.displayName,
};

setMessages([...messages, newMessage]);
Expand Down
1 change: 0 additions & 1 deletion client/src/contexts/SocketContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { createContext, useContext, useEffect, useState } from "react";
import { io, Socket } from "socket.io-client";
import * as Network from "expo-network";
import { useLocation } from "./LocationContext";
import { EXPO_IP } from "@env";

Expand Down
38 changes: 38 additions & 0 deletions client/src/contexts/UserContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { createContext, useContext } from 'react';
import { UserType } from '../utils/types';
import { useState, useEffect } from 'react';
import { generateName } from '../utils/scripts';

const UserContext = createContext<UserType>({
userID: "something",
displayName: "default",
pfp: "null"
});

const [user, setUser] = useState<UserType>({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you separate these into 3 different states?

userID: "something",
displayName: "default",
pfp: "null"
});

useEffect(() => {
user.displayName = generateName();
}, []);

export const useUser = () => {
return useContext(UserContext);
}

/*
export const changeUsername = (newUsername: string) => {
setUser
}
*/

AlexanderWangY marked this conversation as resolved.
Show resolved Hide resolved
export const UserProvider = ({ children }: {children: React.ReactNode}) => {
return (
<UserContext.Provider value={user}>
{children}
</UserContext.Provider>
);
};
6 changes: 6 additions & 0 deletions client/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ export type MessageType = {
messageContent: string;
author: string;
msgID: string;
};

export type UserType = {
userID: string;
displayName: string;
pfp: string;
AlexanderWangY marked this conversation as resolved.
Show resolved Hide resolved
};