<commit_before>/**
 * Copyright (c) 2017, Shopgate, Inc. All rights reserved.
 *
 * This source code is licensed under the Apache 2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import I18n from '@shopgate/pwa-common/components/I18n';
import Picker from 'Components/Picker';
import PriceDifference from './components/PriceDifference';
import connect from './connector';
import styles from './style';

const PICKER_CHANGE_DELAY = 300;

/**
 * The Product Options component.
 */
class Options extends Component {
  static propTypes = {
    setProductOption: PropTypes.func.isRequired,
    currentOptions: PropTypes.shape(),
    options: PropTypes.arrayOf(PropTypes.shape()),
  };

  static defaultProps = {
    currentOptions: {},
    options: null,
  };

  /**
   * Triggers setProductOptions when the component is mounted and has options set.
   */
  componentDidMount() {
    if(!this.props.setProductOption) {
      return;
    }

    this.props.options.forEach((option) => {
      // Only options of type 'select' have a default value. Type 'text' has no default.
      if (option.type !== 'select') {
        return;
      }

      this.props.setProductOption(option.id, option.items[0].value);
    });
  }

  /**
   * When the component receives the product options
   * it will set the first value of each option as active
   * @param {Object} nextProps The incoming props.
   */
  componentWillReceiveProps(nextProps) {
    if (!this.props.options && nextProps.options) {
      nextProps.options.forEach((option) => {
        // Only options of type 'select' have a default value. Type 'text' has no default.
        if (option.type !== 'select') {
          return;
        }

        this.props.setProductOption(option.id, option.items[0].value);
      });
    }
  }
<commit_msg>CON-2376 Fixed Options error on mount<commit_after>/**
 * Copyright (c) 2017, Shopgate, Inc. All rights reserved.
 *
 * This source code is licensed under the Apache 2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import I18n from '@shopgate/pwa-common/components/I18n';
import Picker from 'Components/Picker';
import PriceDifference from './components/PriceDifference';
import connect from './connector';
import styles from './style';

const PICKER_CHANGE_DELAY = 300;

/**
 * The Product Options component.
 */
class Options extends Component {
  static propTypes = {
    setProductOption: PropTypes.func.isRequired,
    currentOptions: PropTypes.shape(),
    options: PropTypes.arrayOf(PropTypes.shape()),
  };

  static defaultProps = {
    currentOptions: {},
    options: null,
  };

  /**
   * Triggers setProductOptions when the component is mounted and has options set.
   */
  componentDidMount() {
    if(!this.props.setProductOption || !this.props.options) {
      return;
    }

    this.props.options.forEach((option) => {
      // Only options of type 'select' have a default value. Type 'text' has no default.
      if (option.type !== 'select') {
        return;
      }

      this.props.setProductOption(option.id, option.items[0].value);
    });
  }

  /**
   * When the component receives the product options
   * it will set the first value of each option as active
   * @param {Object} nextProps The incoming props.
   */
  componentWillReceiveProps(nextProps) {
    if (!this.props.options && nextProps.options) {
      nextProps.options.forEach((option) => {
        // Only options of type 'select' have a default value. Type 'text' has no default.
        if (option.type !== 'select') {
          return;
        }

        this.props.setProductOption(option.id, option.items[0].value);
      });
    }
  }

{'fraction_non_alphanumeric': 0.11914011914011914, 'fraction_numerical': 0.008806008806008805, 'mean_word_length': 3.0439790575916232, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3508843', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1091, 'n_words': 344}