Skip to content

Commit 1b66ec0

Browse files
committed
react: custom toast impl.
1 parent 741cecf commit 1b66ec0

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

react-js/src/App.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect } from "react";
22
import { useNavigate } from 'react-router-dom';
3-
import { Alert, Box, List, Snackbar } from "@mui/material";
3+
import { Box, List } from "@mui/material";
44
import {
55
Code,
66
DirectionsCar,
@@ -21,12 +21,13 @@ import SectionHeader from "./subviews/SectionHeader";
2121
import { TopBar } from "./subviews/TopBar";
2222
import SBSDKService from "./service/SBSDKService";
2323
import ImageUtils, { MimeType } from "./service/ImageUtils";
24+
import { Toast } from "./subviews/Toast.tsx";
2425

2526
function App() {
2627

2728
const navigate = useNavigate();
2829

29-
const [toast, setToast] = React.useState<string | null>(null);
30+
const [toast, setToast] = React.useState<string | undefined>(undefined);
3031

3132
useEffect(() => {
3233
SBSDKService.initialize();
@@ -97,16 +98,7 @@ function App() {
9798
}} />
9899
</List>
99100

100-
<Snackbar open={toast !== null} autoHideDuration={3000} onClose={() => setToast(null)}>
101-
<Alert
102-
onClose={() => setToast(null)}
103-
severity="success"
104-
variant="filled"
105-
sx={{ width: '100%' }}
106-
>
107-
{toast}
108-
</Alert>
109-
</Snackbar>
101+
<Toast text={toast} />
110102
</Box>
111103
)
112104
}

react-js/src/pages/StoredDataDetailsPage.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import React, { useEffect, useState } from "react";
2-
import { Alert, Box, Button, CircularProgress, Snackbar } from "@mui/material";
2+
import { Box, Button, CircularProgress } from "@mui/material";
33
import { Image, SBStoreCroppedDetectionResult } from "scanbot-web-sdk/@types";
44

55
import SBSDKService from "../service/SBSDKService";
66
import ImageUtils from "../service/ImageUtils";
77
import { TopBar } from "../subviews/TopBar";
88
import { TextColor } from "../subviews/FeatureListItem.tsx";
9+
import { Toast } from "../subviews/Toast.tsx";
910

1011
export default function StorageDetailsPage() {
1112

1213
const [item, setItem] = useState<SBStoreCroppedDetectionResult | null>(null);
1314
const [base64Image, setBase64Image] = useState<string | undefined>(undefined);
1415
const [isLoading, setIsLoading] = useState(false);
1516
const [rotatedImage, setRotatedImage] = useState<Image | undefined>(undefined);
16-
const [toast, setToast] = React.useState<string | null>(null);
17+
const [toast, setToast] = React.useState<string | undefined>(undefined);
1718

1819
useEffect(() => {
1920

@@ -114,16 +115,7 @@ export default function StorageDetailsPage() {
114115
color="primary"
115116
/>
116117

117-
<Snackbar open={toast !== null} autoHideDuration={3000} onClose={() => setToast(null)}>
118-
<Alert
119-
onClose={() => setToast(null)}
120-
severity="success"
121-
variant="filled"
122-
sx={{ width: '100%' }}
123-
>
124-
{toast}
125-
</Alert>
126-
</Snackbar>
118+
<Toast text={toast} />
127119
</Box>
128120
)
129121
}

react-js/src/subviews/Toast.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React, { useEffect } from "react";
2+
import { Alert, Snackbar } from "@mui/material";
3+
4+
export class Props {
5+
text?: string;
6+
}
7+
8+
export function Toast(props: Props) {
9+
const [toast, setToast] = React.useState<string | null>(null);
10+
11+
useEffect(() => {
12+
if (props.text) {
13+
setToast(props.text);
14+
}
15+
}, [props.text]);
16+
17+
return (
18+
19+
<Snackbar open={toast !== null} autoHideDuration={3000} onClose={() => setToast(null)}>
20+
<Alert
21+
onClose={() => setToast(null)}
22+
severity="success"
23+
variant="filled"
24+
sx={{ width: '100%' }}
25+
>
26+
{toast}
27+
</Alert>
28+
</Snackbar>
29+
)
30+
}

0 commit comments

Comments
 (0)