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';
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';
13 const apply = async tournament => {
15 await axios.post(`/api/tournaments/${tournament.id}/apply`);
16 toastr.success(i18n.t('tournaments.applySuccess'));
18 toastr.error(i18n.t('tournaments.applyError'));
22 const getTitle = (user, tournament) => {
23 if (isDeniedApplicant(user, tournament)) {
24 return i18n.t('tournaments.applicationDenied');
26 if (isApplicant(user, tournament)) {
27 return i18n.t('tournaments.applicationPending');
29 return i18n.t('tournaments.apply');
32 const ApplyButton = ({ tournament }) => {
33 const { user } = useUser();
35 if (!user || !tournament.accept_applications || isRunner(user, tournament)) return null;
37 return <span className="d-inline-block" title={getTitle(user, tournament)}>
39 disabled={!mayApply(user, tournament)}
40 onClick={() => apply(tournament)}
43 <Icon.APPLY title="" />
48 ApplyButton.propTypes = {
49 tournament: PropTypes.shape({
50 accept_applications: PropTypes.bool,
55 export default withTranslation()(ApplyButton);