Prvotní nástřel fungující aplikace
This commit is contained in:
58
client/src/context/auth.tsx
Normal file
58
client/src/context/auth.tsx
Normal 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
|
||||
}
|
||||
}
|
||||
17
client/src/context/socket.js
Normal file
17
client/src/context/socket.js
Normal 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';
|
||||
Reference in New Issue
Block a user