/* eslint-disable react/static-property-placement */ import _ from 'lodash'; import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import i18n from 'vj/utils/i18n'; import request from 'vj/utils/request'; import Icon from 'vj/components/react/IconComponent'; import getAvailableLangs from 'vj/utils/availableLangs'; import Toolbar, { ToolbarItemComponent as ToolbarItem, ToolbarButtonComponent as ToolbarButton, ToolbarSplitComponent as ToolbarSplit, } from './ToolbarComponent'; const mapStateToProps = (state) => ({ pretestVisible: state.ui.pretest.visible, recordsVisible: state.ui.records.visible, isPosting: state.ui.isPosting, isRunning: state.pretest.isRunning, isWaiting: state.ui.isWaiting, editorLang: state.editor.lang, editorCode: state.editor.code, pretestInput: state.pretest.input, }); const mapDispatchToProps = (dispatch) => ({ togglePanel(uiElement) { dispatch({ type: 'SCRATCHPAD_UI_TOGGLE_VISIBILITY', payload: { uiElement }, }); }, setEditorLanguage(lang) { dispatch({ type: 'SCRATCHPAD_EDITOR_SET_LANG', payload: lang, }); }, postPretest(props) { const req = request.post(UiContext.postSubmitUrl, { lang: props.editorLang, code: props.editorCode, input: props.pretestInput || ' ', pretest: true, }); dispatch({ type: 'SCRATCHPAD_POST_PRETEST', payload: req, }); }, postSubmit(props) { const req = request.post(UiContext.postSubmitUrl, { lang: props.editorLang, code: props.editorCode, }); dispatch({ type: 'SCRATCHPAD_POST_SUBMIT', payload: req, }); }, loadSubmissions() { dispatch({ type: 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS', payload: request.get(UiContext.getSubmissionsUrl), }); }, endWaiting() { dispatch({ type: 'SCRATCHPAD_WAITING_END', payload: 0, }); }, }); const availableLangs = getAvailableLangs(UiContext.pdoc.config.langs); const keys = Object.keys(availableLangs); export default connect(mapStateToProps, mapDispatchToProps)(class ScratchpadToolbarContainer extends React.PureComponent { static contextTypes = { store: PropTypes.object, }; constructor(props) { super(props); this.state = { waitSec: -1 }; if (!availableLangs[this.props.editorLang]) { // preference not allowed const key = keys.find((i) => availableLangs[i].pretest === this.props.editorLang); this.props.setEditorLanguage(key || keys[0]); } } componentDidMount() { if (this.props.recordsVisible) this.props.loadSubmissions(); } componentDidUpdate() { if (this.props.isWaiting) { const { waitSec } = this.state; if (waitSec < 0) this.setState({ waitSec: 5 }); if (waitSec === 0) { this.setState({ waitSec: waitSec - 1 }); this.props.endWaiting(); } else { setTimeout(() => { this.setState({ waitSec: waitSec - 1 }); }, 1000); } } } render() { let canUsePretest = ['default', 'fileio'].includes(UiContext.pdoc.config?.type); if (UiContext.pdoc.config?.type === 'remote_judge') { if (availableLangs[this.props.editorLang].pretest) canUsePretest = true; } if (availableLangs[this.props.editorLang]?.pretest === false) canUsePretest = false; return ( {canUsePretest && ( this.props.postPretest(this.props)} data-global-hotkey="f9" data-tooltip={`${i18n('Pretest Your Code')} (F9)`} > {' '} {i18n('Run Pretest')} {' '} (F9) {' '} {this.props.isWaiting && `(${this.state.waitSec}s)`} )} this.props.postSubmit(this.props)} data-global-hotkey="f10" data-tooltip={`${i18n('Submit Your Code')} (F10)`} > {' '} {i18n('Submit Solution')} {' '} (F10) {' '} {this.props.isWaiting && `(${this.state.waitSec}s)`} {' '} {i18n('Exit')} {' '} (Alt+Q) {canUsePretest && ( this.props.togglePanel('pretest')} data-global-hotkey="alt+p" data-tooltip={`${i18n('Toggle Pretest Panel')} (Alt+P)`} > {' '} {i18n('Pretest')} )} {UiContext.canViewRecord && ( this.props.togglePanel('records')} data-global-hotkey="alt+r" data-tooltip={`${i18n('Toggle Records Panel')} (Alt+R)`} > {' '} {i18n('Records')} )} ); } });