1
+ // Copyright 2022 MFB Technologies, Inc.
2
+
3
+ import { AsyncRequestStatus , createAsyncRenderer } from "@mfbtech/react-async-renderer"
4
+ import { useState } from "react"
5
+ import "./AsyncRendererExample.css"
6
+
7
+ export function AsyncRendererExample ( ) {
8
+ const [ currentAsyncRequestState , setCurrentAsyncRequestState ] = useState ( AsyncRequestStatus . INIT )
9
+ const asyncRequestError = currentAsyncRequestState === AsyncRequestStatus . ERROR ? "some error occurred!" : null
10
+ const asyncRequestData = currentAsyncRequestState === AsyncRequestStatus . FULFILLED ? { data : "my data" } : null
11
+ const renderer = createAsyncRenderer ( {
12
+ status : currentAsyncRequestState ,
13
+ error : asyncRequestError ,
14
+ onCompletedSuccessfullyArgs : asyncRequestData ,
15
+ } )
16
+
17
+ return (
18
+ < div className = "async-renderer-example" >
19
+ < h2 > Async renderer example</ h2 >
20
+ < div >
21
+ < label htmlFor = "asyncRequestStatus" > Status of the long running process:</ label >
22
+ < select
23
+ value = { currentAsyncRequestState }
24
+ onChange = { ( e ) => {
25
+ setCurrentAsyncRequestState ( e . currentTarget . value as AsyncRequestStatus )
26
+ } }
27
+ id = "asyncRequestStatus" >
28
+ {
29
+ Object . entries ( AsyncRequestStatus ) . map ( ( [ name , value ] ) => {
30
+ return (
31
+ < option key = { value } value = { value } > { name } </ option >
32
+ )
33
+ } )
34
+ }
35
+ </ select >
36
+ </ div >
37
+ < div >
38
+ { renderer ( ( args ) => {
39
+ return (
40
+ < p className = 'success' > { args . data } </ p >
41
+ )
42
+ } ,
43
+ {
44
+ onInit : ( ) => ( < p className = 'info' > Waiting for long running process to start...</ p > ) ,
45
+ onLoading : ( ) => ( < p className = 'info' > custom spinner...</ p > ) ,
46
+ onCompletedWithError : ( props ) => ( < p className = 'error' > { props . errorMessage ?? "error" } </ p > )
47
+ } ) }
48
+ </ div >
49
+ </ div >
50
+ )
51
+ }
0 commit comments