<commit_before>import styles from '../styles/connectorManagement.styl'

import React, { Component } from 'react'

import Modal from 'cozy-ui/react/Modal'
import ModalContent from 'cozy-ui/react/Modal/Content'
import AccountConnection from './AccountConnection'
import Notifier from '../components/Notifier'

const AUTHORIZED_DATATYPE = require('config/datatypes')
const isValidType = (type) => AUTHORIZED_DATATYPE.indexOf(type) !== -1

import stylesLoginForm from '../styles/accountLoginForm'

export default class ConnectorManagement extends Component {
  constructor (props, context) {
    super(props, context)
    this.store = this.context.store
    const {t} = context
    const connector = this.store.find(c => c.slug === props.params.connectorSlug)
    this.store.subscribeTo(
      connector.id,
      refreshedConnector => this.setState({
        connector: refreshedConnector,
        isInstalled: this.isInstalled(refreshedConnector)
      })
    )
    const { name, fields } = connector

    this.state = {
      connector: this.sanitize(connector),
      isConnected: connector.accounts.length !== 0 && !connector.accounts.error,
      isInstalled: this.isInstalled(connector),
      isWorking: true,
      selectedAccount: 0,
      fields: this.configureFields(fields, context.t, name),
      submitting: false,
      synching: false,
      deleting: false,
      error: null,
      isClosing: false
    }

    this.store.fetchKonnectorInfos(props.params.connectorSlug)
      .then(konnector => {
        return this.store
          .fetchAccounts(props.params.connectorSlug)
          .then(accounts => {
            const error = konnector.accounts.error

            konnector.accounts = accounts
            // do not loose previous connector attributes
            this.setState({
              connector: Object.assign(konnector, this.state.connector),
              isConnected: konnector.accounts.length !== 0 && !error,
              isWorking: false,
              error
            })
          })
      })
      .catch(error => {
        Notifier.error(t(error.message || error))
        this.gotoParent()
      })
  }

  isInstalled (connector) {
    return connector.state != null && connector.state === 'ready'
  }

  render () {
    const { accounts } = this.state.connector
    const { selectedAccount, isWorking } = this.state
    const { t } = this.context

    return (
      <Modal secondaryAction={() => this.gotoParent()}>
        <ModalContent>
          {isWorking
            ? <div className={styles['installing']}>
              <div className={styles['installing-spinner']} />
              <div>{t('loading.working')}</div>
            </div>
            : <AccountConnection
              existingAccount={accounts.length ? accounts[selectedAccount] : null}
              alertSuccess={(messages) => this.alertSuccess(messages)}
              onCancel={() => this.gotoParent()}
              isUnloading={this.state.isClosing}
              {...this.state}
              {...this.context} />
          }
        </ModalContent>
      </Modal>
    )
  }

  alertSuccess (messages) {
    const { t } = this.context

    Notifier.info([
      messages.map(item => {
        return t(item.message, item.params)
      }).join('.\n')
    ])

    this.gotoParent()
  }

  gotoParent () {
    this.setState({isClosing: true})

    // The setTimeout allows React to perform setState related actions
    setTimeout(() => {
      const router = this.context.router
      let url = router.location.pathname
      router.push(url.substring(0, url.lastIndexOf('/')))
      console.log(document.querySelector(`.${stylesLoginForm['account-form-login']} input`).value)
    }, 0)
  }

  selectAccount (idx) {
    this.setState({ selectedAccount: idx })
  }

  sanitize (connector) {
    // remove invalid dataType declaration
    return Object.assign({}, connector,
      {
        dataType: connector.dataType.filter(isValidType)
      }
    )
  }

