nicer flow for disconnections, adding socket initialization and such

This commit is contained in:
talksik
2022-05-06 06:30:14 -05:00
parent 0f8902b47d
commit 0d8673d594
9 changed files with 269 additions and 137 deletions
+15 -1
View File
@@ -6,7 +6,20 @@ import { queryClient } from "../pages/nirvanaApp";
import { useRecoilValue } from "recoil";
// ====== QUERIES
export function useAuthCheck() {
/**
* ensure that the server is up
*/
export function useServerCheck() {
return useQuery("SERVER_CHECK", ApiCalls.serverCheck, {
retry: true,
refetchOnWindowFocus: false,
refetchInterval: 10000,
});
}
export function useAuthCheck(enabled: boolean = true) {
const jwtToken = useRecoilValue($jwtToken);
return useQuery("AUTH_CHECK", ApiCalls.authCheck, {
@@ -14,6 +27,7 @@ export function useAuthCheck() {
refetchOnWindowFocus: false,
refetchInterval: 10000,
enabled,
});
}
@@ -1,11 +1,13 @@
import React, { useContext } from "react";
import $ws from "./sockets";
import { $jwtToken } from "./recoil";
import MasterLineData from "@nirvana/core/models/masterLineData.model";
import SocketChannels from "@nirvana/core/sockets/channels";
import { User } from "@nirvana/core/models";
import { io } from "socket.io-client";
import toast from "react-hot-toast";
import { useEffect } from "react";
import { useRecoilValue } from "recoil";
import { useUserLines } from "./index";
interface ILineDataContext {
@@ -14,23 +16,50 @@ interface ILineDataContext {
relevantUsers: User[];
}
const lineDataContext = React.createContext<ILineDataContext>({
const LineDataContext = React.createContext<ILineDataContext>({
lines: [],
relevantUsers: [],
});
export function LineDataProvider() {
let $ws;
export function LineDataProvider({ children }) {
// persistent store of lines
const { data: basicUserLinesData } = useUserLines();
const jwtToken = useRecoilValue($jwtToken);
useEffect(() => {
$ws = io("http://localhost:5000", {
query: { token: jwtToken },
});
// client-side
$ws.on("connect_error", (err) => {
console.error(err.message); // prints the message associated with the error
toast.error("sorry...this is our bad...please refresh with cmd + r");
});
}, []);
// connect through ws to enrich basic lines data
useEffect(() => {
// $ws.on(SocketChannels.CONNECT, );
console.log("got basic user lines data!!!");
// ?need to calculate diff and only do stuff then?
// $ws.on(SocketChannels.CONNECT, console.log());
$ws.on("test", () => console.log("test"));
}, [basicUserLinesData]);
// get updated associations with certain lines
return (
<LineDataContext.Provider value={{} as ILineDataContext}>
{children}
</LineDataContext.Provider>
);
}
export function useLineDataProvider() {
return useContext(lineDataContext);
return useContext(LineDataContext);
}
@@ -56,6 +56,10 @@ export default class NirvanaApi {
}
}
async function serverCheck(): Promise<void> {
return await NirvanaApi.fetch(`/`, "GET", false);
}
async function login(reqLoginTokens: {
accessToken: string;
idToken: string;
@@ -98,6 +102,7 @@ async function createLine(
}
export const ApiCalls = {
serverCheck,
login,
authCheck,
getUserDetails,