Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Began working on Search functionality #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ local.properties
node_modules/
npm-debug.log
yarn-error.log
yarn.lock
package-lock.json

# BUCK
buck-out/
Expand Down
1 change: 1 addition & 0 deletions app/config/default.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"api": "https://hacker-news.firebaseio.com/v0",
"search": "http://hn.algolia.com/api/v1/",
"base": "https://news.ycombinator.com",
"colors": {
"orange": "#EE6F2D",
Expand Down
31 changes: 31 additions & 0 deletions app/helpers/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import cheerio from 'cheerio-without-node-native';
import config from '../config/default';

/**
* Gets post from given query
* @param {String} query The query string to search for
* @param {Object} options The options for the search, see the 'searchOptions' variable in helpers/api.js
* @return {Promise} Returns a promise
*/

const searchPost = (options = {}, query='') => {

const searchOptions = {
sortByDate: options.sortByDate || false,
pageNumber: options.pageNumber || 1,
tags: options.tag || 'story',
};

const hitsPerPage = '&hitsPerPage=25';

// There has to be better way to do this
// TODO: Refactor this
const dateParameter = searchOptions.sortByDate ? 'search_by_date' : 'search';
const queryParameter = `query=${query}`;
const tagParameter = `&tags=${searchOptions.tags}`

const searchUrl = `${config.search}${dateParameter}?${queryParameter}${hitsPerPage}${tagParameter}`;

return fetch(searchUrl)
.then(response => response.json())
.catch(error => error);
}

/**
* Get item from given ID
* @param {String} itemId The ID of the item to fetch
Expand Down Expand Up @@ -378,4 +408,5 @@ export {
logout,
getComments,
toggleComments,
searchPost,
};
99 changes: 96 additions & 3 deletions app/screens/Search.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,110 @@
import React from 'react';
import config from '../config/default';
import commonStyles from '../styles/common';
import { Text, View } from 'react-native';

import {
StyleSheet,
Text,
View,
ScrollView
} from 'react-native';
import SearchBar from 'react-native-search-bar' // https://www.npmjs.com/package/react-native-search-bar

import { searchPost , getItems} from '../helpers/api';
import {posts} from '../reducers/items';

export default class Search extends React.Component {
constructor(props) {
super(props);

this.state = {
searchResults: [],
};

this.onSearchTextChanged = this.onSearchTextChanged.bind(this);
}

onSearchTextChanged(searchBarText) {
// There's no point in us searching for nothing
// If they don't have anything in the search box, dont bother showing previous search results
// Of course this can always be changed, I don't know if thats proper functionality :p
if (searchBarText.length !== 0) {
searchPost({}, searchBarText).then(responseJson => {
this.setState({
searchResults: responseJson.hits,
});
});
} else if (searchBarText.length === 0) {
this.setState({
searchResults: [],
});
}
}

render() {
const { searchResults } = this.state;
return (
<View>
<Text>Search Screen</Text>
<View style={commonStyles.flex}>
<SearchBar
ref='SearchBar'
placeholder='Search'
onChangeText={this.onSearchTextChanged}
/>
<ScrollView style={styles.searchContainer}>
<View>
{searchResults.map(result => (
<Text key={result.title}>{result.title}</Text>
))}
</View>
</ScrollView>
</View>
);
}
}


const styles = StyleSheet.create({
searchContainer: {
flex: 1,
padding: 10,
paddingTop: 15,
paddingLeft: 15,
paddingRight: 15,
backgroundColor: 'white',
height: '100%',
width: '100%',
},
header: {
fontSize: 18,
fontWeight: 'bold',
},
headerContainer: {
marginBottom: 10,
},
subHeader: {
padding: 15,
paddingLeft: 10,
paddingRight: 10,
fontSize: 14,
opacity: 0.8,
},
subHeaderContainer: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
backgroundColor: config.colors.mediumGray,
borderColor: config.colors.lightGray,
borderWidth: 1,
borderRadius: 5,
},
rightArrowContainer: {
flexGrow: 1,
},
rightArrow: {
opacity: 0.6,
width: 20,
height: 20,
marginRight: 10,
alignSelf: 'flex-end',
},
});
18 changes: 17 additions & 1 deletion ios/hackd.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
};
objectVersion = 46;
objects = {

/* Begin PBXBuildFile section */
00C302E51ABCBA2D00DB3ED1 /* libRCTActionSheet.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302AC1ABCB8CE00DB3ED1 /* libRCTActionSheet.a */; };
00C302E71ABCBA2D00DB3ED1 /* libRCTGeolocation.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302BA1ABCB90400DB3ED1 /* libRCTGeolocation.a */; };
Expand Down Expand Up @@ -40,6 +39,7 @@
A1622F732036CA2600B52F96 /* libReactNativeHaptic.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A1622F6F2036CA0E00B52F96 /* libReactNativeHaptic.a */; };
A1FE952E2036944300B1087E /* libReactNativeNavigation.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A1FE952B2036942700B1087E /* libReactNativeNavigation.a */; };
ADBDB9381DFEBF1600ED6528 /* libRCTBlob.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADBDB9271DFEBF0700ED6528 /* libRCTBlob.a */; };
4956C8E817984D44AF15080B /* libRNSearchBar.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 881264E1C22747F5B0A84608 /* libRNSearchBar.a */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -378,6 +378,8 @@
ADBDB91F1DFEBF0600ED6528 /* RCTBlob.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTBlob.xcodeproj; path = "../node_modules/react-native/Libraries/Blob/RCTBlob.xcodeproj"; sourceTree = "<group>"; };
C2CF41F2E4494C6681665E2D /* RNTableView.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RNTableView.xcodeproj; path = "../node_modules/react-native-tableview/RNTableView.xcodeproj"; sourceTree = "<group>"; };
EA8641CF51434BDFBD70E90B /* libRNTableView.a */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = archive.ar; path = libRNTableView.a; sourceTree = "<group>"; };
8DF4D81DB4EF4E99BD826526 /* RNSearchBar.xcodeproj */ = {isa = PBXFileReference; name = "RNSearchBar.xcodeproj"; path = "../node_modules/react-native-search-bar/ios/RNSearchBar.xcodeproj"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = wrapper.pb-project; explicitFileType = undefined; includeInIndex = 0; };
881264E1C22747F5B0A84608 /* libRNSearchBar.a */ = {isa = PBXFileReference; name = "libRNSearchBar.a"; path = "libRNSearchBar.a"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -410,6 +412,7 @@
139FDEF61B0652A700C62182 /* libRCTWebSocket.a in Frameworks */,
2DFA5FC5A9DB4F089EDF3478 /* libRNTableView.a in Frameworks */,
86411DEED3CA41479F95BD16 /* libSafariViewManager.a in Frameworks */,
4956C8E817984D44AF15080B /* libRNSearchBar.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -598,6 +601,7 @@
139FDEE61B06529A00C62182 /* RCTWebSocket.xcodeproj */,
C2CF41F2E4494C6681665E2D /* RNTableView.xcodeproj */,
94E0F0BEF8BC4DB495D8AD86 /* SafariViewManager.xcodeproj */,
8DF4D81DB4EF4E99BD826526 /* RNSearchBar.xcodeproj */,
);
name = Libraries;
sourceTree = "<group>";
Expand Down Expand Up @@ -1295,6 +1299,7 @@
"$(inherited)",
"$(SRCROOT)/../node_modules/react-native-tableview/RNTableView",
"$(SRCROOT)/../node_modules/react-native-safari-view",
"$(SRCROOT)/../node_modules/react-native-search-bar/ios",
);
INFOPLIST_FILE = hackdTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
Expand All @@ -1303,6 +1308,7 @@
"$(inherited)",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
OTHER_LDFLAGS = (
"-ObjC",
Expand All @@ -1323,6 +1329,7 @@
"$(inherited)",
"$(SRCROOT)/../node_modules/react-native-tableview/RNTableView",
"$(SRCROOT)/../node_modules/react-native-safari-view",
"$(SRCROOT)/../node_modules/react-native-search-bar/ios",
);
INFOPLIST_FILE = hackdTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
Expand All @@ -1331,6 +1338,7 @@
"$(inherited)",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
OTHER_LDFLAGS = (
"-ObjC",
Expand All @@ -1353,6 +1361,7 @@
"$(inherited)",
"$(SRCROOT)/../node_modules/react-native-tableview/RNTableView",
"$(SRCROOT)/../node_modules/react-native-safari-view",
"$(SRCROOT)/../node_modules/react-native-search-bar/ios",
);
INFOPLIST_FILE = hackd/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -1377,6 +1386,7 @@
"$(inherited)",
"$(SRCROOT)/../node_modules/react-native-tableview/RNTableView",
"$(SRCROOT)/../node_modules/react-native-safari-view",
"$(SRCROOT)/../node_modules/react-native-search-bar/ios",
);
INFOPLIST_FILE = hackd/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down Expand Up @@ -1407,13 +1417,15 @@
"$(inherited)",
"$(SRCROOT)/../node_modules/react-native-tableview/RNTableView",
"$(SRCROOT)/../node_modules/react-native-safari-view",
"$(SRCROOT)/../node_modules/react-native-search-bar/ios",
);
INFOPLIST_FILE = "hackd-tvOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
OTHER_LDFLAGS = (
"-ObjC",
Expand Down Expand Up @@ -1444,13 +1456,15 @@
"$(inherited)",
"$(SRCROOT)/../node_modules/react-native-tableview/RNTableView",
"$(SRCROOT)/../node_modules/react-native-safari-view",
"$(SRCROOT)/../node_modules/react-native-search-bar/ios",
);
INFOPLIST_FILE = "hackd-tvOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
OTHER_LDFLAGS = (
"-ObjC",
Expand Down Expand Up @@ -1482,6 +1496,7 @@
"$(inherited)",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
PRODUCT_BUNDLE_IDENTIFIER = "com.facebook.REACT.hackd-tvOSTests";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -1509,6 +1524,7 @@
"$(inherited)",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
PRODUCT_BUNDLE_IDENTIFIER = "com.facebook.REACT.hackd-tvOSTests";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
Loading