Prvotní nástřel fungující aplikace

This commit is contained in:
Martin Berka
2023-06-01 23:05:51 +02:00
parent bf379e13ed
commit 12583e6efb
59 changed files with 2194 additions and 1011 deletions

View File

@@ -0,0 +1,58 @@
import React, { ReactNode, useContext, useState } from "react"
import { useEffect } from "react"
const LOGIN_KEY = 'login';
export type AuthContextProps = {
login?: string,
setLogin: (name: string) => void,
clearLogin: () => void,
}
type ContextProps = {
children: ReactNode
}
const authContext = React.createContext<AuthContextProps | null>(null);
export function ProvideAuth(props: ContextProps) {
const auth = useProvideAuth();
return <authContext.Provider value={auth}>{props.children}</authContext.Provider>
}
export const useAuth = () => {
return useContext(authContext);
}
function useProvideAuth(): AuthContextProps {
const [loginName, setLoginName] = useState<string | undefined>();
useEffect(() => {
const login = localStorage.getItem(LOGIN_KEY);
if (login) {
setLogin(login);
}
}, [])
useEffect(() => {
if (loginName) {
localStorage.setItem(LOGIN_KEY, loginName)
} else {
localStorage.removeItem(LOGIN_KEY);
}
}, [loginName]);
function setLogin(login: string) {
setLoginName(login);
}
function clearLogin() {
setLoginName(undefined);
}
return {
login: loginName,
setLogin,
clearLogin
}
}

View File

@@ -0,0 +1,17 @@
import React from 'react';
import socketio from "socket.io-client";
import { getBaseUrl } from "../Utils";
// Záměrně omezeno jen na websocket, aby se případně odhalilo chybné nastavení proxy serveru
export const socket = socketio.connect(getBaseUrl(), { transports: ["websocket"] });
export const SocketContext = React.createContext();
// Konstanty websocket eventů, musí odpovídat těm na serveru!
export const EVENT_CONNECT = 'connect';
export const EVENT_DISCONNECT = 'disconnect';
export const EVENT_MESSAGE = 'message';
// export const EVENT_CONFIG = 'config';
// export const EVENT_TOASTER = 'toaster';
// export const EVENT_VOTING = 'voting';
// export const EVENT_VOTE_CONFIG = 'voteSettings';
// export const EVENT_ADMIN = 'admin';