@@ -3,7 +3,8 @@ import { VStack, Heading, Text, Button, Input, Box } from '@chakra-ui/react';
33
44import axiosInstance from '../../api' ;
55import { toastError , toastSuccess } from '../../utils/toast' ;
6- import { useProfileQuery } from '../Events/useProfileQuery' ;
6+ import { useQuery } from 'react-query' ;
7+ import { Profile } from '../../types/profile' ;
78
89const CheckIn = ( ) : React . ReactElement => {
910 const [ eventKey , setEventKey ] = useState ( '' ) ;
@@ -13,32 +14,84 @@ const CheckIn = (): React.ReactElement => {
1314 setEventKey ( event . target . value ) ;
1415 } ;
1516
16- const handleSubmit = ( ) : void => {
17+ const handleSubmit = async ( ) : Promise < void > => {
1718 const isEventKeyError = eventKey === '' ;
1819 setEventKeyError ( isEventKeyError ) ;
1920 if ( isEventKeyError ) return ;
2021
22+ if ( ! data ) {
23+ toastError (
24+ < div >
25+ Not logged in.{ ' ' }
26+ < a
27+ href = "#"
28+ onClick = { ( e ) => {
29+ e . preventDefault ( ) ;
30+ void handleClick ( ) ;
31+ } }
32+ style = { { color : '#f9dcf6' , textDecoration : 'underline' } }
33+ >
34+ Login
35+ </ a >
36+ </ div >
37+ ) ;
38+ return ;
39+ }
40+
2141 axiosInstance
2242 . patch ( '/profile' , { eventKey } )
2343 . then ( ( res ) => {
2444 toastSuccess ( res . data . message ) ;
2545 } )
2646 . catch ( ( err ) => {
27- toastError ( err . response . data . message ) ;
47+ toastError ( err . response ? .data ? .message || 'An error occurred' ) ;
2848 console . log ( err ) ;
2949 } ) ;
3050 } ;
3151
32- const { isError, error } = useProfileQuery ( ) ;
52+ const { data } = useQuery < Profile , Error > (
53+ [ 'get-profile' ] ,
54+ async ( ) => {
55+ try {
56+ const res = await axiosInstance . get ( '/profile' ) ;
57+ return res . data ;
58+ } catch ( err : unknown ) {
59+ if (
60+ err instanceof Error &&
61+ ( err as any ) . response &&
62+ ( ( err as any ) . response . status === 401 ||
63+ ( err as any ) . response . status === 403 )
64+ ) {
65+ return null ;
66+ }
67+ throw err ;
68+ }
69+ } ,
70+ {
71+ retry : ( failureCount , error : any ) => {
72+ if (
73+ error . response &&
74+ ( error . response . status === 401 || error . response . status === 403 )
75+ ) {
76+ return false ;
77+ }
78+ return failureCount < 3 ;
79+ }
80+ }
81+ ) ;
3382
34- if ( isError ) {
35- console . log ( error ) ;
36- return (
37- < Box >
38- < Heading size = "lg" > Temporary Error</ Heading >
39- </ Box >
40- ) ;
41- }
83+ const handleClick = async ( ) : Promise < void > => {
84+ if ( data ) {
85+ // user clicked logout
86+ await axiosInstance . post ( '/auth/logout' , { } ) ;
87+ window . location . href = '/' ;
88+ } else {
89+ // user clicked login
90+ window . location . href = `${ String (
91+ axiosInstance . defaults . baseURL
92+ ) } /auth/login`;
93+ }
94+ } ;
4295
4396 return (
4497 < Box >
@@ -62,7 +115,14 @@ const CheckIn = (): React.ReactElement => {
62115 value = { eventKey }
63116 onChange = { handleChangeKey }
64117 />
65- < Button onClick = { handleSubmit } > Check-in</ Button >
118+ < Button
119+ onClick = { ( e ) => {
120+ e . preventDefault ( ) ;
121+ handleSubmit ( ) . catch ( console . error ) ;
122+ } }
123+ >
124+ Check-in
125+ </ Button >
66126 </ VStack >
67127 </ Box >
68128 ) ;
0 commit comments