  // Set default values for advanced fields that will not be shown
  // on the initial connection form
  configureFields (fields, t, connectorName) {
    if (fields.calendar && !fields.calendar.default) {
      fields.calendar.default = connectorName
    }
    if (fields.folderPath && !fields.folderPath.options) {
      fields.folderPath.options = this.store.folders.map(f => f.path + '/' + f.name)
      fields.folderPath.folders = this.store.folders
    }
    if (!fields.frequency) {
      fields.frequency = {
        type: 'text',<commit_msg>fix: forgot to remove a console.log<commit_after>import styles from '../styles/connectorManagement.styl'

import React, { Component } from 'react'

import Modal from 'cozy-ui/react/Modal'
import ModalContent from 'cozy-ui/react/Modal/Content'
import AccountConnection from './AccountConnection'
import Notifier from '../components/Notifier'

const AUTHORIZED_DATATYPE = require('config/datatypes')
const isValidType = (type) => AUTHORIZED_DATATYPE.indexOf(type) !== -1

import stylesLoginForm from '../styles/accountLoginForm'

export default class ConnectorManagement extends Component {
  constructor (props, context) {
    super(props, context)
    this.store = this.context.store
    const {t} = context
    const connector = this.store.find(c => c.slug === props.params.connectorSlug)
    this.store.subscribeTo(
      connector.id,
      refreshedConnector => this.setState({
        connector: refreshedConnector,
        isInstalled: this.isInstalled(refreshedConnector)
      })
    )
    const { name, fields } = connector

    this.state = {
      connector: this.sanitize(connector),
      isConnected: connector.accounts.length !== 0 && !connector.accounts.error,
      isInstalled: this.isInstalled(connector),
      isWorking: true,
      selectedAccount: 0,
      fields: this.configureFields(fields, context.t, name),
      submitting: false,
      synching: false,
      deleting: false,
      error: null,
      isClosing: false
    }

    this.store.fetchKonnectorInfos(props.params.connectorSlug)
      .then(konnector => {
        return this.store
          .fetchAccounts(props.params.connectorSlug)
          .then(accounts => {
            const error = konnector.accounts.error

            konnector.accounts = accounts
            // do not loose previous connector attributes
            this.setState({
              connector: Object.assign(konnector, this.state.connector),
              isConnected: konnector.accounts.length !== 0 && !error,
              isWorking: false,
              error
            })
          })
      })
      .catch(error => {
        Notifier.error(t(error.message || error))
        this.gotoParent()
      })
  }

  isInstalled (connector) {
    return connector.state != null && connector.state === 'ready'
  }

  render () {
    const { accounts } = this.state.connector
    const { selectedAccount, isWorking } = this.state
    const { t } = this.context

    return (
      <Modal secondaryAction={() => this.gotoParent()}>
        <ModalContent>
          {isWorking
            ? <div className={styles['installing']}>
              <div className={styles['installing-spinner']} />
              <div>{t('loading.working')}</div>
            </div>
            : <AccountConnection
              existingAccount={accounts.length ? accounts[selectedAccount] : null}
              alertSuccess={(messages) => this.alertSuccess(messages)}
              onCancel={() => this.gotoParent()}
              isUnloading={this.state.isClosing}
              {...this.state}
              {...this.context} />
          }
        </ModalContent>
      </Modal>
    )
  }

  alertSuccess (messages) {
    const { t } = this.context

    Notifier.info([
      messages.map(item => {
        return t(item.message, item.params)
      }).join('.\n')
    ])

    this.gotoParent()
  }

  gotoParent () {
    this.setState({isClosing: true})

    // The setTimeout allows React to perform setState related actions
    setTimeout(() => {
      const router = this.context.router
      let url = router.location.pathname
      router.push(url.substring(0, url.lastIndexOf('/')))
    }, 0)
  }

  selectAccount (idx) {
    this.setState({ selectedAccount: idx })
  }

  sanitize (connector) {
    // remove invalid dataType declaration
    return Object.assign({}, connector,
      {
        dataType: connector.dataType.filter(isValidType)
      }
    )
  }

  // Set default values for advanced fields that will not be shown
  // on the initial connection form
  configureFields (fields, t, connectorName) {
    if (fields.calendar && !fields.calendar.default) {
      fields.calendar.default = connectorName
    }
    if (fields.folderPath && !fields.folderPath.options) {
      fields.folderPath.options = this.store.folders.map(f => f.path + '/' + f.name)
      fields.folderPath.folders = this.store.folders
    }
    if (!fields.frequency) {
      fields.frequency = {
        type: 'text',
{'fraction_non_alphanumeric': 0.12445682451253481, 'fraction_numerical': 0.0013370473537604457, 'mean_word_length': 2.4456813819577734, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '235650', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2535, 'n_words': 528}