|
1 | 1 | import { Component } from '@wordpress/element'; |
2 | | -import { TextControl } from '@wordpress/components'; |
| 2 | +import { AlignmentToolbar, BlockControls, BlockAlignmentToolbar } from '@wordpress/editor'; |
| 3 | +import { addQueryArgs } from '@wordpress/url'; |
| 4 | +import { debounce } from 'lodash'; |
| 5 | + |
| 6 | +import SearchGiphy from "./components/SearchGiphy"; |
3 | 7 |
|
4 | 8 | export default class Edit extends Component { |
| 9 | + constructor( props ) { |
| 10 | + super( props ); |
| 11 | + |
| 12 | + this.state = { |
| 13 | + isLoading: false, // If currently fetching from Giphy. |
| 14 | + isSearching: !props.attributes.gif, // If we need to show the search field. |
| 15 | + gifs: [], // Cache results from Giphy. |
| 16 | + pagination: 0, // Current pagination. |
| 17 | + }; |
| 18 | + |
| 19 | + // Get the API Key from https://developers.giphy.com/. |
| 20 | + this.API_KEY = '[INSERT YOUR API KEY HERE]'; |
| 21 | + this.GIPHY_ENDPOINT = 'https://api.giphy.com/v1/gifs/search'; |
| 22 | + this.GIPHY_RESULTS_LIMIT = 5; |
| 23 | + |
| 24 | + this.onSearchChangeHandler = this.onSearchChangeHandler.bind( this ); |
| 25 | + this.fetchGiphy = this.fetchGiphy.bind( this ); |
| 26 | + // Use debounce to prevent multiple concurrent request to Giphy. |
| 27 | + this.onSearchChange = debounce( this.onSearchChange.bind( this ), 500 ); |
| 28 | + this.onGiphyClick = this.onGiphyClick.bind( this ); |
| 29 | + } |
| 30 | + |
| 31 | + componentWillUnmount() { |
| 32 | + this.onSearchChange.cancel(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Debounced function |
| 37 | + * |
| 38 | + * @returns {Promise<void>} |
| 39 | + */ |
| 40 | + async onSearchChange() { |
| 41 | + const { |
| 42 | + attributes: { search } |
| 43 | + } = this.props; |
| 44 | + |
| 45 | + const pagination = this.state.pagination / this.GIPHY_RESULTS_LIMIT; |
| 46 | + |
| 47 | + const results = await this.fetchGiphy( search, pagination ); |
| 48 | + |
| 49 | + if ( 200 !== results.meta.status ) { |
| 50 | + // TODO handle error. |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // The idea is to 'cache' the past results in the state. |
| 55 | + // Something like |
| 56 | + // gifs[0] => Will contain results of pagination 0. |
| 57 | + // gifs[1] => Results of pagination 1. |
| 58 | + // and so on.. |
| 59 | + let gifs = this.state.gifs; |
| 60 | + gifs[ pagination ] = results.data; |
| 61 | + |
| 62 | + this.setState( { |
| 63 | + isLoading: false, |
| 64 | + pagination: pagination, |
| 65 | + gifs: gifs |
| 66 | + } ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Fetch data from Giphy. |
| 71 | + * |
| 72 | + * @param search string |
| 73 | + * @param pagination int |
| 74 | + * |
| 75 | + * @returns {Promise<any>} |
| 76 | + */ |
| 77 | + fetchGiphy( search, pagination ) { |
| 78 | + // Build the request url. |
| 79 | + const requestUrl = addQueryArgs( this.GIPHY_ENDPOINT, { |
| 80 | + q: search, |
| 81 | + limit: this.GIPHY_RESULTS_LIMIT, |
| 82 | + api_key: this.API_KEY, |
| 83 | + offset: pagination, |
| 84 | + } ); |
| 85 | + |
| 86 | + return fetch( requestUrl ) |
| 87 | + .then( data => data.json() ) |
| 88 | + .catch( error => error ); |
| 89 | + }; |
| 90 | + |
| 91 | + onGiphyClick( event, { photo } ) { |
| 92 | + // Save the selected photo data as `gif` in the DB. |
| 93 | + this.props.setAttributes( { gif: photo } ); |
| 94 | + this.setState( { isSearching: false } ); |
| 95 | + } |
| 96 | + |
| 97 | + onSearchChangeHandler( search ) { |
| 98 | + this.setState( { isLoading: true } ); |
| 99 | + // Save the search keyword as `search` in the DB. |
| 100 | + this.props.setAttributes( { search } ); |
| 101 | + this.onSearchChange(); |
| 102 | + } |
| 103 | + |
5 | 104 | render() { |
6 | 105 | const { |
7 | 106 | attributes: { |
8 | | - search |
| 107 | + search, |
| 108 | + gif, |
| 109 | + blockAlignment, |
| 110 | + textAlignment |
9 | 111 | }, |
10 | | - setAttributes, |
| 112 | + className, |
| 113 | + setAttributes |
11 | 114 | } = this.props; |
12 | 115 |
|
| 116 | + const { isLoading, isSearching, gifs, pagination } = this.state; |
| 117 | + |
13 | 118 | return ( |
14 | | - <TextControl |
15 | | - label="Search GIF" |
16 | | - value={ search } |
17 | | - onChange={ search => setAttributes( { search } )} |
18 | | - /> |
| 119 | + <div className={ className }> |
| 120 | + <BlockControls> |
| 121 | + <BlockAlignmentToolbar |
| 122 | + value={ blockAlignment } |
| 123 | + onChange={ blockAlignment => setAttributes( { blockAlignment } ) } |
| 124 | + /> |
| 125 | + <AlignmentToolbar |
| 126 | + value={ textAlignment } |
| 127 | + onChange={ textAlignment => setAttributes( { textAlignment } ) } |
| 128 | + /> |
| 129 | + </BlockControls> |
| 130 | + |
| 131 | + { isSearching ? ( |
| 132 | + <SearchGiphy |
| 133 | + search={ search } |
| 134 | + onSearchChangeHandler={ this.onSearchChangeHandler } |
| 135 | + isLoading={ isLoading } |
| 136 | + gifs={ gifs } |
| 137 | + onGiphyClick={ this.onGiphyClick } |
| 138 | + pagination={ pagination } |
| 139 | + /> |
| 140 | + ) : ( |
| 141 | + <img src={ gif.src } /> |
| 142 | + ) } |
| 143 | + </div> |
19 | 144 | ); |
20 | 145 | } |
21 | 146 | } |
0 commit comments