Skip to content

Commit 0debbd1

Browse files
committed
Fix baseUrl and support zero results
1 parent 32d958d commit 0debbd1

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

sample/src/shared/registration.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ export function providerRegistration(): ProviderRegistration {
1616
},
1717
serviceUrls: {
1818
oauth: {
19-
url: "${baseurl}oauthtoken",
19+
url: "${baseUrl}oauthtoken",
2020
authenticated: false
2121
},
2222
refresh: {
23-
url: "${baseurl}refresh",
23+
url: "${baseUrl}refresh",
2424
authenticated: false
2525
},
2626
controls: {
27-
url: "${baseurl}controls",
27+
url: "${baseUrl}controls",
2828
authenticated: false
2929
},
3030
control_omdb: {
31-
url: "${baseurl}omdb.js",
31+
url: "${baseUrl}omdb.js",
3232
authenticated: false
3333
},
3434
imdbviewer: {
35-
url: "https://www.imdb.com/title/${imbdID}/",
35+
url: "https://www.imdb.com/title/${imdbID}/",
3636
authenticated: false
3737
}
3838
},

sample/src/site/omdb.tsx

+28-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,27 @@ interface OMDBResult {
2020
Type: string;
2121
Poster: string;
2222
}
23+
24+
type OMDBSearchResponse = OMDBResults | OMDBError;
2325
interface OMDBResults {
26+
Response: "True";
2427
Search: OMDBResult[];
28+
totalResults: string;
29+
}
30+
31+
interface OMDBError {
32+
Response: "False";
33+
Error: string;
2534
}
2635

2736
interface OMDBAttachmentMeta {
2837
imdbID: string;
2938
}
3039

40+
function isOMDBResults(resp: OMDBSearchResponse): resp is OMDBResults {
41+
return resp.Response === "True";
42+
}
43+
3144
CloudControl.register<OMDBConfig>(
3245
vendorId,
3346
"omdb",
@@ -44,7 +57,7 @@ function isOMDBAttachment(
4457
function OMDBControl(props: ControlApi<OMDBConfig>) {
4558
const config = props.config;
4659

47-
function search(s: string): AxiosPromise<OMDBResults> {
60+
function search(s: string): AxiosPromise<OMDBSearchResponse> {
4861
return axios.get(
4962
"http://www.omdbapi.com/?s=" +
5063
encodeURIComponent(s) +
@@ -55,6 +68,7 @@ function OMDBControl(props: ControlApi<OMDBConfig>) {
5568

5669
const [searchText, setSearchText] = React.useState("");
5770
const [results, setResults] = React.useState<OMDBResults>();
71+
const [error, setError] = React.useState<OMDBError>();
5872
const [omdbAttachments, setOmdbAttachments] = React.useState<
5973
CloudAttachment<OMDBAttachmentMeta>[]
6074
>(props.attachments.filter(isOMDBAttachment));
@@ -68,7 +82,18 @@ function OMDBControl(props: ControlApi<OMDBConfig>) {
6882
}, []);
6983
const doSearch = () => {
7084
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+
});
7297
}
7398
};
7499
const attachmentMap = omdbAttachments.reduce((res, a) => {
@@ -159,6 +184,7 @@ function OMDBControl(props: ControlApi<OMDBConfig>) {
159184
onChange={e => setSearchText(e.target.value)}
160185
/>
161186
<button onClick={doSearch}>Search</button>
187+
{error && <h4>Failed to find any movies</h4>}
162188
{results && renderResults(results)}
163189
</div>
164190
);

0 commit comments

Comments
 (0)