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 { withUser } from '../../helpers/UserContext';
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, user }) => {
33 if (!tournament.accept_applications || isRunner(user, tournament)) return null;
35 return <span className="d-inline-block" title={getTitle(user, tournament)}>
37 disabled={!mayApply(user, tournament)}
38 onClick={() => apply(tournament)}
41 <Icon.APPLY title="" />
46 ApplyButton.propTypes = {
47 tournament: PropTypes.shape({
48 accept_applications: PropTypes.bool,
51 user: PropTypes.shape({
55 export default withTranslation()(withUser(ApplyButton));