-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.js
97 lines (87 loc) · 2.73 KB
/
Core.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
import React from 'react';
import { View, Button, FlatList, TextInput } from 'react-native';
import { ZOMATO_API, ZOMATO_BASE_URL } from './constants';
import axios from 'axios';
import { Button as Btn } from 'react-native-paper';
import { removeDataFromLocal } from './Utils/Helpers';
export default class Core extends React.Component {
state = {
restaurants: [],
searchText: '',
start: 0,
pageSize: 10,
totalCount: 0
}
static navigationOptions = {
title: 'Home'
}
handleNextPage = () => {
this.setState({
start: this.state.start + this.state.pageSize
}, () => this.getRestFromZomato())
}
handlePrevPage = () => {
this.setState({
start: this.state.start - this.state.pageSize
}, () => this.getRestFromZomato())
}
getRestFromZomato = () => {
const url = `${ZOMATO_BASE_URL}/search?entity_id=14&entity_type=city&q=${this.state.searchText}&count=${this.state.pageSize}&start=${this.state.start}`
axios.get(url, { headers: { 'user-key': ZOMATO_API } }).then(
(res) => {
console.log('response is:', res)
this.setState({
restaurants: res.data.restaurants,
totalCount: res.data.results_found
})
}
).catch(
(err) => {
console.log('error', err)
}
)
}
handleChangeSearch = (text) => {
this.setState({
searchText: text
})
}
handleSubmit = () => {
this.setState({
start: 0
}, () => this.getRestFromZomato())
}
removeitem = async () => {
await removeDataFromLocal('user')
this.props.navigation.navigate('Auth')
}
render() {
console.log('render called')
return (
<View>
<TextInput
onChangeText={(v) => this.handleChangeSearch(v)}
value={this.state.searchText}
onSubmitEditing={() => this.handleSubmit()}
placeholder='Search restaurant' />
<FlatList
data={this.state.restaurants}
keyExtractor={(item, index) => item.restaurant.id.toString()}
renderItem={({ item }) => <Button title={item.restaurant.name}
onPress={() => this.props.navigation.navigate('Restaurant', { 'id': item.restaurant.id })} />}
/>
<Btn icon="chevron-left" color={'indigo'} mode="contained" dark={true}
onPress={() => this.handlePrevPage()}
disabled={this.state.start < 10}
>Prev</Btn>
<Btn icon="chevron-right" color={'indigo'} mode="contained" dark={true}
disabled={this.state.start > (this.state.totalCount - this.state.pageSize)}
onPress={() => this.handleNextPage()}>Next</Btn>
<Btn onPress={() => this.removeitem()}>Logout</Btn>
</View>
)
}
componentDidMount = () => {
this.getRestFromZomato()
}
}