feat(dashboard-v2): add UserContext

This commit is contained in:
Michał Leszczyk 2022-03-02 11:44:48 +01:00
parent a07b7da396
commit 6271d06ed0
No known key found for this signature in database
GPG Key ID: FA123CA8BAA2FBF4
4 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import { createContext } from "react";
export const UserContext = createContext({
user: null,
error: null,
});

View File

@ -0,0 +1,17 @@
import { useEffect, useState } from "react";
import useSWR from "swr";
import { UserContext } from "./UserContext";
export const UserProvider = ({ children }) => {
const { data: user, error, mutate } = useSWR("user");
const [loading, setLoading] = useState(true);
useEffect(() => {
if (user || error) {
setLoading(false);
}
}, [user, error]);
return <UserContext.Provider value={{ user, error, loading, mutate }}>{children}</UserContext.Provider>;
};

View File

@ -0,0 +1,2 @@
export * from "./UserProvider";
export * from "./useUser";

View File

@ -0,0 +1,5 @@
import { useContext } from "react";
import { UserContext } from "./UserContext";
export const useUser = () => useContext(UserContext);