fix useUser
This commit is contained in:
parent
68cf6f3f87
commit
0ccf744575
|
@ -1,6 +0,0 @@
|
||||||
import React from "react";
|
|
||||||
import { AT } from "@lingdocs/lingdocs-main";
|
|
||||||
|
|
||||||
const UserContext = React.createContext<undefined | AT.LingdocsUser>(undefined);
|
|
||||||
|
|
||||||
export default UserContext;
|
|
|
@ -1,17 +1,20 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
export default function useStickyState<T>(defaultValue: T, key: string): [T, React.Dispatch<React.SetStateAction<T>>] {
|
export default function useStickyState<T>(defaultValue: T, key: string): {
|
||||||
|
value: T,
|
||||||
|
setValue: React.Dispatch<React.SetStateAction<T>>,
|
||||||
|
} {
|
||||||
|
|
||||||
const [value, setValue] = useState<T>(() => {
|
const [value, setValue] = useState<T>(() => {
|
||||||
const stickyValue = window.localStorage.getItem(key);
|
const stickyValue = window.localStorage.getItem(key);
|
||||||
return stickyValue !== null
|
return stickyValue !== null
|
||||||
? JSON.parse(stickyValue) as T
|
? JSON.parse(stickyValue) as T
|
||||||
: defaultValue;
|
: defaultValue;
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.localStorage.setItem(key, JSON.stringify(value));
|
window.localStorage.setItem(key, JSON.stringify(value));
|
||||||
}, [key, value]);
|
}, [key, value]);
|
||||||
|
|
||||||
return [value, setValue];
|
return {value, setValue};
|
||||||
}
|
}
|
|
@ -14,7 +14,10 @@ const UserContext = createContext<
|
||||||
|
|
||||||
// TODO: persisting user in local state
|
// TODO: persisting user in local state
|
||||||
function UserProvider({ children }: any) {
|
function UserProvider({ children }: any) {
|
||||||
const [value, setValue] = useStickyState<AT.LingdocsUser | undefined>(undefined, "saved-user");
|
const {value, setValue} = useStickyState<AT.LingdocsUser | undefined>(
|
||||||
|
undefined,
|
||||||
|
"saved-user",
|
||||||
|
);
|
||||||
|
|
||||||
function pullUser() {
|
function pullUser() {
|
||||||
getUser().then((user) => {
|
getUser().then((user) => {
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue