@@ -20,14 +20,27 @@ interface OMDBResult {
20
20
Type : string ;
21
21
Poster : string ;
22
22
}
23
+
24
+ type OMDBSearchResponse = OMDBResults | OMDBError ;
23
25
interface OMDBResults {
26
+ Response : "True" ;
24
27
Search : OMDBResult [ ] ;
28
+ totalResults : string ;
29
+ }
30
+
31
+ interface OMDBError {
32
+ Response : "False" ;
33
+ Error : string ;
25
34
}
26
35
27
36
interface OMDBAttachmentMeta {
28
37
imdbID : string ;
29
38
}
30
39
40
+ function isOMDBResults ( resp : OMDBSearchResponse ) : resp is OMDBResults {
41
+ return resp . Response === "True" ;
42
+ }
43
+
31
44
CloudControl . register < OMDBConfig > (
32
45
vendorId ,
33
46
"omdb" ,
@@ -44,7 +57,7 @@ function isOMDBAttachment(
44
57
function OMDBControl ( props : ControlApi < OMDBConfig > ) {
45
58
const config = props . config ;
46
59
47
- function search ( s : string ) : AxiosPromise < OMDBResults > {
60
+ function search ( s : string ) : AxiosPromise < OMDBSearchResponse > {
48
61
return axios . get (
49
62
"http://www.omdbapi.com/?s=" +
50
63
encodeURIComponent ( s ) +
@@ -55,6 +68,7 @@ function OMDBControl(props: ControlApi<OMDBConfig>) {
55
68
56
69
const [ searchText , setSearchText ] = React . useState ( "" ) ;
57
70
const [ results , setResults ] = React . useState < OMDBResults > ( ) ;
71
+ const [ error , setError ] = React . useState < OMDBError > ( ) ;
58
72
const [ omdbAttachments , setOmdbAttachments ] = React . useState <
59
73
CloudAttachment < OMDBAttachmentMeta > [ ]
60
74
> ( props . attachments . filter ( isOMDBAttachment ) ) ;
@@ -68,7 +82,18 @@ function OMDBControl(props: ControlApi<OMDBConfig>) {
68
82
} , [ ] ) ;
69
83
const doSearch = ( ) => {
70
84
if ( searchText . length > 0 ) {
71
- search ( searchText ) . then ( resp => setResults ( resp . data ) ) ;
85
+ search ( searchText ) . then ( resp => {
86
+ let error ;
87
+ let results ;
88
+ let data = resp . data ;
89
+ if ( isOMDBResults ( data ) ) {
90
+ results = data ;
91
+ } else {
92
+ error = data ;
93
+ }
94
+ setResults ( results ) ;
95
+ setError ( error ) ;
96
+ } ) ;
72
97
}
73
98
} ;
74
99
const attachmentMap = omdbAttachments . reduce ( ( res , a ) => {
@@ -159,6 +184,7 @@ function OMDBControl(props: ControlApi<OMDBConfig>) {
159
184
onChange = { e => setSearchText ( e . target . value ) }
160
185
/>
161
186
< button onClick = { doSearch } > Search</ button >
187
+ { error && < h4 > Failed to find any movies</ h4 > }
162
188
{ results && renderResults ( results ) }
163
189
</ div >
164
190
) ;
0 commit comments