-
Notifications
You must be signed in to change notification settings - Fork 16
/
AlbumsSearchScreen.js
139 lines (116 loc) · 3.01 KB
/
AlbumsSearchScreen.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
var AlbumsListScreen = require('./AlbumsListScreen')
import React, { Component } from 'react'
import {
StyleSheet,
View,
ActivityIndicator,
Text,
TextInput,
TouchableHighlight,
Image,
ScrollView
} from 'react-native'
var iTunesSearchURL = 'https://itunes.apple.com/search?'
function urlForAlbumsQueryWithResultsLimit (query, resultsLimit) {
var data = {
entity: 'album',
term: query,
limit: resultsLimit
}
var queryString = Object.keys(data)
.map(key => key + '=' + encodeURIComponent(data[key]))
.join('&')
return iTunesSearchURL + queryString
};
var AlbumsSearchScreen = React.createClass({
getInitialState () {
return {
isLoading: false,
errorMessage: '',
searchString: ''
}
},
onSearchTextChanged (event) {
this.setState({searchString: event.nativeEvent.text})
},
_executeQuery (query) {
console.log(query)
this.setState({ isLoading: true, errorMessage: '' })
fetch(query)
.then(response => response.json())
.then(json => this._handleResponse(json.results))
.catch(error => this.setState({
isLoading: false,
errorMessage: 'Something bad happened: \n' + error
}))
},
_handleResponse (results) {
this.setState({ isLoading: false })
if (results.length > 0) {
this.props.navigator.push({
title: this.state.searchString,
component: AlbumsListScreen,
passProps: {albums: results}
})
} else {
this.setState({ errorMessage: "Can't find albums for your search input." })
}
},
searchAlbums () {
var query = urlForAlbumsQueryWithResultsLimit(this.state.searchString, 200)
this._executeQuery(query)
},
onSearchTextSubmitEditing (event) {
this.searchAlbums()
},
render () {
var spinner = this.state.isLoading ?
<ActivityIndicator size='large' /> :
<View />
return (
<ScrollView alwaysBounceVertical={false}>
<View style={styles.container}>
<Image source={require('./Assets/itunes_logo.png')} style={styles.image} />
<Text style={styles.description}>
Search by artist, album or genre:
</Text>
<TextInput
style={styles.searchInput}
onChange={this.onSearchTextChanged}
onSubmitEditing={this.onSearchTextSubmitEditing}
clearButtonMode={'while-editing'}
placeholder='Search album'
returnKeyType='search' />
{spinner}
<Text style={styles.description}>{this.state.errorMessage}</Text>
</View>
</ScrollView>
)
}
})
var styles = StyleSheet.create({
container: {
padding: 30,
alignItems: 'center'
},
description: {
marginBottom: 20,
fontSize: 18,
color: '#656565'
},
searchInput: {
height: 36,
padding: 4,
marginBottom: 30,
fontSize: 18,
borderWidth: 1,
borderColor: '#FF8000',
borderRadius: 8
},
image: {
width: 100,
height: 100
}
})
module.exports = AlbumsSearchScreen