feat: show loading before user data is read from storage
This commit is contained in:
parent
2713886e55
commit
e5cfef057f
2 changed files with 22 additions and 5 deletions
|
|
@ -7,6 +7,7 @@ import { storageUserGet, storageUserSave } from '@storage/storageUser';
|
||||||
type AuthContextData = {
|
type AuthContextData = {
|
||||||
user: UserDTO;
|
user: UserDTO;
|
||||||
signIn: (email: string, password: string) => Promise<void>;
|
signIn: (email: string, password: string) => Promise<void>;
|
||||||
|
isLoadingUserStorageData: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type AuthContextProviderProps = {
|
type AuthContextProviderProps = {
|
||||||
|
|
@ -17,6 +18,7 @@ export const AuthContext = createContext<AuthContextData>({} as AuthContextData)
|
||||||
|
|
||||||
export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
||||||
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
||||||
|
const [isLoadingUserStorageData, setIsLoadingUserStorageData] = useState(true);
|
||||||
|
|
||||||
async function signIn(email: string, password: string) {
|
async function signIn(email: string, password: string) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -33,9 +35,15 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadUserData() {
|
async function loadUserData() {
|
||||||
const loggedUser = await storageUserGet();
|
try {
|
||||||
if (loggedUser) {
|
const loggedUser = await storageUserGet();
|
||||||
setUser(loggedUser);
|
if (loggedUser) {
|
||||||
|
setUser(loggedUser);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
setIsLoadingUserStorageData(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,5 +51,9 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
||||||
loadUserData();
|
loadUserData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return <AuthContext.Provider value={{ user, signIn }}>{children}</AuthContext.Provider>;
|
return (
|
||||||
|
<AuthContext.Provider value={{ user, signIn, isLoadingUserStorageData }}>
|
||||||
|
{children}
|
||||||
|
</AuthContext.Provider>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,19 @@ import { AppRoutes } from './app.routes';
|
||||||
|
|
||||||
import { gluestackUIConfig } from '../../config/gluestack-ui.config';
|
import { gluestackUIConfig } from '../../config/gluestack-ui.config';
|
||||||
import { useAuth } from '@hooks/useAuth';
|
import { useAuth } from '@hooks/useAuth';
|
||||||
|
import { Loading } from '@components/Loading';
|
||||||
|
|
||||||
export function Routes() {
|
export function Routes() {
|
||||||
const theme = DefaultTheme;
|
const theme = DefaultTheme;
|
||||||
const { user } = useAuth();
|
const { user, isLoadingUserStorageData } = useAuth();
|
||||||
console.debug(user);
|
console.debug(user);
|
||||||
|
|
||||||
theme.colors.background = gluestackUIConfig.tokens.colors.gray700;
|
theme.colors.background = gluestackUIConfig.tokens.colors.gray700;
|
||||||
|
|
||||||
|
if (isLoadingUserStorageData) {
|
||||||
|
return <Loading />;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box flex={1} bg="$gray700">
|
<Box flex={1} bg="$gray700">
|
||||||
<NavigationContainer theme={theme}>
|
<NavigationContainer theme={theme}>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue