From cb18ec19fe24b2a609ef046a6c9df8a875c06382 Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 25 Jun 2022 17:46:07 +0800 Subject: [PATCH] ui: simplify scratchpad countdown logic (#373) --- .../scratchpad/ScratchpadToolbarContainer.jsx | 28 ++++--------------- .../components/scratchpad/reducers/ui.ts | 16 +++++++---- packages/ui-default/package.json | 2 +- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/packages/ui-default/components/scratchpad/ScratchpadToolbarContainer.jsx b/packages/ui-default/components/scratchpad/ScratchpadToolbarContainer.jsx index 84739631..e6cf016c 100644 --- a/packages/ui-default/components/scratchpad/ScratchpadToolbarContainer.jsx +++ b/packages/ui-default/components/scratchpad/ScratchpadToolbarContainer.jsx @@ -20,6 +20,7 @@ const mapStateToProps = (state) => ({ isPosting: state.ui.isPosting, isRunning: state.pretest.isRunning, isWaiting: state.ui.isWaiting, + waitSec: state.ui.waitSec, editorLang: state.editor.lang, editorCode: state.editor.code, pretestInput: state.pretest.input, @@ -66,10 +67,9 @@ const mapDispatchToProps = (dispatch) => ({ payload: request.get(UiContext.getSubmissionsUrl), }); }, - endWaiting() { + tick() { dispatch({ - type: 'SCRATCHPAD_WAITING_END', - payload: 0, + type: 'SCRATCHPAD_WAITING_TICK', }); }, }); @@ -84,7 +84,6 @@ export default connect(mapStateToProps, mapDispatchToProps)(class ScratchpadTool 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); @@ -97,18 +96,7 @@ export default connect(mapStateToProps, mapDispatchToProps)(class ScratchpadTool } 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); - } - } + if (this.props.waitSec > 0) setTimeout(() => this.props.tick(), 1000); } render() { @@ -131,9 +119,7 @@ export default connect(mapStateToProps, mapDispatchToProps)(class ScratchpadTool {' '} {i18n('Run Pretest')} {' '} - (F9) - {' '} - {this.props.isWaiting && `(${this.state.waitSec}s)`} + {this.props.isWaiting ? `(${this.props.waitSec}s)` : '(F9)'} )} 1, + }; + } case 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS_PENDING': { return { ...state, @@ -108,12 +118,6 @@ export default function reducer(state = { }, }; } - case 'SCRATCHPAD_WAITING_END': { - return { - ...state, - isWaiting: false, - }; - } default: return state; } diff --git a/packages/ui-default/package.json b/packages/ui-default/package.json index a8020e80..e129ca77 100644 --- a/packages/ui-default/package.json +++ b/packages/ui-default/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/ui-default", - "version": "4.37.21", + "version": "4.37.22", "author": "undefined ", "license": "AGPL-3.0", "main": "hydro.js",