From 28f45e461167c04f99c731f0f7d9e3a7613cfd31 Mon Sep 17 00:00:00 2001 From: tk Date: Sat, 24 Jun 2017 21:30:10 -0400 Subject: [PATCH] handle error and slow connections --- src/components/AutoSuggest.js | 78 ++++++++++++++++++++++------------- tasks.todo | 5 ++- 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/components/AutoSuggest.js b/src/components/AutoSuggest.js index b60e3dd..6a5786f 100644 --- a/src/components/AutoSuggest.js +++ b/src/components/AutoSuggest.js @@ -21,6 +21,17 @@ export default class Algo extends Component { }, }); } + static generateVariants(variants) { + const fontVariants = variants.map((variant, vindex) => ( + + )); + return fontVariants; + } static formatVariants(variants) { const formatted = variants.map((variant) => { const fontInfo = {}; @@ -40,6 +51,8 @@ export default class Algo extends Component { constructor(props) { super(props); this.state = { + isLoading: true, + errorHappend: false, value: '', suggestions: [{ family: 'Loading' }], selectedFontVariants: [], @@ -76,7 +89,15 @@ export default class Algo extends Component { } updateSuggestions(searchObject) { index.search(searchObject.value, (err, content) => { + if (err) { + this.setState({ + errorHappend: true, + isLoading: false, + }); + return; + } this.setState({ + isLoading: false, suggestions: content.hits, }); }); @@ -96,15 +117,8 @@ export default class Algo extends Component { this.props.updateFontSize(santizedFontSize); } render() { - const { value, suggestions, selectedFontVariants, fontSize } = this.state; - const variants = selectedFontVariants.map((variant, vindex) => ( - - )); + const { isLoading, errorHappend, value, suggestions, selectedFontVariants, fontSize } = this.state; + const variants = Algo.generateVariants(selectedFontVariants); // only display the style selector only if they have selected a font let selectVariants = ''; @@ -119,7 +133,7 @@ export default class Algo extends Component { }, ]; defaultValues = defaultValues.map(val => ( - + )); selectVariants = ( @@ -131,25 +145,31 @@ export default class Algo extends Component { return (
- typeface.family} - shouldRenderSuggestions={() => true} - highlightFirstSuggestion - renderSuggestion={typeface => ( -
-
{typeface.family}
-
- )} - inputProps={{ - placeholder: 'Search Google fonts', - value, - onChange: this.handleChange, - }} - /> + { errorHappend ?
Refresh the page.
:
+ { + isLoading ?

Loading Fonts...

: + typeface.family} + shouldRenderSuggestions={() => true} + highlightFirstSuggestion + renderSuggestion={typeface => ( +
+
{typeface.family}
+
+ )} + inputProps={{ + placeholder: 'Search Google fonts', + value, + onChange: this.handleChange, + }} + /> + } +
+ }
{selectVariants}
diff --git a/tasks.todo b/tasks.todo index 5f11e79..5d24db8 100644 --- a/tasks.todo +++ b/tasks.todo @@ -6,6 +6,7 @@ ✔ Work on font size @done(2017-06-18 15:27) ✔ Work on color picker @done(2017-06-19 20:55) ✔ Work on backgroundColor @done(2017-06-20 21:22) +✔ Handle slow or no connection and it SHOULD NOT FAIL / it should say no connection or sth @done(2017-06-24 19:26) ☐ Style font selector: ✔ Style the text input @done(2017-06-18 15:27) @project(☐ Style font selector) ✔ Style the autocomplete @done(2017-06-18 15:27) @project(☐ Style font selector) @@ -13,8 +14,8 @@ ☐ Give credit to Algolia <3 ✔ Style font style @done(2017-06-18 15:27) ✔ Style font size @done(2017-06-18 15:27) -☐ Style color picker -☐ Style backgroundColor picker +✔ Style color picker @done(2017-06-21 20:50) +✔ Style backgroundColor picker @done(2017-06-21 20:50) ☐ Add clear button to font search (x) ☐ Make look decent on mobile ☐ Add more colors to github-picker from coolors