Skip to content

Commit

Permalink
Rename getCurrentSearchString.js to getData.js, promisify things, che…
Browse files Browse the repository at this point in the history
…ck if search query data is found from storage #9
  • Loading branch information
0is1 committed Aug 18, 2016
1 parent cbd1b3c commit 7776399
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
10 changes: 0 additions & 10 deletions app/scripts/lib/getCurrentSearchString.js

This file was deleted.

11 changes: 11 additions & 0 deletions app/scripts/lib/getData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SEARCH_KEY_NAME } from "../constants";
import browserObject from "./browserObject";
const getData = (key) => {
return new Promise((resolve, reject) => {
browserObject.storage.local.get(key, (items) => {
resolve(items[key]);
});
});
};

export default getData;
8 changes: 6 additions & 2 deletions app/scripts/lib/setData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SEARCH_RESULTS_OPEN_KEY_NAME, SEARCH_KEY_NAME, BEER_KEY_NAME } from "../constants";
import getCurrentSearchString from "./getCurrentSearchString";
import getData from "./getData";
import search from "../search";
import browserObject from "./browserObject";

Expand All @@ -11,12 +11,16 @@ function setData(key, data) {
break;

case SEARCH_KEY_NAME:
getCurrentSearchString((searchQuery) => {
getData((SEARCH_KEY_NAME))
.then((searchQuery) => {
if (searchQuery === data){
search();
} else {
browserObject.storage.local.set({[SEARCH_KEY_NAME]: data}, function() {});
}
})
.catch((err) => {
console.error(err);
});
break;
}
Expand Down
48 changes: 32 additions & 16 deletions app/scripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@ import React from "react";
import { render } from "react-dom";
import SearchResults from "./components/SearchResults";
import { BEER_KEY_NAME, SEARCH_KEY_NAME, REACT_ROOT_DIV_ID } from "./constants";
import getCurrentSearchString from "./lib/getCurrentSearchString";
import getData from "./lib/getData";
import setData from "./lib/setData";
import ajaxSearch from "./lib/ajaxSearch";

/*
* Search beer data from Oluttamo API
*/
function search(){
getCurrentSearchString((searchQuery) =>{
if(searchQuery){
ajaxSearch(searchQuery)
.then((result) => {
if(result.length === 0){
addSearchResults(searchQuery, [], "404");
} else {
setData(BEER_KEY_NAME, {query: searchQuery, data: result, url: window.location.href});
addSearchResults(searchQuery, result);
}
})
.catch((err) => {
// TODO: better error message
console.error("Oh noes, something went wrong while fetching beers :(", err);
});
let q = "";
getData(SEARCH_KEY_NAME)
.then((searchQuery) => {
q = searchQuery;
if(!searchQuery){
throw{
err: "no search query found!",
};
}
return searchQuery;
})
.then((searchQuery) => {
return getData(searchQuery);
})
.then((resultFromStorage) => {
if(resultFromStorage && resultFromStorage.data){
return resultFromStorage.data;
} else {
return ajaxSearch(q);
}
})
.then((result) => {
if(result.length === 0){
addSearchResults(q, [], "404");
} else {
setData(BEER_KEY_NAME, {query: q, data: result, url: window.location.href});
addSearchResults(q, result);
}
})
.catch((err) => {
// TODO: better error message
console.error("Oh noes, something went wrong while fetching beers :(", err);
});
}

Expand Down

0 comments on commit 7776399

Please sign in to comment.