componentWillReceiveProps()

in frontend/mx/src/containers/app.js [45:97]


  componentWillReceiveProps(nextProps) {
    if (
      this.props.devices.length !== nextProps.devices.length &&
      nextProps.devices.length > 0
    ) {
      this.setState({ selectedDeviceIdx: 0 });
    }

    const {
      selectedDeviceIdx,
      ruleThreshold,
      trigger,
      isThresholdExceeded,
    } = this.state;

    const device = nextProps.devices[selectedDeviceIdx];

    if (
      device &&
      !isThresholdExceeded &&
      typeof ruleThreshold === 'number' &&
      typeof selectedDeviceIdx === 'number'
    ) {
      let deviceTriggerValue;

      switch (trigger.name) {
        case TRIGGER_LABEL_HUMIDITY:
          deviceTriggerValue = device.humidity;
          break;
        case TRIGGER_LABEL_MAGNETISM:
          deviceTriggerValue = device.magnetometer;
          break;
        case TRIGGER_LABEL_MOVEMENT:
          deviceTriggerValue = device.gravity;
          break;
        case TRIGGER_LABEL_PRESSURE:
          deviceTriggerValue = device.pressure;
          break;
        case TRIGGER_LABEL_SOUND:
          deviceTriggerValue = device.decibels;
          break;
        case TRIGGER_LABEL_TEMPERATURE:
          deviceTriggerValue = device.temperature;
          break;
        default:
          //
          deviceTriggerValue = 0;
      }
      if (deviceTriggerValue > ruleThreshold) {
        this.setState({ isThresholdExceeded: true });
      }
    }
  }