From 53eecd02502b31dfe5d2414b9562db7e94f6e392 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Thu, 24 Oct 2024 17:53:49 +0200 Subject: [PATCH] [#205] Resolve async setState issue --- src/components/record/RecordController.jsx | 34 ++++++++++------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/components/record/RecordController.jsx b/src/components/record/RecordController.jsx index 51dabed4..881effad 100644 --- a/src/components/record/RecordController.jsx +++ b/src/components/record/RecordController.jsx @@ -130,37 +130,35 @@ class RecordController extends React.Component { }; _onComplete = () => { - this._handlePhaseChange(RECORD_PHASE.COMPLETED); - this._transitionToRecords(); + this._handlePhaseChange(RECORD_PHASE.COMPLETED, () => { + this._handleRejectMessage(null, () => { + this.props.updateRecord(this.state.record, this.props.currentUser); + this._transitionToRecords(); + }); + }); }; _onReject = (reason) => { - this._handlePhaseChange(RECORD_PHASE.REJECTED); - this._handleRejectMessage(reason); - this._transitionToRecords(); + this._handlePhaseChange(RECORD_PHASE.REJECTED, () => { + this._handleRejectMessage(reason, () => { + this.props.updateRecord(this.state.record, this.props.currentUser); + this._transitionToRecords(); + }); + }); }; - _handleRejectMessage(reason) { - const currentUser = this.props.currentUser; + _handleRejectMessage(reason, callback) { const update = { ...this.state.record, rejectMessage: reason }; - console.log(this.state.record); - this.setState({ record: update }); - - this.props.updateRecord(update, currentUser); + this.setState({ record: update }, callback); } - _handlePhaseChange = (newPhase) => { - const currentUser = this.props.currentUser; + _handlePhaseChange = (newPhase, callback) => { const update = { ...this.state.record, phase: newPhase }; - - this.setState({ record: update }); - - this.props.updateRecord(update, currentUser); + this.setState({ record: update }, callback); }; _getLocalName() { if (EXTENSIONS.split(",").includes("kodi")) { - // return name of the record based on answer of specific question return this._getKodiLocaLName(); } return "record-" + Date.now();