1
1
import PermScanWifiIcon from '@mui/icons-material/PermScanWifi' ;
2
2
import { Button } from '@mui/material' ;
3
- import { useRequest } from 'alova' ;
4
- import { useState , useCallback , useRef } from 'react ' ;
5
- import { toast } from 'react-toastify ' ;
3
+ // eslint-disable-next-line import/named
4
+ import { updateState , useRequest } from 'alova ' ;
5
+ import { useState , useRef } from 'react' ;
6
6
7
7
import WiFiNetworkSelector from './WiFiNetworkSelector' ;
8
8
import type { FC } from 'react' ;
@@ -15,48 +15,37 @@ const NUM_POLLS = 10;
15
15
const POLLING_FREQUENCY = 1000 ;
16
16
17
17
const WiFiNetworkScanner : FC = ( ) => {
18
- const { LL } = useI18nContext ( ) ;
19
-
20
18
const pollCount = useRef ( 0 ) ;
19
+ const { LL } = useI18nContext ( ) ;
21
20
const [ errorMessage , setErrorMessage ] = useState < string > ( ) ;
22
21
23
- const { data : networkList , send : getNetworkList } = useRequest ( NetworkApi . listNetworks , {
22
+ const { send : scanNetworks , onComplete : onCompleteScanNetworks } = useRequest ( NetworkApi . scanNetworks ) ; // is called on page load to start network scan
23
+ const {
24
+ data : networkList ,
25
+ send : getNetworkList ,
26
+ onSuccess : onSuccessNetworkList
27
+ } = useRequest ( NetworkApi . listNetworks , {
24
28
immediate : false
25
29
} ) ;
26
30
27
- const {
28
- send : scanNetworks ,
29
- onSuccess : onSuccessScanNetworks ,
30
- onError : onErrorScanNetworks
31
- } = useRequest ( NetworkApi . scanNetworks ) ;
32
-
33
- const finishedWithError = useCallback ( ( message : string ) => {
34
- toast . error ( message ) ;
35
- setErrorMessage ( message ) ;
36
- pollCount . current = 0 ;
37
- } , [ ] ) ;
38
-
39
- onErrorScanNetworks ( ( event ) => {
40
- console . log ( 'onErrorScanNetworks' ) ; // TODO fix
41
- if ( event . error ?. message === 'Wait' ) {
42
- // 202
43
- console . log ( 'not ready...: ' , event . error ?. message ) ; // TODO fix
31
+ onSuccessNetworkList ( ( event ) => {
32
+ if ( ! event . data ) {
44
33
const completedPollCount = pollCount . current + 1 ;
45
34
if ( completedPollCount < NUM_POLLS ) {
46
35
pollCount . current = completedPollCount ;
47
- setTimeout ( scanNetworks , POLLING_FREQUENCY ) ;
36
+ setTimeout ( getNetworkList , POLLING_FREQUENCY ) ;
48
37
} else {
49
- finishedWithError ( LL . PROBLEM_LOADING ( ) ) ;
38
+ setErrorMessage ( LL . PROBLEM_LOADING ( ) ) ;
39
+ pollCount . current = 0 ;
50
40
}
51
- } else {
52
- finishedWithError ( LL . PROBLEM_LOADING ( ) ) ;
53
41
}
54
42
} ) ;
55
43
56
- onSuccessScanNetworks ( ( ) => {
57
- console . log ( 'onCompleteScanNetworks' ) ; // TODO fix
44
+ onCompleteScanNetworks ( ( ) => {
58
45
pollCount . current = 0 ;
59
- void getNetworkList ( ) ; // fetch the list
46
+ setErrorMessage ( undefined ) ;
47
+ updateState ( 'listNetworks' , ( ) => undefined ) ;
48
+ void getNetworkList ( ) ;
60
49
} ) ;
61
50
62
51
const renderNetworkScanner = ( ) => {
0 commit comments