frontend/containers/EventBar/useActions.ts (view raw)
1import {useRouter} from 'next/router';
2import {useTranslation} from 'next-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(s => s.event);
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 icon: 'account_circle',
65 },
66 {
67 label: t('menu.dashboard'),
68 onClick: () => (window.location.href = '/dashboard'),
69 id: 'GoToDashboardTab',
70 icon: 'space_dashboard',
71 },
72 {
73 label: t('event.actions.share'),
74 onClick: () =>
75 share({
76 title: `Caroster ${event.name}`,
77 }),
78 id: 'ShareEvent',
79 icon: 'share',
80 },
81 {divider: true},
82 ];
83
84 return connected ? loggedMenuActions : noUserMenuActions;
85};
86
87export default useActions;