/* * 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(ns + 'statistics_includes_full')}
{_.map(lists, this.renderList)}{i18n(ns + 'statistics_user_info')}