| Conditions | 11 |
| Total Lines | 104 |
| Code Lines | 81 |
| 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:
Complex classes like RealtimeChart.componentDidMount often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import React, { Component } from 'react'; |
||
| 65 | |||
| 66 | componentDidMount() { |
||
| 67 | this._isMounted = true; |
||
| 68 | // fetch meter realtime data at the first time |
||
| 69 | let isResponseOK = false; |
||
| 70 | fetch(APIBaseURL + '/reports/meterrealtime?meterid=' + this.props.meterId, { |
||
| 71 | method: 'GET', |
||
| 72 | headers: { |
||
| 73 | "Content-type": "application/json", |
||
| 74 | "User-UUID": getCookieValue('user_uuid'), |
||
| 75 | "Token": getCookieValue('token') |
||
| 76 | }, |
||
| 77 | body: null, |
||
| 78 | |||
| 79 | }).then(response => { |
||
| 80 | if (response.ok) { |
||
| 81 | isResponseOK = true; |
||
| 82 | } |
||
| 83 | return response.json(); |
||
| 84 | }).then(json => { |
||
| 85 | if (isResponseOK) { |
||
| 86 | console.log(json); |
||
| 87 | const trendLog = json['energy_value']['values']; |
||
| 88 | const currentEnergyValue = undefined; |
||
| 89 | const pointList = []; |
||
| 90 | if (trendLog.length > 0) { |
||
| 91 | currentEnergyValue = trendLog[trendLog.length - 1]; |
||
| 92 | } |
||
| 93 | json['parameters']['names'].forEach((currentName, index) => { |
||
| 94 | let pointItem = {} |
||
| 95 | pointItem['name'] = currentName; |
||
| 96 | pointItem['value'] = undefined; |
||
| 97 | let currentValues = json['parameters']['values'][index]; |
||
| 98 | if (currentValues.length > 0) { |
||
| 99 | pointItem['value'] = currentValues[currentValues.length - 1]; |
||
| 100 | } |
||
| 101 | pointList.push(pointItem); |
||
| 102 | }); |
||
| 103 | if (this._isMounted) { |
||
| 104 | this.setState({ |
||
| 105 | trendLog: trendLog, |
||
| 106 | currentEnergyValue: currentEnergyValue, |
||
| 107 | pointList: pointList, |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | // todo |
||
| 111 | } else { |
||
| 112 | toast.error(json.description) |
||
| 113 | } |
||
| 114 | }).catch(err => { |
||
| 115 | console.log(err); |
||
| 116 | }); |
||
| 117 | |||
| 118 | //fetch meter realtime data at regular intervals |
||
| 119 | this.refreshInterval = setInterval(() => { |
||
| 120 | let isResponseOK = false; |
||
| 121 | fetch(APIBaseURL + '/reports/meterrealtime?meterid=' + this.props.meterId, { |
||
| 122 | method: 'GET', |
||
| 123 | headers: { |
||
| 124 | "Content-type": "application/json", |
||
| 125 | "User-UUID": getCookieValue('user_uuid'), |
||
| 126 | "Token": getCookieValue('token') |
||
| 127 | }, |
||
| 128 | body: null, |
||
| 129 | |||
| 130 | }).then(response => { |
||
| 131 | if (response.ok) { |
||
| 132 | isResponseOK = true; |
||
| 133 | } |
||
| 134 | return response.json(); |
||
| 135 | }).then(json => { |
||
| 136 | if (isResponseOK) { |
||
| 137 | console.log(json); |
||
| 138 | const trendLog = json['energy_value']['values']; |
||
| 139 | const currentEnergyValue = undefined; |
||
| 140 | const pointList = []; |
||
| 141 | if (trendLog.length > 0) { |
||
| 142 | currentEnergyValue = trendLog[trendLog.length - 1]; |
||
| 143 | } |
||
| 144 | json['parameters']['names'].forEach((currentName, index) => { |
||
| 145 | let pointItem = {} |
||
| 146 | pointItem['name'] = currentName; |
||
| 147 | pointItem['value'] = undefined; |
||
| 148 | let currentValues = json['parameters']['values'][index]; |
||
| 149 | if (currentValues.length > 0) { |
||
| 150 | pointItem['value'] = currentValues[currentValues.length - 1]; |
||
| 151 | } |
||
| 152 | pointList.push(pointItem); |
||
| 153 | }); |
||
| 154 | if (this._isMounted) { |
||
| 155 | this.setState({ |
||
| 156 | trendLog: trendLog, |
||
| 157 | currentEnergyValue: currentEnergyValue, |
||
| 158 | pointList: pointList, |
||
| 159 | }); |
||
| 160 | } |
||
| 161 | // todo |
||
| 162 | } else { |
||
| 163 | toast.error(json.description) |
||
| 164 | } |
||
| 165 | }).catch(err => { |
||
| 166 | console.log(err); |
||
| 167 | }); |
||
| 168 | }, (60 + Math.floor(Math.random() * Math.floor(10))) * 1000); // use random interval to avoid paralels requests |
||
| 169 | } |
||
| 220 |