Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. import React from 'react';
  2. import { AsyncStorage, Button, Text, View, TextInput, ScrollView, KeyboardAvoidingView, StyleSheet, ActivityIndicator, Image } from 'react-native';
  3. import data from './Recipes.json';
  4.  
  5. class RecipeScreen extends React.Component {
  6.  
  7. constructor(props) {
  8. const { navigation } = props;
  9.  
  10. super()
  11. this.state = {
  12. isLoading: true,
  13. title: navigation.getParam('title', 'title'),
  14. image: navigation.getParam('image', 'image'),
  15. comments: "",
  16. url: 'https://api.myjson.com/bins/qs7o4',
  17. TextInputValue: '',
  18. NameInputValue: '',
  19. }
  20.  
  21. }
  22. componentDidMount() {
  23. this.loadComments();
  24. return fetch(this.state.url)
  25. .then( (response) => response.json() )
  26. .then( (responseJson) => {
  27.  
  28. this.setState({
  29. isLoading: false,
  30. recipes: responseJson,
  31. });
  32.  
  33. })
  34.  
  35. .catch((error) => {
  36. console.log(error)
  37. });
  38. }
  39.  
  40. loadComments = async () => {
  41. await AsyncStorage.getItem(this.state.title, (err, comment) => this.setState({comments: comment})).catch(err => {alert.message(err.message)});
  42. }
  43.  
  44. buttonClickListener = () => {
  45. comments = this.state.comments;
  46. let name = this.state.NameInputValue;
  47. let comment = this.state.TextInputValue;
  48.  
  49. if (comments !== null) {
  50. comments += '\n';
  51. name = name === '' ? "Anonimas: " : name + ": ";
  52. comments += name + comment;
  53. } else {
  54. name = name === '' ? "Anonimas: " : name + ": ";
  55. comments = name + comment;
  56. }
  57.  
  58. AsyncStorage.setItem(this.state.title, comments);
  59. this.setState({
  60. comments: comments,
  61. TextInputValue: '',
  62. NameInputValue: '',
  63. });
  64. }
  65.  
  66. render() {
  67. /* If data not loaded create loading circle in midle screen*/
  68. if (this.state.isLoading) {
  69. return (
  70. <View style = { styles.center }>
  71. <ActivityIndicator size = "small" color = "#00ff00" />
  72. </View>
  73. )
  74. }
  75.  
  76. let ingredients = this.state.recipes.recipes.map((val, key) => {
  77. let ingredientArr = null;
  78.  
  79. if ( val.title === this.state.title ) {
  80. ingredientArr = val.ingredientList.map((val, key) => {
  81. return <Text key={key} style={ styles.ingredientText }>
  82. { val }
  83. </Text>
  84. });
  85. }
  86.  
  87. return ingredientArr;
  88. });
  89.  
  90. let preperation = this.state.recipes.recipes.map((val, key) => {
  91. let preparationArr = null;
  92.  
  93. if ( val.title === this.state.title ) {
  94. preparationArr = val.preperation.map((val, key) => {
  95. return <Text key={key} style={ styles.ingredientText }>
  96. { val }
  97. </Text>
  98. });
  99. }
  100. return preparationArr;
  101. });
  102.  
  103. return (
  104. <KeyboardAvoidingView style={{ flex: 1 }} behavior="padding" enabled keyboardVerticalOffset={80}>
  105. <ScrollView style={{ flex: 1, flexDirection: 'column' }}>
  106.  
  107. <View style={{ flex: 1, alignItems: 'center' }}>
  108. <Text style={{ fontWeight: 'bold', fontSize: 24, textAlign: 'center' }}> { this.state.title } </Text>
  109. </View>
  110.  
  111. <View style={{ flex: 4, alignItems: 'center', justifyContent: 'center' }}>
  112. <Image style={{ width: 350, height: 200 }} source={{ uri: this.state.image }} />
  113. </View>
  114.  
  115. <View style={{ flex: 5, flexDirection: 'column', paddingLeft: 13 }}>
  116.  
  117. <View style={{ flex: 1 }}>
  118.  
  119. <Text style={ styles.subtitleText }>Ingrediantai</Text>
  120. { ingredients }
  121.  
  122. </View>
  123.  
  124. <View style={{ flex: 1 }}>
  125.  
  126. <Text style={styles.subtitleText}>Paruo6imo budas:</Text>
  127. { preperation }
  128.  
  129. </View>
  130.  
  131. </View>
  132.  
  133. <View style={{ flex: 1, paddingLeft: 13 }}>
  134. <Text style={ styles.titleText }>Komentarai:</Text>
  135. </View>
  136.  
  137. <View style={{ flex: 1, paddingLeft: 13 }}>
  138. <Text>
  139. { this.state.comments }
  140. </Text>
  141. </View>
  142.  
  143. <View style={{ flex: 1}}>
  144.  
  145. <TextInput
  146. style={{ height: 45, paddingLeft: 13}}
  147. placeholder='Vardas'
  148. multiline={false}
  149. onChangeText={NameInputValue => this.setState({NameInputValue})}
  150. value={this.state.NameInputValue}
  151. />
  152.  
  153. <TextInput
  154. style={{ height: 50, paddingLeft: 13}}
  155. placeholder='Komentaras'
  156. multiline={true}
  157. onChangeText={TextInputValue => this.setState({TextInputValue})}
  158. value={this.state.TextInputValue}
  159. />
  160.  
  161. <Button
  162. title='Komentuoti'
  163. color='green'
  164. onPress={this.buttonClickListener}
  165. />
  166.  
  167. </View>
  168.  
  169. </ScrollView>
  170. </KeyboardAvoidingView>
  171. );
  172. }
  173. }
  174.  
  175. export default RecipeScreen;
  176.  
  177. const styles = StyleSheet.create({
  178. titleText: {
  179. fontWeight: 'bold',
  180. fontSize: 24,
  181. },
  182.  
  183. subtitleText: {
  184. fontWeight: 'bold',
  185. fontSize: 18,
  186. marginBottom: 50,
  187. },
  188.  
  189. ingredientText: {
  190. fontSize: 16,
  191. marginBottom: 10,
  192. },
  193.  
  194. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement