aboutsummaryrefslogtreecommitdiff
path: root/src/App.tsx
blob: 3b1b57165f42666fbc40b97f12b8b88aeb123c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { useContext, useEffect, useState } from "react";
import Chat from "./Chat/Chat";
import "./App.css";
import { LangType, Message } from "./type/messageTypes";
import { MessageDisplay } from "./MessageDisplay/MessageDisplay";
import strings from "./Intl/strings.json";
import { LangContext, LoginContext, LoginType } from "./context";
import { contentTypes, domain, endpoints, port } from "./consts";
import { Login } from "./Login/Login";
import { Sidebar } from "./Sidebar/Sidebar";
import { Topbar } from "./Topbar/Topbar";
// what we "in the business" call type gymnastics
const Wrapper = (): React.ReactElement => {
	const [lang, setLang] = useState<LangType>("en_US");
	const [login, setLogin] = useState<LoginType | undefined>(undefined);
	useEffect(() => {
		document.title = login
			? `IRC logged in as ${login.username}`
			: "IRC Chat";
	}, [login]);
	const [sidebarEnabled, setSidebarEnabled] = useState(false);
	return (
		<LangContext.Provider value={lang}>
			<LoginContext.Provider value={login}>
				<Topbar
					setSidebarEnable={(enabled: boolean) =>
						setSidebarEnabled(enabled)
					}
				></Topbar>
				<Sidebar
					isEnabled={sidebarEnabled}
					setEnable={(enabled: boolean) => setSidebarEnabled(enabled)}
				></Sidebar>
				{/* callbacks for altering the Lang/Login contexts */}
				<Login
					setLogin={(value) => {
						setLogin(value);
					}}
				></Login>
				{login ? (
					<App
						changeLang={(value: string) => {
							setLang(value as LangType);
						}}
					/>
				) : (
					<></>
				)}
			</LoginContext.Provider>
		</LangContext.Provider>
	);
};
const setNameOnServer = async (name: string) => {
	const responseRaw = await fetch(
		`https://${domain}:${port}${endpoints.user}`,
		{
			method: "POST",
			mode: "cors",
			headers: contentTypes.json,
			body: JSON.stringify({
				userName: name,
				dateJoined: Date.now(),
			}),
		}
	);
	if (responseRaw.status === 400) {
		return { success: false, reason: "Username taken or invalid!" };
	} else return { success: true, reason: "" };
};
const validateName = (name: string): boolean => {
	// TODO Name validation
	return !(name === null || name === undefined || name === "");
};

const App = ({
	changeLang,
}: {
	changeLang: (value: string) => void;
}): React.ReactElement => {
	const [messages, setMessages] = useState<Message[]>([]);
	const login = useContext(LoginContext);
	const lang = useContext(LangContext);
	const home = strings[lang].homepage;
	if (!login) {
		return <></>;
	} else
		return (
			<div className="App">
				<button
					onClick={(ev) => {
						const selection = prompt(home.newLangPrompt);
						changeLang(selection ? (selection as LangType) : lang);
					}}
				>
					{home.switchLang}
				</button>
				<button
					onClick={(ev) => {
						// For passing new username to the backend
						// In the future, this could be done with the async/await JS/TS syntax
						const newUsername = prompt("New username: ");
						fetch(`${endpoints.user}?name=${newUsername}`, {
							method: "POST",
						})
							.then((response) => {
								return response.json();
							})
							.then((responseBody: { success: boolean }) => {
								if (responseBody.success) {
									// TODO Put new username response true handler method stub
								} else {
									console.error(
										"Server POST message failed."
									);
									alert(
										"The server encountered an internal error."
									);
								}
							});
					}}
				>
					Change Username
				</button>
				{messages.map((message) => {
					return <MessageDisplay {...message} />;
				})}
				{<Chat user={login.username as string} />}
			</div>
		);
};
export default Wrapper;