import React from 'react'; import SettingsTab from './SettingsTab'; const METHODS_TO_BIND = ['handleDownloadTextChange', 'handleUploadTextChange']; export default class BandwidthTab extends SettingsTab { constructor() { super(); this.state = { downloadValue: null, uploadValue: null }; METHODS_TO_BIND.forEach((method) => { this[method] = this[method].bind(this); }); } arrayToString(array) { return array.join(', '); } getTextboxValue(input = []) { if (Array.isArray(input)) { return this.arrayToString(input); } return input; } handleDownloadTextChange(event) { this.setState({ downloadValue: event.target.value }); this.props.onSettingsChange({ speedLimits: { download: this.processSpeedsForSave(event.target.value), upload: this.processSpeedsForSave(this.getUploadValue()) } }); } handleUploadTextChange(event) { this.setState({ uploadValue: event.target.value }); this.props.onSettingsChange({ speedLimits: { download: this.processSpeedsForSave(this.getDownloadValue()), upload: this.processSpeedsForSave(event.target.value) } }); } getDownloadValue() { let displayedValue = this.state.downloadValue; if (displayedValue == null && this.props.settings.speedLimits != null) { displayedValue = this.processSpeedsForDisplay(this.props.settings.speedLimits.download); } return displayedValue; } getUploadValue() { let displayedValue = this.state.uploadValue; if (displayedValue == null && this.props.settings.speedLimits != null) { displayedValue = this.processSpeedsForDisplay(this.props.settings.speedLimits.upload); } return displayedValue; } processSpeedsForDisplay(speeds = []) { if (!speeds || speeds.length === 0) { return; } return this.arrayToString(speeds.map((speed) => { return Number(speed) / 1024; })); } processSpeedsForSave(speeds = '') { if (speeds === '') { return []; } return this.stringToArray(speeds).map((speed) => { return Number(speed) * 1024; }); } stringToArray(string = '') { return string.replace(/\s/g, '').split(','); } render() { let downloadValue = this.getDownloadValue() || 0; let uploadValue = this.getUploadValue() || 0; return (

Speed Limit Dropdown Presets

Enter a comma-separated list of speeds in kB. 0 represents unlimited.

Slot Availability
); } }