{"size": 1489, "ext": "js", "max_stars_repo_path": "lib/derivepass/containers/application.js", "max_stars_repo_name": "indutny/derivepass-electron", "max_stars_repo_stars_event_min_datetime": "2017-01-27T00:33:56.000Z", "max_stars_repo_stars_event_max_datetime": "2017-01-27T00:33:56.000Z", "max_issues_repo_path": "lib/derivepass/containers/application.js", "max_issues_repo_name": "indutny/derivepass-electron", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/derivepass/containers/application.js", "max_forks_repo_name": "indutny/derivepass-electron", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.8166666667, "max_line_length": 73, "alphanum_fraction": 0.6420416387}
'use strict';

const electron = require('electron');
const ReactRedux = require('react-redux');
const derivepass = require('../../derivepass');
const Application = derivepass.components.Application;
const actions = derivepass.redux.actions;

function mapStateToProps(state, ownProps) {
  const cryptor = ownProps.cryptor;

  const raw = ownProps.app;
  return {
    raw,
    master: state.master,
    app: raw === null ? null : {
      domain: cryptor.decrypt(raw.domain),
      login: cryptor.decrypt(raw.login),
      revision: cryptor.decryptNumber(raw.revision),
      index: raw.index
    }
  };
}

function mapDispatchToProps(dispatch, ownProps) {
  const cryptor = ownProps.cryptor;
  const raw = ownProps.app;

  return {
    copyPassword: (master, app, done) => {
      cryptor.derivePassword(master.password, app, (err, password) => {
        if (err)
          console.error(err);
        else
          electron.clipboard.writeText(password);

        done(null);
      });
    },
    onSave: (app) => {
      const encrypted = {
        domain: cryptor.encrypt(app.domain),
        login: cryptor.encrypt(app.login),
        revision: cryptor.encryptNumber(app.revision),

        changedAt: Date.now()
      };

      dispatch(actions.updateApplication(raw.uuid, encrypted));
    },
    onRemove: () => {
      dispatch(actions.removeApplication(raw.uuid, Date.now()));
    }
  };
}

module.exports = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(
  Application
);
