1
1
import * as Mantine from '@mantine/core' ;
2
- import { showNotification } from '@mantine/notifications' ;
2
+ import { showNotification , updateNotification } from '@mantine/notifications' ;
3
3
import React , { useEffect , useState } from 'react' ;
4
4
import { useDispatch , useSelector } from 'react-redux' ;
5
5
import { Check , X } from 'tabler-icons-react' ;
@@ -21,35 +21,52 @@ function PredictButton() {
21
21
} ) ;
22
22
const actionDispatch = useDispatch ( ) ;
23
23
24
+ const onButtonClick = async ( ) => {
25
+ try {
26
+ showNotification ( {
27
+ id : 'load-prediction' , // id is necessary for updating notification
28
+ title : 'Loading!' ,
29
+ message : `The predictions are loading!` ,
30
+ color : 'teal' ,
31
+ autoClose : 10000 ,
32
+ disallowClose : false ,
33
+ loading : true ,
34
+ icon : < Check size = { 18 } /> ,
35
+ } ) ;
36
+ const predictions = await doPrediction ( docId ) ;
37
+ actionDispatch ( setPredictions ( predictions ) ) ;
38
+
39
+ if ( predictions . length > 0 ) {
40
+ updateNotification ( {
41
+ id : 'load-prediction' , // id is necessary for updating notification
42
+ title : 'Alert!' ,
43
+ message : `The predictions are ready!` ,
44
+ color : 'teal' ,
45
+ autoClose : 10000 ,
46
+ disallowClose : false ,
47
+ loading : false ,
48
+ icon : < Check size = { 18 } /> ,
49
+ } ) ;
50
+ }
51
+ } catch ( e ) {
52
+ showNotification ( {
53
+ title : 'Alert!' ,
54
+ message : `The server returned error while predicting. ${ e ?. message } ` ,
55
+ color : 'red' ,
56
+ disallowClose : false ,
57
+ loading : false ,
58
+ icon : < X size = { 18 } /> ,
59
+ } ) ;
60
+ }
61
+ } ;
62
+
24
63
return (
25
64
< Mantine . Button
26
65
className = { `w-auto bg-orange-400 hover:bg-orange-500 hover:cursor-pointer transition ease-in-out duration-75 ${
27
66
disabled && 'blur-sm'
28
67
} `}
29
68
disabled = { disabled }
30
- onClick = { async ( ) => {
31
- const predictions = await doPrediction ( docId ) ;
32
- actionDispatch ( setPredictions ( predictions ) ) ;
33
-
34
- if ( predictions . length > 0 ) {
35
- showNotification ( {
36
- title : 'Alert!' ,
37
- message : `The predictions are ready!` ,
38
- color : 'teal' ,
39
- autoClose : 10000 ,
40
- disallowClose : false ,
41
- icon : < Check size = { 18 } /> ,
42
- } ) ;
43
- } else {
44
- showNotification ( {
45
- title : 'Alert!' ,
46
- message : 'The server returned error while predicting.' ,
47
- color : 'red' ,
48
- disallowClose : false ,
49
- icon : < X size = { 18 } /> ,
50
- } ) ;
51
- }
52
- } }
69
+ onClick = { onButtonClick }
53
70
>
54
71
Predict
55
72
</ Mantine . Button >
0 commit comments