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 = {
|
||||
user: UserDTO;
|
||||
signIn: (email: string, password: string) => Promise<void>;
|
||||
isLoadingUserStorageData: boolean;
|
||||
};
|
||||
|
||||
type AuthContextProviderProps = {
|
||||
|
|
@ -17,6 +18,7 @@ export const AuthContext = createContext<AuthContextData>({} as AuthContextData)
|
|||
|
||||
export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
||||
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
||||
const [isLoadingUserStorageData, setIsLoadingUserStorageData] = useState(true);
|
||||
|
||||
async function signIn(email: string, password: string) {
|
||||
try {
|
||||
|
|
@ -33,15 +35,25 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
|||
}
|
||||
|
||||
async function loadUserData() {
|
||||
try {
|
||||
const loggedUser = await storageUserGet();
|
||||
if (loggedUser) {
|
||||
setUser(loggedUser);
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoadingUserStorageData(false);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
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 { useAuth } from '@hooks/useAuth';
|
||||
import { Loading } from '@components/Loading';
|
||||
|
||||
export function Routes() {
|
||||
const theme = DefaultTheme;
|
||||
const { user } = useAuth();
|
||||
const { user, isLoadingUserStorageData } = useAuth();
|
||||
console.debug(user);
|
||||
|
||||
theme.colors.background = gluestackUIConfig.tokens.colors.gray700;
|
||||
|
||||
if (isLoadingUserStorageData) {
|
||||
return <Loading />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Box flex={1} bg="$gray700">
|
||||
<NavigationContainer theme={theme}>
|
||||
|
|
|
|||
Loading…
Reference in a new issue