-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchScreen.js
117 lines (102 loc) · 2.69 KB
/
SearchScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React, { Component } from "react";
import {
Platform,
StyleSheet,
Text,
View,
FlatList,
Image,
Alert
} from "react-native";
//import { DrawerNavigator } from 'react-navigation'
const numColumns = 2;
import { PininterestComponent } from "./src/Component/PininterestComponent";
import {CommonHeader} from "./src/Component/CommonHeader";
import { CommonHeaderWithTextInput } from "./src/Component/CommonHeaderWithTextInput";
export default class SearchScreen extends Component {
constructor(props) {
super(props);
this.state = {
isSearchClicked: false,
arrayholder: [],
dataSource:[]
};
}
componentDidMount() {
return fetch("https://demo6231795.mockable.io")
.then(response => response.json())
.then(response => {
this.setState(
{
dataSource: response.resource_data_cache[0].data.results,
arrayholder: response.resource_data_cache[0].data.results
},
function() {}
);
})
.catch(error => {
console.error(error);
});
}
render() {
return (
<View style={{flex:1}}>
{this.changeLayoutOnSearchClick(this.state.isSearchClicked)}
<FlatList
contentContainerStyle={{
alignItems: "center",
backgroundColor: "white",
}}
data={this.state.arrayholder}
keyExtractor={(item, index) => item.id}
renderItem={({ item }) => this.renderItem(item)}
numColumns={numColumns}
/>
</View>
);
}
renderItem(item, item1) {
return (
<PininterestComponent
data={item}
width={item.images["170x"].width}
height={item.images["170x"].height}
name={item.pinner.username}
/>
);
}
changeLayoutOnSearchClick(isSearchClicked) {
if (isSearchClicked == true) {
return (
<CommonHeaderWithTextInput
action={this.showCommonHeader}
text={this.getSearchText}
/>
);
} else {
return <CommonHeader action={this.handler} navigation={this.props.navigation}/>;
}
}
handler=()=> {
this.setState({ isSearchClicked: true });
}
showCommonHeader=()=> {
this.setState({ isSearchClicked: false });
}
getSearchText = (text) => {
if (text != "") {
const filtered_data = this.state.dataSource.filter(data => {
if ((data.pinner.full_name.toLowerCase()).includes((text.toLowerCase()))) {
return data;
}
});
this.setState({
arrayholder: filtered_data
});
} else {
this.setState({
arrayholder: this.state.dataSource
});
}
};
}