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