frontend/containers/EventBar/useActions.ts (view raw)
1import {useRouter} from 'next/router';
2import {useTranslation} from 'react-i18next';
3import useProfile from '../../hooks/useProfile';
4import useShare from '../../hooks/useShare';
5import useEventStore from '../../stores/useEventStore';
6
7interface Props {
8 onAdd: (isAddToMyEvent: boolean) => void;
9 eventId?: string;
10}
11
12const useActions = (props: Props) => {
13 const {onAdd, eventId} = props;
14 const {t} = useTranslation();
15 const router = useRouter();
16 const {connected} = useProfile();
17 const {share} = useShare();
18 const {event} = useEventStore();
19
20 const noUserMenuActions = [
21 {
22 label: t('event.actions.add_to_my_events'),
23 onClick: () => {
24 onAdd(true);
25 },
26 id: 'AddToMyEventsTab',
27 },
28 {divider: true},
29 {
30 label: t('event.actions.share'),
31 onClick: () =>
32 share({
33 title: `Caroster ${event.name}`,
34 }),
35 id: 'ShareEvent',
36 },
37 {divider: true},
38 {
39 label: t('menu.login'),
40 onClick: () => {
41 router.push(`/auth/login?redirectPath=${router.asPath}`);
42 },
43 id: 'SignInTab',
44 },
45 {
46 label: t('menu.register'),
47 onClick: () => {
48 router.push({
49 pathname: `/auth/register`,
50 query: {redirectPath: router.asPath},
51 state: {event: eventId},
52 });
53 },
54 id: 'SignUpTab',
55 },
56 {divider: true},
57 ];
58
59 const loggedMenuActions = [
60 {
61 label: t('menu.profile'),
62 onClick: () => router.push('/profile'),
63 id: 'ProfileTab',
64 },
65 {
66 label: t('menu.dashboard'),
67 onClick: () => (window.location.href = '/dashboard'),
68 id: 'GoToDashboardTab',
69 },
70 {
71 label: t('event.actions.share'),
72 onClick: () =>
73 share({
74 title: `Caroster ${event.name}`,
75 }),
76 id: 'ShareEvent',
77 },
78 {divider: true},
79 ];
80
81 return connected ? loggedMenuActions : noUserMenuActions;
82};
83
84export default useActions;