@@ -11,6 +11,8 @@ import TextArea from 'antd/es/input/TextArea'
11
11
import { ColumnType } from 'antd/es/table'
12
12
import { isoTimestampToHumanReadable } from '../../utils/dateUtils'
13
13
14
+ import useSWR from 'swr'
15
+
14
16
const OPERATION_STATUS_TO_HUMAN = {
15
17
0 : 'Not started' ,
16
18
1 : 'Running' ,
@@ -74,25 +76,29 @@ export function OperationControls({
74
76
}
75
77
76
78
export function OperationsList ( ) : JSX . Element {
77
- const [ operations , setOperations ] = useState ( [ ] )
78
-
79
- const fetchAndUpdateOperationsIfNeeded = async ( ) => {
80
- const response = await fetch ( '/api/async_migrations' )
79
+ const fetchAndUpdateOperationsIfNeeded = async ( url : string ) => {
80
+ const response = await fetch ( url )
81
81
const responseJson = await response . json ( )
82
82
const results = responseJson . results
83
83
if ( JSON . stringify ( results ) !== JSON . stringify ( operations ) ) {
84
- setOperations ( results )
84
+ return results
85
85
}
86
86
}
87
87
88
- const triggerOperation = async ( id ) => {
88
+ const triggerOperation = async id => {
89
89
await fetch ( `/api/async_migrations/${ id } /trigger` , { method : 'POST' } )
90
90
await fetchAndUpdateOperationsIfNeeded ( )
91
91
}
92
92
93
+ const { data : operations , error, isLoading, mutate } = useSWR (
94
+ '/api/async_migrations' ,
95
+ fetchAndUpdateOperationsIfNeeded
96
+ )
97
+
93
98
useEffect ( ( ) => {
94
- fetchAndUpdateOperationsIfNeeded ( )
95
- const intervalId = setInterval ( fetchAndUpdateOperationsIfNeeded , 5000 )
99
+ const intervalId = setInterval ( ( ) => {
100
+ mutate ( '/api/async_migrations' )
101
+ } , 5000 )
96
102
return ( ) => {
97
103
try {
98
104
clearInterval ( intervalId )
@@ -121,11 +127,11 @@ export function OperationsList(): JSX.Element {
121
127
} ,
122
128
{
123
129
title : 'Started at' ,
124
- render : ( _ , migration ) => migration . started_at ? isoTimestampToHumanReadable ( migration . started_at ) : '' ,
130
+ render : ( _ , migration ) => ( migration . started_at ? isoTimestampToHumanReadable ( migration . started_at ) : '' ) ,
125
131
} ,
126
132
{
127
133
title : 'Finished at' ,
128
- render : ( _ , migration ) => migration . finished_at ? isoTimestampToHumanReadable ( migration . finished_at ) : '' ,
134
+ render : ( _ , migration ) => ( migration . finished_at ? isoTimestampToHumanReadable ( migration . finished_at ) : '' ) ,
129
135
} ,
130
136
{
131
137
title : '' ,
@@ -140,7 +146,13 @@ export function OperationsList(): JSX.Element {
140
146
} ,
141
147
]
142
148
143
- return < Table columns = { columns } dataSource = { operations } />
149
+ return isLoading ? (
150
+ < div > loading...</ div >
151
+ ) : error ? (
152
+ < div > error</ div >
153
+ ) : (
154
+ < Table columns = { columns } dataSource = { operations } />
155
+ )
144
156
}
145
157
146
158
export function CreateNewOperation ( ) : JSX . Element {
@@ -221,8 +233,8 @@ export function CreateNewOperation(): JSX.Element {
221
233
code [ `operation-${ i + 1 } ` ] ||
222
234
`CREATE TABLE test_table ( foo String ) Engine=MergeTree() ORDER BY foo`
223
235
}
224
- onValueChange = { ( value ) => setCode ( { ...code , [ `operation-${ i + 1 } ` ] : value } ) }
225
- highlight = { ( code ) => highlight ( code , languages . sql ) }
236
+ onValueChange = { value => setCode ( { ...code , [ `operation-${ i + 1 } ` ] : value } ) }
237
+ highlight = { code => highlight ( code , languages . sql ) }
226
238
padding = { 10 }
227
239
style = { {
228
240
fontFamily : '"Fira code", "Fira Mono", monospace' ,
@@ -242,8 +254,8 @@ export function CreateNewOperation(): JSX.Element {
242
254
id = { `create-migration-form-rollback-${ i + 1 } ` }
243
255
name = { `rollback-${ i + 1 } ` }
244
256
value = { code [ `rollback-${ i + 1 } ` ] || `DROP TABLE IF EXISTS test_table` }
245
- onValueChange = { ( value ) => setCode ( { ...code , [ `rollback-${ i + 1 } ` ] : value } ) }
246
- highlight = { ( code ) => highlight ( code , languages . sql ) }
257
+ onValueChange = { value => setCode ( { ...code , [ `rollback-${ i + 1 } ` ] : value } ) }
258
+ highlight = { code => highlight ( code , languages . sql ) }
247
259
padding = { 10 }
248
260
style = { {
249
261
fontFamily : '"Fira code", "Fira Mono", monospace' ,
0 commit comments