Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi/login logout toast #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import axios from 'axios';
const instance = axios.create({
baseURL: import.meta.env.VITE_BASE_URL
? import.meta.env.VITE_BASE_URL
: // : 'http://127.0.0.1:3000',
'https://points-api.illinoiswcs.org',
: 'http://127.0.0.1:3000',
// : 'https://points-api.illinoiswcs.org',
withCredentials: true
});

Expand Down
9 changes: 5 additions & 4 deletions src/components/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ const Navbar = ({ onClose, ...rest }: NavbarProps): React.ReactElement => {
);

const Greeting = (): React.ReactElement => {
if (!data) return null;

const names = data?.name.split(' ');
const name = names ? names[0] : 'undefined';
const name = names[0];

return (
<Box p="4" m="4">
<Text>
{data?.name ? `Welcome back, ${name}!` : 'Hello, guest user!'}
</Text>
<Text>Welcome back, {name}!</Text>
</Box>
);
};
Expand Down
81 changes: 68 additions & 13 deletions src/pages/CheckIn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { VStack, Heading, Text, Button, Input, Box } from '@chakra-ui/react';

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

const CheckIn = (): React.ReactElement => {
const [eventKey, setEventKey] = useState('');
Expand All @@ -13,7 +14,7 @@ const CheckIn = (): React.ReactElement => {
setEventKey(event.target.value);
};

const handleSubmit = (): void => {
const handleSubmit = async (): Promise<void> => {
const isEventKeyError = eventKey === '';
setEventKeyError(isEventKeyError);
if (isEventKeyError) return;
Expand All @@ -24,21 +25,68 @@ const CheckIn = (): React.ReactElement => {
toastSuccess(res.data.message);
})
.catch((err) => {
toastError(err.response.data.message);
toastError(
<div>
Not Logged in.{' '}
<a
href="#"
onClick={(e) => {
e.preventDefault();
void handleClick();
}}
style={{ color: '#f9dcf6', textDecoration: 'underline' }}
>
Login
</a>
</div>
);
console.log(err);
});
};

const { isError, error } = useProfileQuery();
const { data } = useQuery<Profile, Error>(
['get-profile'],
async () => {
try {
const res = await axiosInstance.get('/profile');
return res.data;
} catch (err: unknown) {
if (
err instanceof Error &&
(err as any).response &&
((err as any).response.status === 401 ||
(err as any).response.status === 403)
) {
return null;
}
throw err;
}
},
{
retry: (failureCount, error: any) => {
if (
error.response &&
(error.response.status === 401 || error.response.status === 403)
) {
return false;
}
return failureCount < 3;
}
}
);

if (isError) {
console.log(error);
return (
<Box>
<Heading size="lg">Temporary Error</Heading>
</Box>
);
}
const handleClick = async (): Promise<void> => {
if (data) {
// user clicked logout
await axiosInstance.post('/auth/logout', {});
window.location.href = '/';
} else {
// user clicked login
window.location.href = `${String(
axiosInstance.defaults.baseURL
)}/auth/login`;
}
};

return (
<Box>
Expand All @@ -62,7 +110,14 @@ const CheckIn = (): React.ReactElement => {
value={eventKey}
onChange={handleChangeKey}
/>
<Button onClick={handleSubmit}>Check-in</Button>
<Button
onClick={(e) => {
e.preventDefault();
handleSubmit().catch(console.error);
}}
>
Check-in
</Button>
</VStack>
</Box>
);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const toastSuccess = (msg: string): void => {
});
};

export const toastError = (msg: string): void => {
toast.error(`🦄 ${msg}`, {
export const toastError = (msg: React.ReactNode): void => {
toast.error(msg, {
position: 'top-right',
autoClose: 2500,
hideProgressBar: false,
Expand Down