<commit_before>import R from 'ramda';
import React from 'react';
import { PopupForm } from 'xod-client';

import { ENUMERATING_PORTS, ENUMERATING_BOARDS, NO_PORTS_FOUND } from '../../shared/messages';

class PopupUploadConfig extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      isVisible: props.isVisible,
      selectedBoard: null,
      boards: null,
      ports: null,
    };

    this.onClose = this.onClose.bind(this);
    this.onBoardChanged = this.onBoardChanged.bind(this);
    this.onPortChanged = this.onPortChanged.bind(this);
    this.onRefreshPortsClicked = this.onRefreshPortsClicked.bind(this);
    this.onUploadClicked = this.onUploadClicked.bind(this);

    this.changeBoard = this.changeBoard.bind(this);
    this.changePort = this.changePort.bind(this);
  }

  componentDidMount() {
    this.getSelectedBoard()
      .then(selectedBoard => this.getBoards(selectedBoard));
    this.getPorts();
  }

  onClose() {
    this.props.onClose();
  }

  onBoardChanged(event) {
    this.changeBoard(event.target.value);
  }

  onPortChanged(event) {
    const selectedPort = R.find(R.propEq('comName', event.target.value), this.state.ports);
    this.changePort(selectedPort);
  }

  onRefreshPortsClicked() {
    this.getPorts();
  }

  onUploadClicked() {
    this.props.onUpload(this.state.selectedBoard, this.props.selectedPort);
  }

  getBoards(selectedBoard = this.state.selectedBoard) {
    const isBoardSelected = (selectedBoard !== null);

    this.props.listBoards()
      .then(R.tap(boards => this.setState({ boards })))
      .then((boards) => {
        const doesSelectedBoardExist = (
          isBoardSelected && R.contains(selectedBoard, boards)
        );
        const defaultBoardIndex = R.compose(
          R.defaultTo(0),
          R.findIndex(R.propEq('board', 'Arduino/Genuino Uno'))
        )(boards);

        if (!isBoardSelected || !doesSelectedBoardExist) {
          this.changeBoard(defaultBoardIndex);
        }
      });
  }

  getPorts() {
    this.props.listPorts()
      .then(R.tap(ports => this.setState({ ports })))
      .then((ports) => {
        const hasSelectedPort = R.contains(this.props.selectedPort, ports);
        const defaultPort = (ports && ports.length > 0) ? ports[0] : null;
        const defaultPreferredPort = R.compose(
          R.defaultTo(defaultPort),
          R.find(R.compose(
            R.test(/^(\/dev\/ttyUSB|\/dev\/tty.usb|\/dev\/cu.usb|\/dev\/ttyACM)/i),
            R.prop('comName')
          ))
        )(ports);

        if (!hasSelectedPort) {
          this.changePort(defaultPreferredPort);<commit_msg>tweak(xod-client-electron): catch errorCode NO_PORTS_FOUND to show message in select box<commit_after>import R from 'ramda';
import React from 'react';
import { PopupForm } from 'xod-client';

import { NO_PORTS_FOUND as NO_PORTS_FOUND_ERRCODE } from '../../shared/errorCodes';
import { ENUMERATING_PORTS, ENUMERATING_BOARDS, NO_PORTS_FOUND } from '../../shared/messages';

class PopupUploadConfig extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      isVisible: props.isVisible,
      selectedBoard: null,
      boards: null,
      ports: null,
    };

    this.onClose = this.onClose.bind(this);
    this.onBoardChanged = this.onBoardChanged.bind(this);
    this.onPortChanged = this.onPortChanged.bind(this);
    this.onRefreshPortsClicked = this.onRefreshPortsClicked.bind(this);
    this.onUploadClicked = this.onUploadClicked.bind(this);

    this.changeBoard = this.changeBoard.bind(this);
    this.changePort = this.changePort.bind(this);
  }

  componentDidMount() {
    this.getSelectedBoard()
      .then(selectedBoard => this.getBoards(selectedBoard));
    this.getPorts();
  }

  onClose() {
    this.props.onClose();
  }

  onBoardChanged(event) {
    this.changeBoard(event.target.value);
  }

  onPortChanged(event) {
    const selectedPort = R.find(R.propEq('comName', event.target.value), this.state.ports);
    this.changePort(selectedPort);
  }

  onRefreshPortsClicked() {
    this.getPorts();
  }

  onUploadClicked() {
    this.props.onUpload(this.state.selectedBoard, this.props.selectedPort);
  }

  getBoards(selectedBoard = this.state.selectedBoard) {
    const isBoardSelected = (selectedBoard !== null);

    this.props.listBoards()
      .then(R.tap(boards => this.setState({ boards })))
      .then((boards) => {
        const doesSelectedBoardExist = (
          isBoardSelected && R.contains(selectedBoard, boards)
        );
        const defaultBoardIndex = R.compose(
          R.defaultTo(0),
          R.findIndex(R.propEq('board', 'Arduino/Genuino Uno'))
        )(boards);

        if (!isBoardSelected || !doesSelectedBoardExist) {
          this.changeBoard(defaultBoardIndex);
        }
      });
  }

  getPorts() {
    this.props.listPorts()
      .catch(err => (
        (err.errorCode === NO_PORTS_FOUND_ERRCODE) ? [] : Promise.reject(err)
      ))
      .then(R.tap(ports => this.setState({ ports })))
      .then((ports) => {
        const hasSelectedPort = R.contains(this.props.selectedPort, ports);
        const defaultPort = (ports && ports.length > 0) ? ports[0] : null;
        const defaultPreferredPort = R.compose(
          R.defaultTo(defaultPort),
          R.find(R.compose(
            R.test(/^(\/dev\/ttyUSB|\/dev\/tty.usb|\/dev\/cu.usb|\/dev\/ttyACM)/i),
            R.prop('comName')
          ))
        )(ports);

        if (!hasSelectedPort) {
          this.changePort(defaultPreferredPort);
{'fraction_non_alphanumeric': 0.14360549132947978, 'fraction_numerical': 0.001083815028901734, 'mean_word_length': 2.9020436927413673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '1567021', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1773, 'n_words': 266}