/* * Copyright 2014 Mirantis, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. **/ import _ from 'underscore'; import i18n from 'i18n'; import React from 'react'; import utils from 'utils'; import models from 'models'; import {Input, ProgressBar} from 'views/controls'; import {RegistrationDialog, RetrievePasswordDialog} from 'views/dialogs'; export default { propTypes: { settings: React.PropTypes.object.isRequired }, getDefaultProps() { return {statsCheckboxes: ['send_anonymous_statistic', 'send_user_info']}; }, getInitialState() { var tracking = this.props.settings.get('tracking'); return { isConnected: !!(tracking.email.value && tracking.password.value), actionInProgress: false, remoteLoginForm: new models.MirantisLoginForm(), registrationForm: new models.MirantisRegistrationForm(), remoteRetrievePasswordForm: new models.MirantisRetrievePasswordForm() }; }, setConnected() { this.setState({isConnected: true}); }, saveSettings(initialAttributes) { var settings = this.props.settings; this.setState({actionInProgress: true}); return settings.save(null, {patch: true, wait: true, validate: false}) .fail((response) => { if (initialAttributes) settings.set(initialAttributes); utils.showErrorDialog({response: response}); }) .always(() => { this.setState({actionInProgress: false}); }); }, prepareStatisticsToSave() { var currentAttributes = _.cloneDeep(this.props.settings.attributes); // We're saving only two checkboxes _.each(this.props.statsCheckboxes, function(field) { var path = this.props.settings.makePath('statistics', field, 'value'); this.props.settings.set(path, this.props.statistics.get(path)); }, this); return this.saveSettings(currentAttributes); }, prepareTrackingToSave(response) { var currentAttributes = _.cloneDeep(this.props.settings.attributes); // Saving user contact data to Statistics section _.each(response, function(value, name) { if (name != 'password') { var path = this.props.settings.makePath('statistics', name, 'value'); this.props.settings.set(path, value); this.props.tracking.set(path, value); } }, this); // Saving email and password to Tracking section _.each(this.props.tracking.get('tracking'), function(data, inputName) { var path = this.props.settings.makePath('tracking', inputName, 'value'); this.props.settings.set(path, this.props.tracking.get(path)); }, this); this.saveSettings(currentAttributes).done(this.setConnected); }, showResponseErrors(response) { var jsonObj, error = ''; try { jsonObj = JSON.parse(response.responseText); error = jsonObj.message; } catch (e) { error = i18n('welcome_page.register.connection_error'); } this.setState({error: error}); }, connectToMirantis() { this.setState({error: null}); var tracking = this.props.tracking.get('tracking'); if (this.props.tracking.isValid({models: this.configModels})) { var remoteLoginForm = this.state.remoteLoginForm; this.setState({actionInProgress: true}); _.each(tracking, (data, inputName) => { var name = remoteLoginForm.makePath('credentials', inputName, 'value'); remoteLoginForm.set(name, tracking[inputName].value); }); remoteLoginForm.save() .done(this.prepareTrackingToSave) .fail(this.showResponseErrors) .always(() => { this.setState({actionInProgress: false}); }); } }, checkRestrictions(name, action = 'disable') { return this.props.settings.checkRestrictions(this.configModels, action, this.props.settings.get('statistics').name); }, componentWillMount() { var model = this.props.statistics || this.props.tracking; this.configModels = { fuel_settings: model, version: app.version, default: model }; }, getError(model, name) { return (model.validationError || {})[model.makePath('statistics', name)]; }, getText(key) { if (_.contains(app.version.get('feature_groups'), 'mirantis')) return i18n(key); return i18n(key + '_community'); }, renderInput(settingName, wrapperClassName, disabledState) { var model = this.props.statistics || this.props.tracking, setting = model.get(model.makePath('statistics', settingName)); if (this.checkRestrictions('metadata', 'hide').result || this.checkRestrictions(settingName, 'hide').result || setting.type == 'hidden') return null; var error = this.getError(model, settingName), disabled = this.checkRestrictions('metadata').result || this.checkRestrictions(settingName).result || disabledState; return ; }, renderList(list, key) { return (
{i18n('statistics.' + key + '_title')}
); }, renderIntro() { var ns = 'statistics.', isMirantisIso = _.contains(app.version.get('feature_groups'), 'mirantis'), lists = { actions: [ 'operation_type', 'operation_time', 'actual_time', 'network_verification', 'ostf_results' ], settings: [ 'envronments_amount', 'distribution', 'network_type', 'kernel_parameters', 'admin_network_parameters', 'pxe_parameters', 'dns_parameters', 'storage_options', 'related_projects', 'modified_settings', 'networking_configuration' ], node_settings: [ 'deployed_nodes_amount', 'deployed_roles', 'disk_layout', 'interfaces_configuration' ], system_info: [ 'hypervisor', 'hardware_info', 'fuel_version', 'openstack_version' ] }; return (
{this.getText(ns + 'help_to_improve')}

{i18n(ns + 'statistics_includes_full')}

{_.map(lists, this.renderList)}

{i18n(ns + 'statistics_user_info')}

); }, onCheckboxChange(name, value) { var model = this.props.statistics || this.props.tracking; model.set(model.makePath('statistics', name, 'value'), value); }, onTrackingSettingChange(name, value) { this.setState({error: null}); var path = this.props.tracking.makePath('tracking', name); delete (this.props.tracking.validationError || {})[path]; this.props.tracking.set(this.props.tracking.makePath(path, 'value'), value); }, clearRegistrationForm() { if (!this.state.isConnected) { var tracking = this.props.tracking, initialData = this.props.settings.get('tracking'); _.each(tracking.get('tracking'), (data, name) => { var path = tracking.makePath('tracking', name, 'value'); tracking.set(path, initialData[name].value); }); tracking.validationError = null; } }, renderRegistrationForm(model, disabled, error, showProgressBar) { var tracking = model.get('tracking'), sortedFields = _.chain(_.keys(tracking)) .without('metadata') .sortBy((inputName) => tracking[inputName].weight) .value(); return (
{error &&
{error}
}
{showProgressBar && } {_.map(sortedFields, function(inputName) { return ; }, this)}
); }, showRegistrationDialog() { RegistrationDialog.show({ registrationForm: this.state.registrationForm, setConnected: this.setConnected, settings: this.props.settings, tracking: this.props.tracking, saveSettings: this.saveSettings }); }, showRetrievePasswordDialog() { RetrievePasswordDialog.show({ remoteRetrievePasswordForm: this.state.remoteRetrievePasswordForm }); } };