From c8f885ab5bfe9e1b7a6a5bd29c5ce00e81771c67 Mon Sep 17 00:00:00 2001 From: salixor Date: Mon, 1 Jul 2024 21:04:15 +0200 Subject: [PATCH] Add loading display when searching for GIFs --- web/src/giphy.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/web/src/giphy.js b/web/src/giphy.js index 35172bb..8e884d4 100644 --- a/web/src/giphy.js +++ b/web/src/giphy.js @@ -1,5 +1,6 @@ import {Component, html} from "../lib/htm/preact.js"; import * as widgetAPI from "./widget-api.js"; +import {Spinner} from "./spinner.js"; import {SearchBox} from "./search-box.js"; const GIPHY_SEARCH_DEBOUNCE = 1000 @@ -34,13 +35,14 @@ export class GiphySearchTab extends Component { async makeGifSearchRequest() { try { + this.setState({gifs: [], loading: true}) const resp = await fetch(`https://api.giphy.com/v1/gifs/search?q=${this.state.searchTerm}&api_key=${GIPHY_API_KEY}`) // TODO handle error responses properly? const data = await resp.json() if (data.data.length === 0) { - this.setState({gifs: [], error: "No results"}) + this.setState({gifs: [], error: "No results", loading: false}) } else { - this.setState({gifs: data.data, error: null}) + this.setState({gifs: data.data, error: null, loading: false}) } } catch (error) { this.setState({error}) @@ -82,24 +84,27 @@ export class GiphySearchTab extends Component { } render() { - // TODO display loading state? return html` <${SearchBox} onInput=${this.updateGifSearchQuery} onKeyUp=${this.searchKeyUp} value=${this.state.searchTerm} placeholder="Find GIFs"/>
-
- ${this.state.error} -
-
- ${this.state.gifs.map((gif) => html` -
this.handleGifClick(gif)} data-gif-id=${gif.id}> - ${gif.title} + ${this.state.loading ? + html`<${Spinner} size=${80} green/>` : + html` +
+ ${this.state.error}
- `)} -
- +
+ ${this.state.gifs.map((gif) => html` +
this.handleGifClick(gif)} data-gif-id=${gif.id}> + ${gif.title} +
+ `)} +
+ + `}
`