From f89b09a004e5b870ff3063534fd4b5808b806544 Mon Sep 17 00:00:00 2001 From: Sarah's Mac Date: Thu, 20 Feb 2020 16:08:05 -0800 Subject: [PATCH 1/2] Fetch for Search Screen --- screens/SearchScreen.js | 43 ++++++++++++++++---------- server/controllers/searchController.js | 40 ++++++++++++------------ server/server.js | 4 +-- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/screens/SearchScreen.js b/screens/SearchScreen.js index 78d381f..524400b 100644 --- a/screens/SearchScreen.js +++ b/screens/SearchScreen.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { Container, Header, @@ -17,6 +17,16 @@ import { TextInput, Alert } from 'react-native'; const SearchScreen = ({ navigation }) => { const [searchInput, setSearchInput] = useState(''); + const [top, topOnChange] = useState(['CFL Lightbulbs', + 'Paper', + 'Aluminum Cans', + 'Cardboard', + 'Newspapers', + 'Plastic Bottles', + 'Glass Containers', + 'Glossy Magazines', + 'Oil-Based Paints', + 'Christmas Trees']); const handleItemPress = item => { navigation.navigate('Details', { @@ -26,24 +36,24 @@ const SearchScreen = ({ navigation }) => { const handleSearchPress = () => { if (searchInput !== '') { - // handle search + getSearched(); } else { Alert.alert('Invalid Input', 'Please enter a valid search') } } + const getSearched = () => { + /* get search result data from database */ + fetch(`http://192.168.0.108:3000/search/${id}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }) + .then(res => res.json()) + .then(res => res.rows) + .catch(err => console.log({ err: 'err in search get fetch' })) + } - const top = [ - 'CFL Lightbulbs', - 'Paper', - 'Aluminum Cans', - 'Cardboard', - 'Newspapers', - 'Plastic Bottles', - 'Glass Containers', - 'Glossy Magazines', - 'Oil-Based Paints', - 'Christmas Trees' - ]; const topList = top.map((item, i) => { return ( @@ -57,14 +67,15 @@ const SearchScreen = ({ navigation }) => { ); }); + return (
setSearchInput(text)} placeholder="Search" /> - - + + diff --git a/server/controllers/searchController.js b/server/controllers/searchController.js index cbafdf1..54be975 100644 --- a/server/controllers/searchController.js +++ b/server/controllers/searchController.js @@ -13,41 +13,43 @@ searchController.postSearch = (req, res, next) => { const values = [search_words, favorite_search, url, description, user_id, count_searches] db.query(text, values) - .then(result => { - res.locals.data = result.rows - return next() - }) - .catch(err => next(err)) + .then(result => { + res.locals.data = result.rows + return next() + }) + .catch(err => next(err)) } /* get seach word data from database */ searchController.getSearch = (req, res, next) => { + const id = req.params.id; + const values = [id] const text = ` SELECT * FROM "Search" WHERE user_id = ($1) - ` - - db.query(text) - .then(result => { - res.locals.data = result.rows - return next() - }) - .catch(err => next(err)) + ` + db.query(text, values) + .then(result => { + res.locals.data = result.rows + // console.log(result.rows) + return next() + }) + .catch(err => next(err)) } /* delete seach word data from database */ searchController.deleteSearch = (req, res, next) => { - const id = req.params.id + const id = req.params.id + + const values = [id] - const values = [id] - - const text = ` + const text = ` DELETE FROM "Search" WHERE search_id = ($1); ` - - db.query(text, values) + + db.query(text, values) .then(result => { return next() }) diff --git a/server/server.js b/server/server.js index 4687dd5..216d74f 100644 --- a/server/server.js +++ b/server/server.js @@ -40,8 +40,8 @@ app.post('/search', searchController.postSearch, (req, res) => { res.status(200).json(res.locals.data) }) /* route for search get */ -app.get('/search', searchController.getSearch, (req, res) => { - res.json(res.locals.data) +app.get('/search/:id', searchController.getSearch, (req, res) => { + res.status(200).json(res.locals.data) }) /* route for search delete */ app.delete('/search:id', searchController.deleteSearch, (req, res) => { From 382c2e09e00fcfea2ca3afd22df7c273f511c282 Mon Sep 17 00:00:00 2001 From: Sarah's Mac Date: Thu, 20 Feb 2020 18:14:35 -0800 Subject: [PATCH 2/2] fixed bug --- screens/DetailsScreen.js | 22 ++++++------- screens/LoginScreen.js | 71 ++++++++++++++++++++++++++++++++-------- 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/screens/DetailsScreen.js b/screens/DetailsScreen.js index 7a3662c..0a30c05 100644 --- a/screens/DetailsScreen.js +++ b/screens/DetailsScreen.js @@ -42,8 +42,8 @@ function renderContent(item) { {item.content} - @@ -52,15 +52,15 @@ function renderContent(item) { } export default function DetailsScreen({ route }) { - fetch(`http://192.168.0.108:3000/search/${id}`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json' - } - }) - .then(res => res.json()) - .then(res => res.rows) - .catch(err => console.log({ err: 'err in search get fetch' })) + // fetch(`http://192.168.0.108:3000/search/${id}`, { + // method: 'GET', + // headers: { + // 'Content-Type': 'application/json' + // } + // }) + // .then(res => res.json()) + // .then(res => res) + // .catch(err => console.log({ err: 'err in search get fetch' })) console.log(route.params.item) return ( diff --git a/screens/LoginScreen.js b/screens/LoginScreen.js index e95ed27..492dabb 100644 --- a/screens/LoginScreen.js +++ b/screens/LoginScreen.js @@ -4,7 +4,8 @@ import { Text, Image, TextInput, - TouchableOpacity + TouchableOpacity, + StyleSheet } from 'react-native'; @@ -39,30 +40,74 @@ const LoginScreen = ({ navigation }) => { 'Content-Type': 'application/json' } }) - .then(res => res.json()) - .then(res => console.log('fetch res', res)) - .catch(err => console.log({err: 'err in login get fetch'})) + .then(res => res.json()) + .then(res => console.log('fetch res', res)) + .catch(err => console.log({ err: 'err in login get fetch' })) } return ( - + - setLoginState({ ...loginState, username: text })} style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} + setLoginState({ ...loginState, username: text })} placeholder="Username" /> - setLoginState({ ...loginState, password: text })} style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} + setLoginState({ ...loginState, password: text })} placeholder="Password" /> - - Login + + Login - - Forgot your login details?Get help signing in. + + Forgot your login details?Get help signing in. - Don't have an account?Sign Up. + Don't have an account?Sign Up. ) } -export default React.memo(LoginScreen); \ No newline at end of file +export default React.memo(LoginScreen); + +const styles = StyleSheet.create({ + container: { + backgroundColor: 'white', + width: 420, + alignItems: 'center', + justifyContent: 'center', + marginBottom: 80, + paddingTop: 125, + paddingBottom: 180, + }, + input: { + height: 40, + backgroundColor: 'rgb(255, 255, 255)', + marginBottom: 20, + width: 200, + paddingHorizontal: 10, + color: 'black', + borderColor: 'gray', + borderWidth: 1 + }, + buttonContainer: { + backgroundColor: '#23A75B', + paddingVertical: 15, + borderRadius: 5, + marginTop: 10, + }, + buttonText: { + textAlign: 'center', + color: 'white', + fontWeight: '700', + fontSize: 16, + width: 175 + }, + addText: { + textAlign: 'center', + color: 'white', + fontWeight: '700', + fontSize: 16, + }, + text: { + marginTop: 25, + } +}); \ No newline at end of file