11import { useState , useEffect } from "react" ;
22import { Box , Button , Spinner , useToast } from '@chakra-ui/react'
3- import { fetchUserProfile , getUserProfileFromStorage , getAccessToken , openAuthWindow , saveAccessToken , saveAuthCode , saveUserProfile , getAccessTokenFromStorage , deleteAccessToken } from "./helpers/auth" ;
3+ import { fetchUserProfile , getUserProfileFromStorage , getAccessToken , openAuthWindow , saveAccessToken , saveAuthCode , saveUserProfile , getAccessTokenFromStorage , deleteAccessToken , handleInitialLogin , handleLoadFrom , handleLogin , handleLogout } from "./helpers/auth" ;
44import { UserProfile } from "./types/googleapis" ;
55import { loadContextmenu , pushNotification } from "./helpers/windowhelper" ;
66import TaskPage from "./components/TaskPage" ;
77import { useRecoilState , useRecoilValue , useSetRecoilState } from "recoil" ;
8- import { accessTokenState , activeCategoryTasksState , activeTaskCategoryState , attemptLoginState , attemptLogoutState , loggedInSelector , messageState , userProfileState } from "./config/states" ;
8+ import { accessTokenState , activeCategoryTasksState , activeTaskCategoryState , attemptLoginState , attemptLogoutState , authLoadingState , loggedInSelector , messageState , userProfileState } from "./config/states" ;
99import Header from "./components/ui/Header" ;
1010import { task } from "./types/taskapi" ;
1111import { listen_for_auth_code } from "./helpers/eventlistner" ;
12- import { test_command } from "./helpers/invoker" ;
1312import { AccessToken } from "./helpers/commands" ;
1413
1514// disable default context menu on build
1615loadContextmenu ( ) ;
1716
1817function App ( ) {
19- const [ loading , setLoading ] = useState < boolean > ( false ) ;
18+ const [ loading , setLoading ] = useRecoilState < boolean > ( authLoadingState ) ;
2019 const loggedIn = useRecoilValue ( loggedInSelector ) ;
2120 const setProfile = useSetRecoilState < UserProfile | null > ( userProfileState ) ;
2221 const setAccessToken = useSetRecoilState < string | null > ( accessTokenState ) ;
@@ -92,7 +91,7 @@ function App() {
9291 // check the offline data for access token
9392 useEffect ( ( ) => {
9493 setLoading ( true )
95- handleInitialLogin ( ) . catch ( ( err ) => {
94+ handleInitialLogin ( ) . catch ( ( err ) => {
9695 console . log ( err ) ;
9796 setLoading ( false )
9897 setToastMessage ( {
@@ -106,77 +105,8 @@ function App() {
106105 } , [ ] )
107106
108107
109- async function handleInitialLogin ( ) {
110- // get access token from storage
111- const accessToken = await getAccessTokenFromStorage ( ) ;
112- if ( ! accessToken ) throw new Error ( "Signin required" ) ;
113- pushNotification ( "Login Successful" )
114- const profile = navigator . onLine ? await fetchUserProfile ( accessToken . access_token ) : await getUserProfileFromStorage ( ) ;
115- if ( ! profile || ! profile ?. email ) throw new Error ( "Something went wrong, please try again" ) ;
116- setProfile ( profile ) ;
117- setAccessToken ( accessToken . access_token ) ;
118- pushNotification ( `welcome back ${ profile . name } ` )
119- }
120-
121-
122- async function handleLoadFrom ( accessTokenBody : AccessToken ) {
123- try {
124- await saveAccessToken ( JSON . stringify ( accessTokenBody , null , 2 ) )
125- setAccessToken ( accessTokenBody . access_token ) ;
126- const userProfile = await fetchUserProfile ( accessTokenBody . access_token ) ;
127- await saveUserProfile ( userProfile )
128- setProfile ( userProfile ) ;
129- }
130- catch ( err ) {
131- console . log ( err ) ;
132- setLoading ( false )
133- await handleLogout ( ) ;
134- setToastMessage ( {
135- title : "Error" ,
136- body : "Error signing in" ,
137- type : "error"
138- } )
139- }
140- }
141108
142-
143-
144-
145- async function handleLogout ( ) {
146- setLoading ( true )
147- setAccessToken ( null ) ;
148- setProfile ( null ) ;
149- setActiveTaskCategory ( - 1 )
150- setActiveCategoryTasksState ( [ ] )
151- await deleteAccessToken ( ) ;
152- setLoading ( false )
153- }
154-
155-
156109
157-
158-
159- async function handleLogin ( ) {
160- setLoading ( true )
161- try {
162- const storedAccessToken = await getAccessTokenFromStorage ( ) ;
163- if ( storedAccessToken ) {
164- handleLoadFrom ( storedAccessToken ) ;
165- pushNotification ( 'Login Successful' )
166- return ;
167- }
168- pushNotification ( 'login required' )
169- await openAuthWindow ( ) ;
170- } catch ( error ) {
171- console . log ( error ) ;
172- setLoading ( false )
173- setToastMessage ( {
174- title : "Error" ,
175- body : "Error signing in" ,
176- type : "error"
177- } )
178- }
179- }
180110
181111
182112 return (
0 commit comments