Skip to content

Commit

Permalink
Merge pull request instamobile#4 from SI669-classroom-f20/branch
Browse files Browse the repository at this point in the history
connect to firebase to search remotely
  • Loading branch information
Coldstream-Louis authored Dec 19, 2020
2 parents 4308008 + fb4b3d2 commit be2dcfd
Showing 1 changed file with 157 additions and 133 deletions.
290 changes: 157 additions & 133 deletions src/screens/Search/SearchScreen.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,157 @@
import React from 'react';
import {
FlatList,
Text,
View,
Image,
TouchableHighlight
} from 'react-native';
import styles from './styles';
import { ListItem, SearchBar } from 'react-native-elements';
import MenuImage from '../../components/MenuImage/MenuImage';
import {
getCategoryName,
getRecipesByRecipeName,
getRecipesByCategoryName,
getRecipesByIngredientName
} from '../../data/MockDataAPI';

export default class SearchScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerRight: (
<MenuImage
onPress={() => {
navigation.openDrawer();
}}
/>
)
};
};

constructor(props) {
super(props);
this.state = {
value: '',
data: []
};
}

componentDidMount() {
const { navigation } = this.props;
navigation.setParams({
handleSearch: this.handleSearch,
data: this.getValue()
});
}

handleSearch = (text) => {
var recipeArray1 = getRecipesByRecipeName(text);
var recipeArray2 = getRecipesByCategoryName(text);
// var recipeArray3 = getRecipesByIngredientName(text);
var aux = recipeArray1.concat(recipeArray2);
var recipeArray = [...new Set(aux)];
if (text == '') {
this.setState({
value: text,
data: []
});
} else {
this.setState({
value: text,
data: recipeArray
});
}
};

getValue = () => {
return this.state.value;
};

onPressRecipe = item => {
this.props.navigation.navigate('Recipe', { item });
};

renderRecipes = ({ item }) => (
<TouchableHighlight underlayColor='rgba(73,182,77,0.9)' onPress={() => this.onPressRecipe(item)}>
<View style={styles.container}>
<Image style={styles.photo} source={{ uri: item.photo_url }} />
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.category}>{getCategoryName(item.categoryId)}</Text>
</View>
</TouchableHighlight>
);

render() {
return (

<View>
<View style={styles.searchContainer}>
<SearchBar
containerStyle={{
backgroundColor: 'transparent',
borderBottomColor: 'transparent',
borderTopColor: 'transparent',
flex: 1,
marginTop: 50,
width: 300
}}
inputContainerStyle={{
backgroundColor: 'white',
height: 40
}}
inputStyle={{
backgroundColor: 'white',
borderRadius: 10,
color: 'black'
}}
searchIcond
clearIcon
//lightTheme
round
onChangeText={this.handleSearch}
//onClear={() => params.handleSearch('')}
placeholder="Search"
value={this.state.value}
/>
</View>
<View style={styles.listContainer}>
<FlatList
vertical
showsVerticalScrollIndicator={false}
numColumns={2}
data={this.state.data}
renderItem={this.renderRecipes}
keyExtractor={item => `${item.recipeId}`}
/>
</View>

</View>
);
}
}
import React from 'react';
import {
FlatList,
Text,
View,
Image,
TouchableHighlight
} from 'react-native';
import styles from './styles';
import { ListItem, SearchBar } from 'react-native-elements';
import MenuImage from '../../components/MenuImage/MenuImage';
import { getDataModel } from '../../data/dataModel';



export default class SearchScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerRight: (
<MenuImage
onPress={() => {
navigation.openDrawer();
}}
/>
)
};
};

constructor(props) {
super(props);
this.dataModel = getDataModel();
let recipes=this.dataModel.getRecipes();
this.state = {
MyRecipes:recipes,
value: '',
data: []
};
}

getRecipesByCategoryName(categoryName) {
let nameUpper = categoryName.toUpperCase();
let recipesArray = [];
this.state.MyRecipes.map(data => {
if (data.category.toUpperCase().includes(nameUpper)) {
recipesArray.push(data); // return a vector of recipes
}
});
return recipesArray;
}

getRecipesByRecipeName(recipeName) {
let nameUpper = recipeName.toUpperCase();
let recipesArray = [];
this.state.MyRecipes.map(data => {
if (data.title.toUpperCase().includes(nameUpper)) {
recipesArray.push(data);
}
});
return recipesArray;
}

componentDidMount() {
const { navigation } = this.props;
navigation.setParams({
handleSearch: this.handleSearch,
data: this.getValue()
});
}

handleSearch = (text) => {
var recipeArray1 = this.getRecipesByRecipeName(text);
var recipeArray2 = this.getRecipesByCategoryName(text);
// var recipeArray3 = getRecipesByIngredientName(text);
var aux = recipeArray1.concat(recipeArray2);
var recipeArray = [...new Set(aux)];
if (text == '') {
this.setState({
value: text,
data: []
});
} else {
this.setState({
value: text,
data: recipeArray
});
}
};

getValue = () => {
return this.state.value;
};

onPressRecipe = item => {
this.props.navigation.navigate('Recipe', { item });
};

renderRecipes = ({ item }) => (
<TouchableHighlight underlayColor='rgba(73,182,77,0.9)' onPress={() => this.onPressRecipe(item)}>
<View style={styles.container}>
<Image style={styles.photo} source={{ uri: item.image_url }} />
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.category}>{item.category}</Text>
</View>
</TouchableHighlight>
);




render() {
return (

<View>
<View style={styles.searchContainer}>
<SearchBar
containerStyle={{
backgroundColor: 'transparent',
borderBottomColor: 'transparent',
borderTopColor: 'transparent',
flex: 1,
marginTop: 50,
width: 300
}}
inputContainerStyle={{
backgroundColor: 'white',
height: 40
}}
inputStyle={{
backgroundColor: 'white',
borderRadius: 10,
color: 'black'
}}
searchIcond
clearIcon
//lightTheme
round
onChangeText={this.handleSearch}
//onClear={() => params.handleSearch('')}
placeholder="Search"
value={this.state.value}
/>
</View>
<View style={styles.listContainer}>
<FlatList
vertical
showsVerticalScrollIndicator={false}
numColumns={2}
data={this.state.data}
renderItem={this.renderRecipes}
/>
</View>

</View>
);
}
}

0 comments on commit be2dcfd

Please sign in to comment.