import React, { Component } from 'react'; import { Row, Col, ListGroup, ListGroupItem } from 'reactstrap'; import Resistor from './resistor'; class Result extends Component { state = {}; constructor(props) { super(props); this.state = { series: this.props.series, factors: this.props.factors, powerSource: this.props.powerSource, powerLed: this.props.powerLed, ledConsumption: this.props.ledConsumption, colors: this.props.colors, perfectMatch: null, // First resistor before the perfect value underR: null, underRColors: [ 'red', 'red' ], underRRingTwo: { color: 'red' }, underRFactor: null, // First resistor after the perfect value overR: null, overRRingOne: { color: 'red' }, overRRingTwo: { color: 'red' }, overRFactor: null, }; } componentDidMount = () => { const perfectMatch = Math.round( 1000 * ((this.state.powerSource - this.state.powerLed ) / ( this.props.ledConsumption/1000))) / 1000; let overR = null; let underR = null; let underRFactor = null; let overRFactor = null; let underRColors = []; let overRColors = []; if ( perfectMatch > 0 ) { for( let iFactor = 0 ; iFactor < this.state.factors.length ; iFactor += 1 ) { const factorDetails = this.state.factors[iFactor]; const factor = factorDetails.factor; for( let iSerie = 0 ; iSerie < this.state.series.e24.length ; iSerie += 1 ) { const currentR = this.state.series.e24[iSerie] * factor; if ( currentR >= perfectMatch ) { overR = currentR; overRFactor = factorDetails; break; } underR = currentR; } underRFactor = factorDetails; if ( overR) { break; } } if ( overR === perfectMatch ){ underR = null; overR = null; underRFactor = null; overRFactor = null; } } // Extract colors from resistors if ( underR ) { for ( let i = 0 ; i < 2 ; i += 1) { for ( let j = 0 ; j < this.state.colors.length ; j += 1) { if ( this.state.colors[j].value === Number(underR.toString()[i]) ) { underRColors[i] = this.state.colors[j].color; break; } } } } if ( overR ) { for ( let i = 0 ; i < 2 ; i += 1) { for ( let j = 0 ; j < this.state.colors.length ; j += 1) { if ( this.state.colors[j].value === Number(overR.toString()[i]) ) { overRColors[i] = this.state.colors[j].color; break; } } } } this.setState({ perfectMatch, overR, overRFactor, underR, underRFactor, underRColors, overRColors }); } render() { return ( Vous devez mettre une résistance de {this.state.perfectMatch}Ω. { this.state.overR ? ( Cette résistance n'existant pas dans la série e24 vous pouvez opter pour l'une des résistances suivante : {this.state.underR ? ( {this.state.underR}Ω {' '} ({this.state.underR / this.state.underRFactor.factor} {this.state.underRFactor.factorLabel}Ω) ) : (null) } {this.state.overR ? ( {this.state.overR}Ω {' '} ({this.state.overR / this.state.overRFactor.factor} {this.state.overRFactor.factorLabel}Ω) ) : (null) } ) : (null) } ) } } export default Result