-
Notifications
You must be signed in to change notification settings - Fork 200
/
GameWidget.jsx
202 lines (186 loc) · 6.54 KB
/
GameWidget.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { Component, useState } from 'react';
import _ from 'lodash';
import { connect } from 'react-redux';
import cn from 'classnames';
import * as selectors from '../selectors';
import Editor from './Editor';
import EditorToolbar from './EditorsToolbars/EditorToolbar';
import GameActionButtons from '../components/GameActionButtons';
import * as GameActions from '../middlewares/Game';
import OutputClicker from './OutputClicker';
import editorModes from '../config/editorModes';
import GameStatusCodes from '../config/gameStatusCodes';
import OutputTab from '../components/ExecutionOutput/OutputTab';
import Output from '../components/ExecutionOutput/Output';
const RightSide = ({ output, children }) => {
const [showTab, setShowTab] = useState('editor');
const over = showTab === 'editor' ? '' : 'overflow-auto';
const isShowOutput = output && output.status;
return (
<>
<div className={`flex-grow-1 ${over}`} id="editor">
{showTab === 'editor' ? <div className="h-100">{children}</div>
: (
<div className="h-auto">
{isShowOutput && <Output sideOutput={output} />}
</div>
)}
</div>
<nav>
<div className="nav nav-tabs bg-gray text-uppercase text-center font-weight-bold" id="nav-tab" role="tablist">
<a
className={cn(
'nav-item nav-link flex-grow-1 text-black rounded-0 px-5',
{ active: showTab === 'editor' },
)}
href="#Editor"
onClick={e => {
e.preventDefault();
setShowTab('editor');
}}
>
Editor
</a>
<a
className={cn(
'nav-item nav-link flex-grow-1 text-black rounded-0 p-2 block',
{ active: showTab === 'output' },
)}
href="#Output"
onClick={e => {
e.preventDefault();
setShowTab('output');
}}
>
{isShowOutput && <OutputTab sideOutput={output} side="right" />}
</a>
</div>
</nav>
</>
);
};
class GameWidget extends Component {
static defaultProps = {
leftEditor: {},
rightEditor: {},
};
getLeftEditorParams = () => {
const {
isStoredGame,
currentUserId,
players,
leftEditor,
updateEditorValue,
checkResult,
leftEditorHeight,
leftEditorsMode,
theme,
} = this.props;
// FIXME: currentUser shouldn't return {} for spectator
const isPlayer = _.hasIn(players, currentUserId);
const editable = !isStoredGame && isPlayer;
const editorState = leftEditor;
const onChange = editable
? value => {
updateEditorValue(value);
}
: _.noop;
return {
onChange,
editable,
syntax: editorState.currentLangSlug || 'javascript',
value: editorState.text,
editorHeight: leftEditorHeight,
mode: editable ? leftEditorsMode : editorModes.default,
checkResult: editable ? checkResult : _.noop,
theme,
};
};
getRightEditorParams = () => {
const { rightEditor, rightEditorHeight } = this.props;
const editorState = rightEditor;
return {
onChange: _.noop,
editable: false,
mode: editorModes.default,
syntax: editorState.currentLangSlug || 'javascript',
value: editorState.text,
editorHeight: rightEditorHeight,
};
};
renderGameActionButtons = (editor, disabled) => <GameActionButtons disabled={disabled} editorUser={editor.userId} />;
getToolbarParams = editor => {
const { currentUserId, players, isStoredGame } = this.props;
const isPlayer = editor.userId === currentUserId;
return {
isSpectator: isStoredGame || !isPlayer,
player: players[editor.userId],
editor,
};
};
render() {
const {
isStoredGame, leftEditor, rightEditor, rightOutput,
} = this.props;
if (leftEditor === null || rightEditor === null) {
// FIXME: render loader
return null;
}
return (
<>
<div className="col-12 col-lg-6 p-1">
<div className="card " style={{ height: '470px' }} data-guide-id="LeftEditor">
<EditorToolbar
{...this.getToolbarParams(leftEditor)}
toolbarClassNames="btn-toolbar justify-content-between align-items-center m-1"
editorSettingClassNames="btn-group align-items-center m-1"
userInfoClassNames="btn-group align-items-center justify-content-end m-1"
/>
<div className="flex-grow-1">
<Editor {...this.getLeftEditorParams()} />
</div>
{/* TODO: move state to parent component */}
{!isStoredGame && this.renderGameActionButtons(leftEditor, false)}
</div>
</div>
<div className="col-12 col-lg-6 p-1">
<div className="card " style={{ height: '470px' }}>
<EditorToolbar
{...this.getToolbarParams(rightEditor)}
toolbarClassNames="btn-toolbar justify-content-between align-items-center m-1 flex-row-reverse"
editorSettingClassNames="btn-group align-items-center m-1 flex-row-reverse justify-content-end"
userInfoClassNames="btn-group align-items-center justify-content-end m-1 flex-row-reverse"
/>
<RightSide output={rightOutput}>
<Editor {...this.getRightEditorParams()} />
</RightSide>
</div>
</div>
<OutputClicker />
</>
);
}
}
const mapStateToProps = state => {
const leftEditor = selectors.leftEditorSelector(state);
const rightEditor = selectors.rightEditorSelector(state);
const leftUserId = _.get(leftEditor, ['userId'], null);
const rightUserId = _.get(rightEditor, ['userId'], null);
return {
currentUserId: selectors.currentUserIdSelector(state),
players: selectors.gamePlayersSelector(state),
leftEditor,
rightEditor,
leftEditorHeight: selectors.editorHeightSelector(leftUserId)(state),
rightEditorHeight: selectors.editorHeightSelector(rightUserId)(state),
rightOutput: selectors.rightExecutionOutputSelector(state),
leftEditorsMode: selectors.editorsModeSelector(leftUserId)(state),
theme: selectors.editorsThemeSelector(leftUserId)(state),
isStoredGame: selectors.gameStatusSelector(state).status === GameStatusCodes.stored,
};
};
const mapDispatchToProps = {
updateEditorValue: GameActions.sendEditorText,
checkResult: GameActions.checkGameResult,
};
export default connect(mapStateToProps, mapDispatchToProps)(GameWidget);