]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/results/ReportButton.js
improved user context
[alttp.git] / resources / js / components / results / ReportButton.js
1 import PropTypes from 'prop-types';
2 import React, { useState } from 'react';
3 import { Button } from 'react-bootstrap';
4 import { withTranslation } from 'react-i18next';
5
6 import ReportDialog from './ReportDialog';
7 import Icon from '../common/Icon';
8 import { findResult } from '../../helpers/User';
9 import i18n from '../../i18n';
10
11 const getButtonLabel = (user, round) => {
12         const result = findResult(user, round);
13         if (round.locked) {
14                 if (result && result.comment) {
15                         return i18n.t('results.editComment');
16                 } else {
17                         return i18n.t('results.addComment');
18                 }
19         } else {
20                 if (result && (result.time || result.forfeit)) {
21                         return i18n.t('results.edit');
22                 } else {
23                         return i18n.t('results.report');
24                 }
25         }
26 };
27
28 const ReportButton = ({ round, user }) => {
29         const [showDialog, setShowDialog] = useState(false);
30
31         if (round.locked && !findResult(user, round)) {
32                 return null;
33         }
34
35         return <>
36                 <Button
37                         onClick={() => setShowDialog(true)}
38                         variant="secondary"
39                 >
40                         {getButtonLabel(user, round)}
41                         {' '}
42                         <Icon.EDIT title="" />
43                 </Button>
44                 <ReportDialog
45                         onHide={() => setShowDialog(false)}
46                         round={round}
47                         show={showDialog}
48                         user={user}
49                 />
50         </>;
51 };
52
53 ReportButton.propTypes = {
54         round: PropTypes.shape({
55                 locked: PropTypes.bool,
56         }),
57         tournament: PropTypes.shape({
58         }),
59         user: PropTypes.shape({
60         }),
61 };
62
63 export default withTranslation()(ReportButton);