Skip to content

Commit bc0fd02

Browse files
Hi/login logout toast (#47)
* have meaningful pop-up * displaying no text when logged out * fixing pr error * Update api.ts * Update index.tsx --------- Co-authored-by: Firmiana <[email protected]>
1 parent 892e5bd commit bc0fd02

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed

src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import axios from 'axios';
33
const instance = axios.create({
44
baseURL: import.meta.env.VITE_BASE_URL
55
? import.meta.env.VITE_BASE_URL
6-
: // : 'http://127.0.0.1:3000',
7-
'https://points-api.illinoiswcs.org',
6+
// : 'http://127.0.0.1:3000',
7+
: 'https://points-api.illinoiswcs.org',
88
withCredentials: true
99
});
1010

src/components/Navbar/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ const Navbar = ({ onClose, ...rest }: NavbarProps): React.ReactElement => {
5959
);
6060

6161
const Greeting = (): React.ReactElement => {
62+
if (!data) return null;
63+
6264
const names = data?.name.split(' ');
63-
const name = names ? names[0] : 'undefined';
65+
const name = names[0];
66+
6467
return (
6568
<Box p="4" m="4">
66-
<Text>
67-
{data?.name ? `Welcome back, ${name}!` : 'Hello, guest user!'}
68-
</Text>
69+
<Text>Welcome back, {name}!</Text>
6970
</Box>
7071
);
7172
};

src/pages/CheckIn/index.tsx

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { VStack, Heading, Text, Button, Input, Box } from '@chakra-ui/react';
33

44
import axiosInstance from '../../api';
55
import { toastError, toastSuccess } from '../../utils/toast';
6-
import { useProfileQuery } from '../Events/useProfileQuery';
6+
import { useQuery } from 'react-query';
7+
import { Profile } from '../../types/profile';
78

89
const 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
);

src/utils/toast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export const toastSuccess = (msg: string): void => {
1313
});
1414
};
1515

16-
export const toastError = (msg: string): void => {
17-
toast.error(`🦄 ${msg}`, {
16+
export const toastError = (msg: React.ReactNode): void => {
17+
toast.error(msg, {
1818
position: 'top-right',
1919
autoClose: 2500,
2020
hideProgressBar: false,

0 commit comments

Comments
 (0)