Skip to content

Commit 47f1327

Browse files
committed
Disable create and clone buttons while waiting for network response.
I've accidentally created a backfill multiple times thinking I didn't click the create button and pressing it multiple times as a result. This disables the button while the request is sending preventing that.
1 parent 65d21a1 commit 47f1327

File tree

2 files changed

+77
-62
lines changed

2 files changed

+77
-62
lines changed

service/web/tabs/app/src/containers/CloneFormContainer.tsx

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { LayoutContainer } from "../containers"
3333
interface CloneFormState {
3434
loading: boolean
3535
errorText?: string
36+
cloneButtonDisabled: boolean
3637

3738
statusResponse: any
3839

@@ -61,6 +62,7 @@ class CloneFormContainer extends React.Component<
6162
this.setState({
6263
loading: false,
6364
errorText: null,
65+
cloneButtonDisabled: false,
6466

6567
statusResponse: null,
6668

@@ -131,6 +133,35 @@ class CloneFormContainer extends React.Component<
131133
})
132134
}
133135

136+
async handleClick() {
137+
this.setState({ cloneButtonDisabled: true })
138+
try {
139+
const response = await Axios.post(`/backfills/${this.id}/clone`, {
140+
dry_run: this.state.dry_run,
141+
scan_size: this.state.scan_size,
142+
batch_size: this.state.batch_size,
143+
num_threads: this.state.num_threads,
144+
range_clone_type: this.state.range_clone_type,
145+
pkey_range_start: this.nullIfEmpty(this.state.pkey_range_start),
146+
pkey_range_end: this.nullIfEmpty(this.state.pkey_range_end),
147+
backoff_schedule: this.nullIfEmpty(this.state.backoff_schedule),
148+
extra_sleep_ms: this.state.extra_sleep_ms,
149+
parameter_map: this.state.parameters
150+
})
151+
let id = response.data.id
152+
let history = (this.props as any).history
153+
history.push(`/app/backfills/${id}`)
154+
} catch (error) {
155+
console.log(error)
156+
this.setState({
157+
loading: false,
158+
errorText: error.response.data
159+
})
160+
} finally {
161+
this.setState({ cloneButtonDisabled: false })
162+
}
163+
}
164+
134165
render() {
135166
if (!this.state || !this.state.backfills) {
136167
return (
@@ -320,34 +351,7 @@ class CloneFormContainer extends React.Component<
320351
)}
321352
<Button
322353
onClick={() => {
323-
Axios.post(`/backfills/${this.id}/clone`, {
324-
dry_run: this.state.dry_run,
325-
scan_size: this.state.scan_size,
326-
batch_size: this.state.batch_size,
327-
num_threads: this.state.num_threads,
328-
range_clone_type: this.state.range_clone_type,
329-
pkey_range_start: this.nullIfEmpty(
330-
this.state.pkey_range_start
331-
),
332-
pkey_range_end: this.nullIfEmpty(this.state.pkey_range_end),
333-
backoff_schedule: this.nullIfEmpty(
334-
this.state.backoff_schedule
335-
),
336-
extra_sleep_ms: this.state.extra_sleep_ms,
337-
parameter_map: this.state.parameters
338-
})
339-
.then(response => {
340-
let id = response.data.id
341-
let history = (this.props as any).history
342-
history.push(`/app/backfills/${id}`)
343-
})
344-
.catch(error => {
345-
console.log(error)
346-
this.setState({
347-
loading: false,
348-
errorText: error.response.data
349-
})
350-
})
354+
this.handleClick()
351355
}}
352356
intent={Intent.PRIMARY}
353357
loading={this.state.loading}

service/web/tabs/app/src/containers/CreateFormContainer.tsx

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { RESERVED_VARIANT } from "../utilities"
3232
interface CreateFormState {
3333
loading: boolean
3434
errorText?: string
35+
createButtonDisabled: boolean
3536
backfill?: IBackfill
3637
dry_run: boolean
3738

@@ -62,6 +63,7 @@ class CreateFormContainer extends React.Component<
6263
this.setState({
6364
loading: false,
6465
errorText: null,
66+
createButtonDisabled: false,
6567
backfill: null,
6668
dry_run: true,
6769
scan_size: 10000,
@@ -75,6 +77,45 @@ class CreateFormContainer extends React.Component<
7577
})
7678
}
7779

80+
async handleClick() {
81+
this.setState({ createButtonDisabled: true })
82+
83+
try {
84+
await new Promise(resolve => { setTimeout(resolve, 10000) })
85+
const response = await Axios.post(
86+
`/services/${this.service}/variants/${this.variant}/create`,
87+
{
88+
backfill_name: this.state.backfill.name,
89+
dry_run: this.state.dry_run,
90+
scan_size: this.state.scan_size,
91+
batch_size: this.state.batch_size,
92+
num_threads: this.state.num_threads,
93+
pkey_range_start: this.nullIfEmpty(
94+
this.base64(this.state.pkey_range_start)
95+
),
96+
pkey_range_end: this.nullIfEmpty(
97+
this.base64(this.state.pkey_range_end)
98+
),
99+
backoff_schedule: this.nullIfEmpty(this.state.backoff_schedule),
100+
extra_sleep_ms: this.state.extra_sleep_ms,
101+
parameter_map: this.state.parameters
102+
}
103+
)
104+
105+
let id = response.data.backfill_run_id
106+
let history = (this.props as any).history
107+
history.push(`/app/backfills/${id}`)
108+
} catch (error) {
109+
console.log(error)
110+
this.setState({
111+
loading: false,
112+
errorText: error.response.data
113+
})
114+
} finally {
115+
this.setState({ createButtonDisabled: false })
116+
}
117+
}
118+
78119
render() {
79120
let registeredBackfills = simpleSelectorGet(this.props.simpleNetwork, [
80121
this.registeredBackfills,
@@ -246,43 +287,13 @@ class CreateFormContainer extends React.Component<
246287
)}
247288
<Button
248289
onClick={() => {
249-
Axios.post(
250-
`/services/${this.service}/variants/${this.variant}/create`,
251-
{
252-
backfill_name: this.state.backfill.name,
253-
dry_run: this.state.dry_run,
254-
scan_size: this.state.scan_size,
255-
batch_size: this.state.batch_size,
256-
num_threads: this.state.num_threads,
257-
pkey_range_start: this.nullIfEmpty(
258-
this.base64(this.state.pkey_range_start)
259-
),
260-
pkey_range_end: this.nullIfEmpty(
261-
this.base64(this.state.pkey_range_end)
262-
),
263-
backoff_schedule: this.nullIfEmpty(
264-
this.state.backoff_schedule
265-
),
266-
extra_sleep_ms: this.state.extra_sleep_ms,
267-
parameter_map: this.state.parameters
268-
}
269-
)
270-
.then(response => {
271-
let id = response.data.backfill_run_id
272-
let history = (this.props as any).history
273-
history.push(`/app/backfills/${id}`)
274-
})
275-
.catch(error => {
276-
console.log(error)
277-
this.setState({
278-
loading: false,
279-
errorText: error.response.data
280-
})
281-
})
290+
this.handleClick()
282291
}}
283292
intent={Intent.PRIMARY}
284293
loading={this.state.loading}
285-
disabled={!this.state.backfill}
294+
disabled={
295+
this.state.createButtonDisabled || !this.state.backfill
296+
}
286297
text={"Create"}
287298
/>
288299
</div>

0 commit comments

Comments
 (0)