import React from 'react';
import ClientActions from '../../actions/ClientActions';
import Dropdown from '../forms/Dropdown';
import EventTypes from '../../constants/EventTypes';
import format from '../../util/formatData';
import LimitsIcon from '../icons/Limits';
import SidebarItem from '../sidebar/SidebarItem';
import SettingsStore from '../../stores/SettingsStore';
import TransferDataStore from '../../stores/TransferDataStore';
const METHODS_TO_BIND = ['handleSettingsFetchRequestSuccess', 'onTransferDataRequestSuccess'];
const SPEEDS = [1024, 10240, 102400, 512000, 1048576, 2097152, 5242880, 10485760, 0];
class SpeedLimitDropdown extends React.Component {
constructor() {
super();
this.state = {
settings: {
speedLimits: {
download: [],
upload: []
}
},
throttle: null
};
METHODS_TO_BIND.forEach((method) => {
this[method] = this[method].bind(this);
});
}
componentDidMount() {
SettingsStore.listen(EventTypes.SETTINGS_FETCH_REQUEST_SUCCESS, this.handleSettingsFetchRequestSuccess);
SettingsStore.fetchSettings(EventTypes.SETTINGS_FETCH_REQUEST_ERROR, this.handleSettingsFetchRequestError);
TransferDataStore.listen(
EventTypes.CLIENT_TRANSFER_DATA_REQUEST_SUCCESS,
this.onTransferDataRequestSuccess
);
TransferDataStore.fetchTransferData();
}
componentWillUnmount() {
SettingsStore.unlisten(EventTypes.SETTINGS_FETCH_REQUEST_SUCCESS, this.handleSettingsFetchRequestSuccess);
TransferDataStore.unlisten(
EventTypes.CLIENT_TRANSFER_DATA_REQUEST_SUCCESS,
this.onTransferDataRequestSuccess
);
}
onTransferDataRequestSuccess() {
this.setState({
throttle: TransferDataStore.getThrottles({latest: true})
});
}
getDropdownHeader() {
return (
Speed Limits
);
}
getHumanReadableSpeed(bytes) {
if (bytes === 0) {
return 'Unlimited';
} else {
let formattedData = format.data(bytes, '/s', 0);
return (
{formattedData.value}
{formattedData.unit}
);
}
}
getSpeedList(property) {
let heading = {
className: `dropdown__label dropdown__label--${property}`,
displayName: `${property.charAt(0).toUpperCase()}${property.slice(1)}`,
selectable: false,
value: null
};
let insertCurrentThrottle = true;
let currentThrottle = this.state.throttle;
let items = this.state.settings.speedLimits[property].map((bytes) => {
let selected = false;
bytes = Number(bytes);
// Check if the current throttle setting exists in the preset speeds list.
// Determine if we need to add the current throttle setting to the menu.
if (currentThrottle && currentThrottle[property] === bytes) {
selected = true;
insertCurrentThrottle = false;
}
return {
displayName: this.getHumanReadableSpeed(bytes),
property,
selected,
selectable: true,
value: bytes
};
});
// If the current throttle setting doesn't exist in the pre-set speeds list,
// add it and show it as selected.
if (insertCurrentThrottle && currentThrottle) {
// Find the position to insert the current throttle setting so that it
// remains sorted from lowest to highest.
let insertionPoint = _.sortedIndex(SPEEDS, currentThrottle[property]);
items.splice(insertionPoint, 0, {
displayName: this.getHumanReadableSpeed(currentThrottle[property]),
property: property,
selected: true,
selectable: true,
value: currentThrottle[property]
});
}
items.unshift(heading);
return items;
}
getDropdownMenus() {
return [this.getSpeedList('download'), this.getSpeedList('upload')];
}
handleItemSelect(data) {
ClientActions.setThrottle(data.property, data.value);
}
handleSettingsFetchRequestSuccess() {
this.setState({
settings: SettingsStore.getSettings()
});
}
render() {
// return hi;
return (
);
}
}
export default SpeedLimitDropdown;