@@ -4,8 +4,8 @@ import { fetchUserProfile,getUserProfileFromStorage, getAccessToken, openAuthWi
44import { AccessToken , UserProfile } from "./types/googleapis" ;
55import { loadContextmenu , pushNotification } from "./helpers/windowhelper" ;
66import TaskPage from "./components/TaskPage" ;
7- import { useRecoilState , useSetRecoilState } from "recoil" ;
8- import { accessTokenState , activeCategoryTasksState , activeTaskCategoryState , attemptLoginState , attemptLogoutState , loggedInState , messageState , userProfileState } from "./config/states" ;
7+ import { useRecoilState , useRecoilValue , useSetRecoilState } from "recoil" ;
8+ import { accessTokenState , activeCategoryTasksState , activeTaskCategoryState , attemptLoginState , attemptLogoutState , 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" ;
@@ -15,7 +15,7 @@ loadContextmenu();
1515
1616function App ( ) {
1717 const [ loading , setLoading ] = useState < boolean > ( false ) ;
18- const [ loggedIn , setLoggedIn ] = useRecoilState < boolean > ( loggedInState ) ;
18+ const loggedIn = useRecoilValue ( loggedInSelector ) ;
1919 const setProfile = useSetRecoilState < UserProfile | null > ( userProfileState ) ;
2020 const setAccessToken = useSetRecoilState < string | null > ( accessTokenState ) ;
2121 const [ attemptedLogin , setAttemptedLogin ] = useRecoilState < boolean > ( attemptLoginState ) ;
@@ -44,6 +44,20 @@ function App() {
4444 } , [ toastMessage ] )
4545
4646
47+ useEffect ( ( ) => {
48+ if ( attemptedLogin ) {
49+ handleLogin ( ) ;
50+ setAttemptedLogin ( false ) ;
51+ }
52+ } , [ attemptedLogin ] )
53+
54+ useEffect ( ( ) => {
55+ if ( attemptedLogout ) {
56+ handleLogout ( ) ;
57+ setAttemptedLogout ( false ) ;
58+ }
59+ } , [ attemptedLogout ] )
60+
4761
4862
4963 // to generate a port and listen to it
@@ -76,60 +90,40 @@ function App() {
7690 // check the offline data for access token
7791 useEffect ( ( ) => {
7892 setLoading ( true )
79- // get access token from storage
80- getAccessTokenFromStorage ( ) . then ( ( accessToken ) => {
81- try {
82- if ( ! accessToken ) throw new Error ( "Signin required" ) ;
83- pushNotification ( "Login Successful" )
84- if ( navigator . onLine ) {
85- // fetch user profile
86- fetchUserProfile ( accessToken . access_token ) . then ( ( profile ) => {
87- if ( ! profile ) throw new Error ( "Something went wrong, please try again" ) ;
88- setProfile ( profile ) ;
89- setAccessToken ( accessToken . access_token ) ;
90- setLoggedIn ( true ) ;
91- setLoading ( false )
92- pushNotification ( `welcome back ${ profile . name } ` )
93- } ) ;
94- }
95- else {
96- getUserProfileFromStorage ( ) . then ( ( profile ) => {
97- if ( ! profile ) throw new Error ( "Signin required" ) ;
98- setProfile ( profile ) ;
99- setAccessToken ( accessToken . access_token ) ;
100- setLoggedIn ( true ) ;
101- pushNotification ( `welcome back ${ profile . name } ` )
102- } ) ;
103- }
104- } catch ( err ) {
105- console . log ( err ) ;
106- setLoading ( false )
107- setToastMessage ( {
108- title : "Error" ,
109- body : "Error signing in" ,
110- type : "error"
111- } )
112- }
93+ handleInitialLogin ( ) . catch ( ( err ) => {
94+ console . log ( err ) ;
95+ setLoading ( false )
96+ setToastMessage ( {
97+ title : "Error" ,
98+ body : "Error signing in" ,
99+ type : "error"
100+ } )
113101 } ) . finally ( ( ) => {
114102 setLoading ( false )
115103 } )
116104 } , [ ] )
117105
118106
107+ async function handleInitialLogin ( ) {
108+ // get access token from storage
109+ const accessToken = await getAccessTokenFromStorage ( ) ;
110+ if ( ! accessToken ) throw new Error ( "Signin required" ) ;
111+ pushNotification ( "Login Successful" )
112+ const profile = navigator . onLine ? await fetchUserProfile ( accessToken . access_token ) : await getUserProfileFromStorage ( ) ;
113+ if ( ! profile ) throw new Error ( "Something went wrong, please try again" ) ;
114+ setProfile ( profile ) ;
115+ setAccessToken ( accessToken . access_token ) ;
116+ pushNotification ( `welcome back ${ profile . name } ` )
117+ }
118+
119119
120120 async function handleLoadFrom ( accessTokenBody : AccessToken ) {
121121 try {
122- saveAccessToken ( JSON . stringify ( accessTokenBody , null , 2 ) ) . then ( ( ) => {
123- console . log ( "access token saved" ) ;
124- } ) ;
122+ await saveAccessToken ( JSON . stringify ( accessTokenBody , null , 2 ) )
125123 setAccessToken ( accessTokenBody . access_token ) ;
126124 const userProfile = await fetchUserProfile ( accessTokenBody . access_token ) ;
127- saveUserProfile ( userProfile ) . then ( ( ) => {
128- console . log ( "user profile saved" ) ;
129- } ) ;
130- console . log ( userProfile ) ;
125+ await saveUserProfile ( userProfile )
131126 setProfile ( userProfile ) ;
132- setLoggedIn ( true ) ;
133127 }
134128 catch ( err ) {
135129 console . log ( err ) ;
@@ -142,32 +136,21 @@ function App() {
142136 } )
143137 }
144138 }
139+
140+
145141
146142
147143 async function handleLogout ( ) {
148144 setLoading ( true )
149145 setAccessToken ( null ) ;
150146 setProfile ( null ) ;
151- setLoggedIn ( false ) ;
152147 setActiveTaskCategory ( - 1 )
153148 setActiveCategoryTasksState ( [ ] )
154149 await deleteAccessToken ( ) ;
155150 setLoading ( false )
156151 }
157152
158- useEffect ( ( ) => {
159- if ( attemptedLogin ) {
160- handleLogin ( ) ;
161- setAttemptedLogin ( false ) ;
162- }
163- } , [ attemptedLogin ] )
164-
165- useEffect ( ( ) => {
166- if ( attemptedLogout ) {
167- handleLogout ( ) ;
168- setAttemptedLogout ( false ) ;
169- }
170- } , [ attemptedLogout ] )
153+
171154
172155
173156
0 commit comments