From faefe1955ef6a53421299ebd5fbf792bef4d041b Mon Sep 17 00:00:00 2001 From: Max Chopart Date: Tue, 19 Mar 2024 14:21:09 +0100 Subject: [PATCH] [Upd #101] Refactor for readability --- src/components/record/Record.jsx | 54 ++++++++++++-------------------- 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/src/components/record/Record.jsx b/src/components/record/Record.jsx index df2ad061..5b2e6fa9 100644 --- a/src/components/record/Record.jsx +++ b/src/components/record/Record.jsx @@ -61,27 +61,22 @@ class Record extends React.Component { this.setState({ form }); }; + _filterQuestionsBySeverity = (severity) => { + return filterObjectsByKeyValuePair(this.getFormQuestionsData(), SConstants.HAS_VALIDATION_SEVERITY, severity); + }; + _handleOnSave = () => { const { form } = this.state; if (form) { this.validateForm(); - this.setState( - { - invalidQuestions: filterObjectsByKeyValuePair( - this.getFormQuestionsData(), - SConstants.HAS_VALIDATION_SEVERITY, - "error", - ), - }, - () => { - const { invalidQuestions } = this.state; - if (invalidQuestions.length > 0) { - this.setState({ showModal: true }); - } else { - this.props.handlers.onSave(); - } - }, - ); + const invalidQuestions = this._filterQuestionsBySeverity("error"); + this.setState({ invalidQuestions }, () => { + if (invalidQuestions.length > 0) { + this.setState({ showModal: true }); + } else { + this.props.handlers.onSave(); + } + }); } }; @@ -89,23 +84,14 @@ class Record extends React.Component { const { form } = this.state; if (form) { this.validateForm(); - this.setState( - { - incompleteQuestions: filterObjectsByKeyValuePair( - this.getFormQuestionsData(), - SConstants.HAS_VALIDATION_SEVERITY, - "warning", - ), - }, - () => { - const { incompleteQuestions } = this.state; - if (incompleteQuestions.length > 0) { - this.setState({ showModal: true }); - } else { - this.props.handlers.onComplete(); - } - }, - ); + const incompleteQuestions = this._filterQuestionsBySeverity("warning"); + this.setState({ incompleteQuestions }, () => { + if (incompleteQuestions.length > 0) { + this.setState({ showModal: true }); + } else { + this.props.handlers.onComplete(); + } + }); } };