Skip to content

Commit

Permalink
[battery_manager] Fixing testing bugs (#7044)
Browse files Browse the repository at this point in the history
Fixes bugs validation bugs and console errors found in battery manager during the testing round.

    Resolves #6957
    Resolves #6958
    Resolves #6955
  • Loading branch information
HenriRabalais authored Nov 27, 2020
1 parent 3d4cb5d commit a29242f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
6 changes: 4 additions & 2 deletions jsx/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ class Modal extends Component {

const submitButton = () => {
if (onSubmit) {
const submit = () => onSubmit().then(() => this.props.onClose())
.catch(() => {});
return (
<div style={submitStyle}>
<ButtonElement
label="Submit"
onUserInput={() => onSubmit().then(() => this.props.onClose())}
onUserInput={submit}
/>
</div>
);
Expand Down Expand Up @@ -169,7 +171,7 @@ class Modal extends Component {

Modal.propTypes = {
title: PropTypes.string,
onSubmit: PropTypes.object,
onSubmit: PropTypes.func,
onClose: PropTypes.func.isRequired,
show: PropTypes.bool.isRequired,
throwWarning: PropTypes.bool,
Expand Down
24 changes: 16 additions & 8 deletions modules/battery_manager/jsx/batteryManagerIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class BatteryManagerIndex extends Component {
return new Promise((resolve, reject) => {
return fetch(url, {credentials: 'same-origin', method: method})
.then((resp) => resp.json())
.then((data) => this.setState({[state]: data}, resolve()))
.then((data) => this.setState({[state]: data}, resolve))
.catch((error) => {
this.setState({error: true}, reject());
this.setState({error: true}, reject);
console.error(error);
});
});
Expand Down Expand Up @@ -211,7 +211,7 @@ class BatteryManagerIndex extends Component {
* Close the Form
*/
closeForm() {
this.setState({add: false, edit: false, test: {}});
this.setState({add: false, edit: false, test: {}, errors: {}});
}

/**
Expand Down Expand Up @@ -254,10 +254,10 @@ class BatteryManagerIndex extends Component {
this.checkDuplicate(test)
.then((test) => this.validateTest(test))
.then((test) => this.postData(
this.props.testEndpoint+test.id,
this.props.testEndpoint+(test.id || ''),
test,
request)
)
request
))
.then(() => this.fetchData(this.props.testEndpoint, 'GET', 'tests'))
.then(() => resolve())
.catch((e) => reject(e));
Expand Down Expand Up @@ -470,18 +470,26 @@ class BatteryManagerIndex extends Component {
}
if (test.ageMinDays == null) {
errors.ageMinDays = 'This field is required';
} else if (test.ageMinDays < 0) {
errors.ageMinDays = 'This field must be 0 or greater';
}
if (test.ageMaxDays == null) {
errors.ageMaxDays = 'This field is required';
} else if (test.ageMaxDays < 0) {
errors.ageMaxDays = 'This field must be 0 or greater';
}
if (test.ageMinDays > test.ageMaxDays) {
errors.ageMinDays = 'Minimum age must be lesser than maximum age.';
errors.ageMaxDays = 'Maximum age must be greater than minimum age.';
}
if (test.stage == null) {
errors.stage = 'This field is required';
}

if (Object.entries(errors).length === 0) {
this.setState({errors}, resolve(test));
this.setState({errors}, () => resolve(test));
} else {
this.setState({errors}, reject());
this.setState({errors}, reject);
}
});
}
Expand Down
15 changes: 15 additions & 0 deletions modules/battery_manager/php/testendpoint.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,27 @@ class TestEndpoint implements RequestHandlerInterface
if (!isset($test->row['ageMinDays'])) {
$errors[] = 'Minimum age is a required field.';
}
if (isset($test->row['ageMinDays']) && $test->row['ageMinDays'] < 0) {
$errors[] = 'Minimum age must be greater than 0.';
}
if (!isset($test->row['ageMaxDays'])) {
$errors[] = 'Maximum age is a required field.';
}
if (isset($test->row['ageMaxDays']) && $test->row['ageMaxDays'] < 0) {
$errors[] = 'Maximum age must be greater than 0.';
}
if ($test->row['ageMaxDays'] < $test->row['ageMinDays']) {
$errors[] = 'Maximum age must be greater than minimum age.';
}
if (!isset($test->row['stage'])) {
$errors[] = 'Stage is a required field.';
}
if (isset($test->row['instrumentOrder'])
&& ($test->row['instrumentOrder'] > 127
|| $test->row['instrumentOrder'] < -127)
) {
$errors[] = 'Instrument order must be between -127 and 127';
}

return $errors;
}
Expand Down

0 comments on commit a29242f

Please sign in to comment.