]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/tournament/ApplyButton.js
svg dungeon tracker
[alttp.git] / resources / js / components / tournament / ApplyButton.js
1 import axios from 'axios';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button } from 'react-bootstrap';
5 import { withTranslation } from 'react-i18next';
6 import toastr from 'toastr';
7
8 import Icon from '../common/Icon';
9 import { isApplicant, isDeniedApplicant, isRunner, mayApply } from '../../helpers/permissions';
10 import { useUser } from '../../hooks/user';
11 import i18n from '../../i18n';
12
13 const apply = async tournament => {
14         try {
15                 await axios.post(`/api/tournaments/${tournament.id}/apply`);
16                 toastr.success(i18n.t('tournaments.applySuccess'));
17         } catch (e) {
18                 toastr.error(i18n.t('tournaments.applyError'));
19         }
20 };
21
22 const getTitle = (user, tournament) => {
23         if (isDeniedApplicant(user, tournament)) {
24                 return i18n.t('tournaments.applicationDenied');
25         }
26         if (isApplicant(user, tournament)) {
27                 return i18n.t('tournaments.applicationPending');
28         }
29         return i18n.t('tournaments.apply');
30 };
31
32 const ApplyButton = ({ tournament }) => {
33         const { user } = useUser();
34
35         if (!user || !tournament.accept_applications || isRunner(user, tournament)) return null;
36
37         return <span className="d-inline-block" title={getTitle(user, tournament)}>
38                 <Button
39                         disabled={!mayApply(user, tournament)}
40                         onClick={() => apply(tournament)}
41                         variant="primary"
42                 >
43                         <Icon.APPLY title="" />
44                 </Button>
45         </span>;
46 };
47
48 ApplyButton.propTypes = {
49         tournament: PropTypes.shape({
50                 accept_applications: PropTypes.bool,
51                 id: PropTypes.number,
52         }),
53 };
54
55 export default withTranslation()(ApplyButton);