insomnia/app/ui/components/editors/auth/basic-auth.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-11-26 08:29:16 +00:00
import React, {PropTypes, PureComponent} from 'react';
import autobind from 'autobind-decorator';
import KeyValueEditor from '../../key-value-editor/editor';
import {trackEvent} from '../../../../analytics/index';
import {AUTH_BASIC} from '../../../../common/constants';
2016-04-09 21:08:55 +00:00
@autobind
class BasicAuth extends PureComponent {
_handleOnCreate () {
trackEvent('Basic Auth Editor', 'Create');
}
_handleOnDelete () {
trackEvent('Basic Auth Editor', 'Delete');
}
_handleToggleDisable (pair) {
2016-11-26 08:29:16 +00:00
const label = pair.disabled ? 'Disable' : 'Enable';
trackEvent('Basic Auth Editor', 'Toggle', label);
}
2016-11-26 08:29:16 +00:00
_handleChange (pairs) {
2016-11-26 08:29:16 +00:00
const pair = {
type: AUTH_BASIC,
2016-11-26 08:29:16 +00:00
username: pairs.length ? pairs[0].name : '',
password: pairs.length ? pairs[0].value : '',
disabled: pairs.length ? pairs[0].disabled : false
2016-11-26 08:29:16 +00:00
};
this.props.onChange(pair);
}
2016-11-26 08:29:16 +00:00
render () {
const {
authentication,
showPasswords,
handleRender,
handleGetRenderContext
} = this.props;
2016-11-26 08:29:16 +00:00
const pairs = [{
name: authentication.username || '',
value: authentication.password || '',
disabled: authentication.disabled || false
2016-11-26 08:29:16 +00:00
}];
return (
<KeyValueEditor
pairs={pairs}
maxPairs={1}
disableDelete
handleRender={handleRender}
handleGetRenderContext={handleGetRenderContext}
2016-11-26 08:29:16 +00:00
namePlaceholder="Username"
valuePlaceholder="•••••••••••"
2016-11-26 08:29:16 +00:00
valueInputType={showPasswords ? 'text' : 'password'}
onToggleDisable={this._handleToggleDisable}
onCreate={this._handleOnCreate}
onDelete={this._handleOnDelete}
onChange={this._handleChange}
/>
);
}
}
2016-04-09 21:08:55 +00:00
BasicAuth.propTypes = {
handleRender: PropTypes.func.isRequired,
handleGetRenderContext: PropTypes.func.isRequired,
2016-11-26 00:37:59 +00:00
handleUpdateSettingsShowPasswords: PropTypes.func.isRequired,
2016-04-09 21:08:55 +00:00
onChange: PropTypes.func.isRequired,
2016-11-26 00:37:59 +00:00
authentication: PropTypes.object.isRequired,
showPasswords: PropTypes.bool.isRequired
2016-04-09 21:08:55 +00:00
};
export default BasicAuth;