move to public repo
This commit is contained in:
29
client/src/context/ContextProvider.tsx
Normal file
29
client/src/context/ContextProvider.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
import { PropsWithChildren, useEffect, useState } from "react";
|
||||
import type { User } from "@types";
|
||||
|
||||
import { SocketContext, socket } from "./socket";
|
||||
import { SessionContext } from "./session";
|
||||
|
||||
import { fetchSession } from "../utils/auth";
|
||||
|
||||
const ContextProvider = (props: PropsWithChildren) => {
|
||||
const [user, setUser] = useState<User>({});
|
||||
|
||||
async function getSession() {
|
||||
const user = await fetchSession();
|
||||
if (user) {
|
||||
setUser(user);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
getSession();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<SocketContext.Provider value={socket}>
|
||||
<SessionContext.Provider value={{ user, setUser }}>{props.children}</SessionContext.Provider>
|
||||
</SocketContext.Provider>
|
||||
);
|
||||
};
|
||||
export default ContextProvider;
|
||||
7
client/src/context/session.ts
Normal file
7
client/src/context/session.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { User } from "@types";
|
||||
import { createContext, Dispatch, SetStateAction } from "react";
|
||||
|
||||
export const SessionContext = createContext<{
|
||||
user: User;
|
||||
setUser: Dispatch<SetStateAction<User>>;
|
||||
} | null>(null);
|
||||
18
client/src/context/socket.ts
Normal file
18
client/src/context/socket.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { createContext } from "react";
|
||||
import { io, Socket } from "socket.io-client";
|
||||
import { apiUrl } from "../config/config";
|
||||
|
||||
export const socket: Socket = io(apiUrl, {
|
||||
withCredentials: true,
|
||||
autoConnect: false
|
||||
});
|
||||
|
||||
socket.on("connect", () => {
|
||||
console.log("socket connected");
|
||||
});
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log("socket disconnected");
|
||||
});
|
||||
|
||||
export const SocketContext = createContext<Socket | null>(null);
|
||||
Reference in New Issue
Block a user