Skip to content

Commit 851d4d5

Browse files
committed
Introduce life-cycle methods to App
- Move reload after status update to componentDidUpdate - Move history.listen/unlisten to componentDidMount/componentWillUnmount
1 parent 2e91144 commit 851d4d5

2 files changed

Lines changed: 29 additions & 27 deletions

File tree

components/frontend/src/App.jsx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,27 @@ class App extends Component {
3737
email: null,
3838
snackBarMessages: [],
3939
}
40-
history.listen(({ location, action }) => this.onHistory({ location, action }))
41-
}
42-
43-
onHistory({ location, action }) {
44-
if (action === Action.Pop) {
45-
const reportUuid = reportUuidFromPath(location.pathname)
46-
this.setState({ reportUuid: reportUuid, loading: true }, () => this.reload())
47-
}
4840
}
4941

5042
componentDidMount() {
43+
this.unlistenHistory = history.listen(({ location, action }) => {
44+
if (action !== Action.Pop) return
45+
this.setState({ reportUuid: reportUuidFromPath(location.pathname), loading: true })
46+
})
5147
this.loginForwardAuth()
5248
this.initUserSession()
5349
this.connectToNrMeasurementsEventSource()
54-
this.reload()
50+
this.fetchReports()
51+
}
52+
53+
componentDidUpdate(_prevProps, prevState) {
54+
if (prevState.reportUuid !== this.state.reportUuid || prevState.reportDate !== this.state.reportDate) {
55+
this.fetchReports()
56+
}
5557
}
5658

5759
componentWillUnmount() {
60+
this.unlistenHistory()
5861
this.source.close()
5962
}
6063

@@ -72,18 +75,16 @@ class App extends Component {
7275
}))
7376
}
7477

75-
reload(json) {
76-
if (json) {
77-
showURLAvailabilityMessages(json.availability, (message) => this.showMessage(message))
78-
this.fieldsWithUrlAvailabilityErrors = json.availability
79-
? json.availability.filter((urlKey) => urlKey.status_code !== 200)
80-
: null
81-
this.checkSession(json)
82-
}
83-
this.loadAndSetState()
78+
processResponseJSON(json) {
79+
showURLAvailabilityMessages(json.availability, (message) => this.showMessage(message))
80+
this.fieldsWithUrlAvailabilityErrors = json.availability
81+
? json.availability.filter((urlKey) => urlKey.status_code !== 200)
82+
: null
83+
this.checkSession(json)
84+
this.fetchReports()
8485
}
8586

86-
loadAndSetState() {
87+
fetchReports() {
8788
const reportUuid = this.state.reportUuid
8889
const reportDate = this.state.reportDate
8990
const showErrorMessage = (error) =>
@@ -148,19 +149,19 @@ class App extends Component {
148149
date = null
149150
}
150151
history.replace({ search: toSearchString(parsed) })
151-
this.setState({ reportDate: date, loading: true }, () => this.reload())
152+
this.setState({ reportDate: date, loading: true })
152153
}
153154

154155
openReportsOverview() {
155156
if (history.location.pathname !== "/") {
156157
this.historyPush("/")
157-
this.setState({ reportUuid: "", loading: true }, () => this.reload())
158+
this.setState({ reportUuid: "", loading: true })
158159
}
159160
}
160161

161162
openReport(reportUuid) {
162163
this.historyPush(encodeURI(reportUuid))
163-
this.setState({ reportUuid: reportUuid, loading: true }, () => this.reload())
164+
this.setState({ reportUuid: reportUuid, loading: true })
164165
}
165166

166167
historyPush(target) {
@@ -179,13 +180,13 @@ class App extends Component {
179180
description: "Successfully reconnected to server",
180181
})
181182
this.setState({ nrMeasurements: newNrMeasurements, nrMeasurementsStreamConnected: true }, () =>
182-
this.reload(),
183+
this.fetchReports(),
183184
)
184185
}
185186
},
186187
onDelta: (newNrMeasurements) => {
187188
if (newNrMeasurements !== this.state.nrMeasurements) {
188-
this.setState({ nrMeasurements: newNrMeasurements }, () => this.reload())
189+
this.setState({ nrMeasurements: newNrMeasurements }, () => this.fetchReports())
189190
}
190191
},
191192
onError: () => {
@@ -278,7 +279,7 @@ class App extends Component {
278279
nrMeasurements={this.state.nrMeasurements}
279280
openReport={(reportUuid) => this.openReport(reportUuid)}
280281
openReportsOverview={() => this.openReportsOverview()}
281-
reload={(json) => this.reload(json)}
282+
reload={(json) => this.processResponseJSON(json)}
282283
reportDate={this.state.reportDate}
283284
reportUuid={this.state.reportUuid}
284285
reports={this.state.reports}

components/frontend/src/App.test.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ it("handles a date reset", async () => {
126126
it("reloads on a browser pop but not on a push", async () => {
127127
render(<App />)
128128
const callsAfterMount = fetchServerApi.fetchServerApi.mock.calls.length
129-
history.push("/some-uuid")
129+
history.push("/first-uuid")
130+
history.push("/second-uuid")
130131
const callsAfterPush = fetchServerApi.fetchServerApi.mock.calls.length
131-
history.back()
132+
history.back() // pops back to /first-uuid, which differs from the mount-time reportUuid ""
132133
await vi.waitFor(() => expect(fetchServerApi.fetchServerApi.mock.calls.length).toBeGreaterThan(callsAfterPush))
133134
expect(callsAfterPush).toBe(callsAfterMount)
134135
})

0 commit comments

Comments
 (0)