feat(dashboard-v2): add UserContext
This commit is contained in:
parent
a07b7da396
commit
6271d06ed0
|
@ -0,0 +1,6 @@
|
|||
import { createContext } from "react";
|
||||
|
||||
export const UserContext = createContext({
|
||||
user: null,
|
||||
error: null,
|
||||
});
|
|
@ -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>;
|
||||
};
|
|
@ -0,0 +1,2 @@
|
|||
export * from "./UserProvider";
|
||||
export * from "./useUser";
|
|
@ -0,0 +1,5 @@
|
|||
import { useContext } from "react";
|
||||
|
||||
import { UserContext } from "./UserContext";
|
||||
|
||||
export const useUser = () => useContext(UserContext);
|
Reference in New Issue