| Conditions | 7 |
| Total Lines | 51 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import React, { Component } from 'react'; |
||
| 45 | fetchData = async () => { |
||
| 46 | try { |
||
| 47 | const response = await fetch( |
||
| 48 | `${APIBaseURL}/reports/spaceenvironmentmonitor?sensorid=${this.props.sensorId}&timerange=24h`, |
||
| 49 | { |
||
| 50 | method: 'GET', |
||
| 51 | headers: { |
||
| 52 | 'Content-type': 'application/json', |
||
| 53 | 'User-UUID': getCookieValue('user_uuid'), |
||
| 54 | 'Token': getCookieValue('token') |
||
| 55 | } |
||
| 56 | } |
||
| 57 | ); |
||
| 58 | |||
| 59 | if (!response.ok) throw new Error('Data fetch failed'); |
||
| 60 | const json = await response.json(); |
||
| 61 | |||
| 62 | const pointList = []; |
||
| 63 | const trendData = {}; |
||
| 64 | |||
| 65 | if (json['energy_value']) { |
||
| 66 | pointList.push({ |
||
| 67 | name: json['energy_value'].name, |
||
| 68 | value: json['energy_value'].values.length > 0 |
||
| 69 | ? json['energy_value'].values[json['energy_value'].values.length - 1] |
||
| 70 | : undefined |
||
| 71 | }); |
||
| 72 | trendData[json['energy_value'].name] = { |
||
| 73 | values: json['energy_value'].values, |
||
| 74 | timestamps: json['energy_value'].timestamps.map(ts => ts.substring(11, 16)) |
||
| 75 | }; |
||
| 76 | } |
||
| 77 | |||
| 78 | json['parameters']['names'].forEach((name, index) => { |
||
| 79 | const values = json['parameters']['values'][index]; |
||
| 80 | const timestamps = json['parameters']['timestamps'][index].map(ts => ts.substring(11, 16)); |
||
| 81 | |||
| 82 | pointList.push({ |
||
| 83 | name, |
||
| 84 | value: values.length > 0 ? values[values.length - 1] : undefined |
||
| 85 | }); |
||
| 86 | |||
| 87 | trendData[name] = { values, timestamps }; |
||
| 88 | }); |
||
| 89 | |||
| 90 | if (this._isMounted) { |
||
| 91 | this.setState({ pointList, trendData }); |
||
| 92 | } |
||
| 93 | } catch (err) { |
||
| 94 | console.error('Realtime data fetch error:', err); |
||
| 95 | toast.error(this.props.t(err.message || 'Data fetch failed')); |
||
| 96 | } |
||
| 208 | export default withTranslation()(RealtimeData); |