Skip to content

Commit c0ae4be

Browse files
committed
Loading state for predict button
1 parent 30a33e6 commit c0ae4be

File tree

1 file changed

+41
-24
lines changed
  • Track 4_ReactJS_Web Development/Project/Frontend/hrc-dashboard/src/components/navigation

1 file changed

+41
-24
lines changed

Track 4_ReactJS_Web Development/Project/Frontend/hrc-dashboard/src/components/navigation/PredictButton.jsx

+41-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Mantine from '@mantine/core';
2-
import { showNotification } from '@mantine/notifications';
2+
import { showNotification, updateNotification } from '@mantine/notifications';
33
import React, { useEffect, useState } from 'react';
44
import { useDispatch, useSelector } from 'react-redux';
55
import { Check, X } from 'tabler-icons-react';
@@ -21,35 +21,52 @@ function PredictButton() {
2121
});
2222
const actionDispatch = useDispatch();
2323

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+
2463
return (
2564
<Mantine.Button
2665
className={`w-auto bg-orange-400 hover:bg-orange-500 hover:cursor-pointer transition ease-in-out duration-75 ${
2766
disabled && 'blur-sm'
2867
}`}
2968
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}
5370
>
5471
Predict
5572
</Mantine.Button>

0 commit comments

Comments
 (0)