Skip to content

Commit 663133a

Browse files
committed
Working search and result display
1 parent e634746 commit 663133a

File tree

11 files changed

+349
-88
lines changed

11 files changed

+349
-88
lines changed

example/bundle.js

Lines changed: 114 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -954,28 +954,42 @@ var lib_1 = __webpack_require__(15);
954954
var React = __webpack_require__(4);
955955
var ReactDOM = __webpack_require__(19);
956956
// feel free to change these :)
957-
var suggestions = ["stop it", "nice one", "look up", "no", "bad"];
957+
var suggestions = ["watching", "quiz", "stop it", "nice one", "learn", "no", "read", "work"];
958958
var ExampleApp = /** @class */ (function (_super) {
959959
__extends(ExampleApp, _super);
960960
function ExampleApp(props) {
961961
var _this = _super.call(this, props) || this;
962962
_this.state = {
963-
apiKey: ""
963+
apiKey: "",
964+
isKeySubmitted: false
964965
};
965966
_this.onKeyChange = _this.onKeyChange.bind(_this);
967+
_this.onKeySubmit = _this.onKeySubmit.bind(_this);
968+
_this.onGifSelected = _this.onGifSelected.bind(_this);
966969
return _this;
967970
}
968971
ExampleApp.prototype.onKeyChange = function (event) {
969972
this.setState({ apiKey: event.target.value });
970973
};
974+
ExampleApp.prototype.onKeySubmit = function (event) {
975+
event.preventDefault();
976+
this.setState({
977+
isKeySubmitted: true
978+
});
979+
};
980+
ExampleApp.prototype.onGifSelected = function (gifObject) {
981+
console.dir(gifObject);
982+
};
971983
ExampleApp.prototype.render = function () {
972-
var apiKey = this.state.apiKey;
984+
var _a = this.state, apiKey = _a.apiKey, isKeySubmitted = _a.isKeySubmitted;
973985
var suggestions = this.props.suggestions;
986+
if (!isKeySubmitted) {
987+
return (React.createElement("form", { onSubmit: this.onKeySubmit },
988+
React.createElement("input", { type: "text", placeholder: "Enter your Giphy API Key", value: apiKey, onChange: this.onKeyChange }),
989+
React.createElement("button", { type: "submit" }, "Set API Key")));
990+
}
974991
return (React.createElement("div", null,
975-
React.createElement("div", null,
976-
React.createElement("input", { type: "text", placeholder: "Enter your Giphy API Key", value: apiKey, onChange: this.onKeyChange })),
977-
React.createElement("div", null,
978-
React.createElement(lib_1.Selector, { apiKey: apiKey, suggestions: suggestions }))));
992+
React.createElement(lib_1.Selector, { apiKey: apiKey, suggestions: suggestions, onGifSelected: this.onGifSelected })));
979993
};
980994
return ExampleApp;
981995
}(React.Component));
@@ -18764,6 +18778,7 @@ var types_1 = __webpack_require__(16);
1876418778
var GiphyClient_1 = __webpack_require__(17);
1876518779
var SearchInput_1 = __webpack_require__(33);
1876618780
var Suggestions_1 = __webpack_require__(34);
18781+
var SearchResults_1 = __webpack_require__(36);
1876718782
var Selector = /** @class */ (function (_super) {
1876818783
__extends(Selector, _super);
1876918784
function Selector(props) {
@@ -18787,9 +18802,9 @@ var Selector = /** @class */ (function (_super) {
1878718802
* search
1878818803
* @param q string
1878918804
*/
18790-
Selector.prototype.onQueryChange = function (q) {
18805+
Selector.prototype.onQueryChange = function (q, cb) {
1879118806
// Update the query
18792-
this.setState({ query: q });
18807+
this.setState({ query: q }, cb);
1879318808
};
1879418809
/**
1879518810
* Fired when the query should be executed
@@ -18827,20 +18842,24 @@ var Selector = /** @class */ (function (_super) {
1882718842
* Fired when a suggestion has been selected
1882818843
*/
1882918844
Selector.prototype.onSuggestionSelected = function (q) {
18830-
this.onQueryChange(q);
18831-
this.onQueryExecute();
18845+
var _this = this;
18846+
this.onQueryChange(q, function () {
18847+
_this.onQueryExecute();
18848+
});
1883218849
};
1883318850
Selector.prototype.render = function () {
1883418851
var _a = this.state, query = _a.query, searchResult = _a.searchResult, isPending = _a.isPending, searchError = _a.searchError;
18835-
var suggestions = this.props.suggestions;
18852+
var _b = this.props, suggestions = _b.suggestions, onGifSelected = _b.onGifSelected;
1883618853
var showSuggestions = !!suggestions.length && !searchResult && !isPending && !searchError;
1883718854
return (React.createElement("div", null,
1883818855
React.createElement(SearchInput_1.SearchInput, { onQueryChange: this.onQueryChange, onQueryExecute: this.onQueryExecute, queryValue: query }),
1883918856
showSuggestions && (React.createElement(Suggestions_1.Suggestions, { suggestions: suggestions, onSuggestionSelected: this.onSuggestionSelected })),
1884018857
isPending && React.createElement("div", null, "Loading"),
1884118858
!isPending && !!searchError && React.createElement("div", null,
1884218859
"Error: ",
18843-
searchError.message)));
18860+
searchError.message),
18861+
!isPending &&
18862+
!!searchResult && (React.createElement(SearchResults_1.SearchResults, { gifObjects: searchResult.gifObjects, onGifSelected: onGifSelected }))));
1884418863
};
1884518864
Selector.defaultProps = {
1884618865
rating: types_1.Rating.G,
@@ -20375,17 +20394,7 @@ var GiphyClient = /** @class */ (function () {
2037520394
*/
2037620395
GiphyClient.prototype.searchGifs = function (params) {
2037720396
return this.client.search("gifs", params).then(function (response) {
20378-
return response.data.map(function (rawGifObject) {
20379-
return {
20380-
id: rawGifObject.id,
20381-
slug: rawGifObject.slug,
20382-
url: rawGifObject.url,
20383-
embedUrl: rawGifObject.embed_url,
20384-
source: rawGifObject.source,
20385-
rating: rawGifObject.rating,
20386-
title: rawGifObject.title
20387-
};
20388-
});
20397+
return { gifObjects: response.data };
2038920398
});
2039020399
};
2039120400
return GiphyClient;
@@ -22955,14 +22964,93 @@ var Suggestion = /** @class */ (function (_super) {
2295522964
this.props.onSelected(this.props.suggestion);
2295622965
};
2295722966
Suggestion.prototype.render = function () {
22958-
var _a = this.props, suggestion = _a.suggestion, onSelected = _a.onSelected;
22959-
return React.createElement("a", { href: "javascript:void(0)", onClick: this.onClick }, suggestion);
22967+
var suggestion = this.props.suggestion;
22968+
return (React.createElement("a", { href: "javascript:void(0)", onClick: this.onClick }, suggestion));
2296022969
};
2296122970
return Suggestion;
2296222971
}(React.Component));
2296322972
exports.Suggestion = Suggestion;
2296422973

2296522974

22975+
/***/ }),
22976+
/* 36 */
22977+
/***/ (function(module, exports, __webpack_require__) {
22978+
22979+
"use strict";
22980+
22981+
var __extends = (this && this.__extends) || (function () {
22982+
var extendStatics = Object.setPrototypeOf ||
22983+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
22984+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
22985+
return function (d, b) {
22986+
extendStatics(d, b);
22987+
function __() { this.constructor = d; }
22988+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
22989+
};
22990+
})();
22991+
Object.defineProperty(exports, "__esModule", { value: true });
22992+
var React = __webpack_require__(1);
22993+
var SearchResult_1 = __webpack_require__(37);
22994+
var SearchResults = /** @class */ (function (_super) {
22995+
__extends(SearchResults, _super);
22996+
function SearchResults(props) {
22997+
return _super.call(this, props) || this;
22998+
}
22999+
SearchResults.prototype.render = function () {
23000+
var _a = this.props, gifObjects = _a.gifObjects, onGifSelected = _a.onGifSelected;
23001+
return (React.createElement("div", null, gifObjects.map(function (gifObject) { return (React.createElement(SearchResult_1.SearchResult, { key: gifObject.id, gifObject: gifObject, onSelected: onGifSelected })); })));
23002+
};
23003+
return SearchResults;
23004+
}(React.Component));
23005+
exports.SearchResults = SearchResults;
23006+
23007+
23008+
/***/ }),
23009+
/* 37 */
23010+
/***/ (function(module, exports, __webpack_require__) {
23011+
23012+
"use strict";
23013+
23014+
var __extends = (this && this.__extends) || (function () {
23015+
var extendStatics = Object.setPrototypeOf ||
23016+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
23017+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
23018+
return function (d, b) {
23019+
extendStatics(d, b);
23020+
function __() { this.constructor = d; }
23021+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
23022+
};
23023+
})();
23024+
Object.defineProperty(exports, "__esModule", { value: true });
23025+
var React = __webpack_require__(1);
23026+
var SearchResult = /** @class */ (function (_super) {
23027+
__extends(SearchResult, _super);
23028+
function SearchResult(props) {
23029+
var _this = _super.call(this, props) || this;
23030+
_this.onClick = _this.onClick.bind(_this);
23031+
return _this;
23032+
}
23033+
SearchResult.prototype.onClick = function (event) {
23034+
event.preventDefault();
23035+
this.props.onSelected(this.props.gifObject);
23036+
};
23037+
SearchResult.prototype.render = function () {
23038+
var gifObject = this.props.gifObject;
23039+
console.dir(gifObject);
23040+
var sourceImage = gifObject.images.fixed_height_small;
23041+
var style = {
23042+
width: sourceImage.width + "px",
23043+
height: sourceImage.height + "px",
23044+
background: "url(" + sourceImage.gif_url + ")",
23045+
display: "block"
23046+
};
23047+
return React.createElement("a", { href: "javascript:void(0)", onClick: this.onClick, style: style });
23048+
};
23049+
return SearchResult;
23050+
}(React.Component));
23051+
exports.SearchResult = SearchResult;
23052+
23053+
2296623054
/***/ })
2296723055
/******/ ]);
2296823056
});

example/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/example.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import * as React from 'react';
44
import * as ReactDOM from 'react-dom';
55

66
// feel free to change these :)
7-
const suggestions = ["stop it", "nice one", "look up", "no", "bad"];
7+
const suggestions = ["watching", "quiz", "stop it", "nice one", "learn", "no", "read", "work"];
88

99
interface IExampleProps {
1010
suggestions: string[];
1111
}
1212

1313
interface IExampleState {
1414
apiKey: string;
15+
isKeySubmitted: boolean;
16+
1517
}
1618

1719
class ExampleApp extends React.Component<IExampleProps, IExampleState> {
@@ -21,30 +23,47 @@ class ExampleApp extends React.Component<IExampleProps, IExampleState> {
2123
super(props);
2224

2325
this.state = {
24-
apiKey: ""
26+
apiKey: "",
27+
isKeySubmitted: false
2528
};
2629

2730
this.onKeyChange = this.onKeyChange.bind(this);
31+
this.onKeySubmit = this.onKeySubmit.bind(this);
32+
this.onGifSelected = this.onGifSelected.bind(this);
2833
}
2934

3035
public onKeyChange (event: any): void {
3136
this.setState({apiKey: event.target.value});
3237
}
3338

39+
public onKeySubmit (event: any): void {
40+
event.preventDefault();
41+
42+
this.setState({
43+
isKeySubmitted: true
44+
});
45+
}
46+
47+
public onGifSelected (gifObject: any): void {
48+
console.dir(gifObject);
49+
}
50+
3451
public render (): JSX.Element {
35-
const {apiKey} = this.state;
52+
const {apiKey, isKeySubmitted} = this.state;
3653
const {suggestions} = this.props;
3754

38-
// todo: This pattern will not work as the API key needs to be set before selector is constructed
55+
if (!isKeySubmitted) {
56+
return (
57+
<form onSubmit={this.onKeySubmit}>
58+
<input type="text" placeholder="Enter your Giphy API Key" value={apiKey} onChange={this.onKeyChange}/>
59+
<button type="submit">Set API Key</button>
60+
</form>
61+
);
62+
}
3963

4064
return (
4165
<div>
42-
<div>
43-
<input type="text" placeholder="Enter your Giphy API Key" value={apiKey} onChange={this.onKeyChange}/>
44-
</div>
45-
<div>
46-
<Selector apiKey={apiKey} suggestions={suggestions}/>
47-
</div>
66+
<Selector apiKey={apiKey} suggestions={suggestions} onGifSelected={this.onGifSelected}/>
4867
</div>
4968
);
5069
}

0 commit comments

Comments
 (0)