]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/tournament/SettingsDialog.js
tournament admin control
[alttp.git] / resources / js / components / tournament / SettingsDialog.js
1 import axios from 'axios';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button, Modal } from 'react-bootstrap';
5 import { withTranslation } from 'react-i18next';
6 import toastr from 'toastr';
7
8 import ToggleSwitch from '../common/ToggleSwitch';
9 import i18n from '../../i18n';
10
11 const open = async tournament => {
12         try {
13                 await axios.post(`/api/tournaments/${tournament.id}/open`);
14                 toastr.success(i18n.t('tournaments.openSuccess'));
15         } catch (e) {
16                 toastr.error(i18n.t('tournaments.openError'));
17         }
18 };
19
20 const close = async tournament => {
21         try {
22                 await axios.post(`/api/tournaments/${tournament.id}/close`);
23                 toastr.success(i18n.t('tournaments.closeSuccess'));
24         } catch (e) {
25                 toastr.error(i18n.t('tournaments.closeError'));
26         }
27 };
28
29 const lock = async tournament => {
30         try {
31                 await axios.post(`/api/tournaments/${tournament.id}/lock`);
32                 toastr.success(i18n.t('tournaments.lockSuccess'));
33         } catch (e) {
34                 toastr.error(i18n.t('tournaments.lockError'));
35         }
36 };
37
38 const unlock = async tournament => {
39         try {
40                 await axios.post(`/api/tournaments/${tournament.id}/unlock`);
41                 toastr.success(i18n.t('tournaments.unlockSuccess'));
42         } catch (e) {
43                 toastr.error(i18n.t('tournaments.unlockError'));
44         }
45 };
46
47 const SettingsDialog = ({
48         onHide,
49         show,
50         tournament,
51 }) =>
52 <Modal className="settings-dialog" onHide={onHide} show={show}>
53         <Modal.Header closeButton>
54                 <Modal.Title>
55                         {i18n.t('tournaments.settings')}
56                 </Modal.Title>
57         </Modal.Header>
58         <Modal.Body>
59                 <div className="d-flex align-items-center justify-content-between mb-3">
60                         <span>{i18n.t('tournaments.open')}</span>
61                         <ToggleSwitch
62                                 onChange={({ target: { value } }) => value ? open(tournament) : close(tournament)}
63                                 value={tournament.accept_applications}
64                         />
65                 </div>
66                 <div className="d-flex align-items-center justify-content-between">
67                         <span>{i18n.t('tournaments.locked')}</span>
68                         <ToggleSwitch
69                                 onChange={({ target: { value } }) => value ? lock(tournament) : unlock(tournament)}
70                                 value={tournament.locked}
71                         />
72                 </div>
73         </Modal.Body>
74         <Modal.Footer>
75                 <Button onClick={onHide} variant="secondary">
76                         {i18n.t('button.close')}
77                 </Button>
78         </Modal.Footer>
79 </Modal>;
80
81 SettingsDialog.propTypes = {
82         onHide: PropTypes.func,
83         show: PropTypes.bool,
84         tournament: PropTypes.shape({
85                 accept_applications: PropTypes.bool,
86                 locked: PropTypes.bool,
87         }),
88 };
89
90 export default withTranslation()(SettingsDialog);