99 stringifyWithPreservingUndefinedAsNull ,
1010} from './utils' ;
1111
12+ import NiceModal from '@ebay/nice-modal-react' ;
1213import { Store } from 'react-notifications-component' ;
1314import keyBy from 'lodash/keyBy' ;
1415
@@ -113,21 +114,27 @@ class Api {
113114 }
114115 }
115116
116- urlProvider = async ( ) => {
117- const url = new URL ( this . url )
118- let token = new URLSearchParams ( window . location . search ) . get ( "token" )
119- ?? local . get < string > ( TOKEN_LOCAL_STORAGE_ITEM_NAME ) ;
120- const authRequired = ! ! local . get ( AUTH_FLAG_LOCAL_STORAGE_ITEM_NAME ) ;
121- if ( authRequired ) {
122- if ( ! token ) {
123- token = prompt ( "Enter your z2m admin token" ) as string ;
124- if ( token ) {
125- local . set ( TOKEN_LOCAL_STORAGE_ITEM_NAME , token ) ;
117+ urlProvider = async ( ) : Promise < string > => {
118+ const promise = new Promise < string > ( ( resolve ) => {
119+ const url = new URL ( this . url )
120+ let token = new URLSearchParams ( window . location . search ) . get ( "token" )
121+ ?? local . get < string > ( TOKEN_LOCAL_STORAGE_ITEM_NAME ) ;
122+ const authRequired = ! ! local . get ( AUTH_FLAG_LOCAL_STORAGE_ITEM_NAME ) ;
123+ if ( authRequired ) {
124+ if ( ! token ) {
125+ NiceModal . show ( 'auth-form' , { onAuth : ( token : string ) => {
126+ local . set ( TOKEN_LOCAL_STORAGE_ITEM_NAME , token ) ;
127+ url . searchParams . append ( "token" , token ) ;
128+ resolve ( url . toString ( ) ) ;
129+ } } ) ;
130+ return ;
126131 }
132+ url . searchParams . append ( "token" , token ) ;
127133 }
128- url . searchParams . append ( "token" , token ) ;
129- }
130- return url . toString ( ) ;
134+ resolve ( url . toString ( ) ) ;
135+ } ) ;
136+
137+ return promise ;
131138 }
132139
133140 connect ( ) : void {
@@ -277,7 +284,7 @@ class Api {
277284 if ( e . code === UNAUTHORIZED_ERROR_CODE ) {
278285 local . set ( AUTH_FLAG_LOCAL_STORAGE_ITEM_NAME , true ) ;
279286 local . remove ( TOKEN_LOCAL_STORAGE_ITEM_NAME ) ;
280- NotificationManager . error ( "Unauthorized" ) ;
287+ showNotify ( 'error' , "Unauthorized" , false ) ;
281288 setTimeout ( ( ) => {
282289 window . location . reload ( ) ;
283290 } , 1000 ) ;
@@ -296,7 +303,7 @@ class Api {
296303 this . processDeviceStateMessage ( data ) ;
297304 }
298305 } catch ( e ) {
299- NotificationManager . error ( e . message ) ;
306+ showNotify ( 'error' , e . message , false ) ;
300307 console . error ( event . data ) ;
301308 }
302309
0 commit comments