Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. import React from 'react';
  2. import { connect } from "react-redux";
  3. import { FlatList, ActivityIndicator, Text, View, TouchableWithoutFeedback, ImageBackground } from 'react-native';
  4. import WebinarInfoScreen from '../screens/WebinarInfoScreen';
  5. import images from '../assets/images/index';
  6. import WebinarRating from '../components/WebinarRating';
  7.  
  8.  
  9. export class FetchExample extends React.Component {
  10.  
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. isLoading: true,
  15. upcomingWebinars: [],
  16. category_id: '',
  17. call_all_webinars: '',
  18. }
  19. console.log(this.state, 'here')
  20. }
  21.  
  22. componentDidMount() {
  23. this.checkForCategoryId()
  24.  
  25.  
  26. }
  27.  
  28. checkForCategoryId() {
  29. let call_all_webinars = '';
  30. if(this.props.category_id != undefined){
  31. this.state.category_id = this.props.category_id
  32. let call_all_webinars = '/' + this.state.category_id;
  33. this.fetchWebinars(call_all_webinars)
  34. }
  35. this.fetchWebinars(call_all_webinars)
  36. }
  37.  
  38. fetchWebinars = async () => {
  39. try {
  40. console.log(this.state.call_all_webinars)
  41. await fetch(this.state.call_all_webinars, {
  42. method: 'GET',
  43. headers: new Headers({
  44. 'Authorization': 'Bearer ' + this.props.token.token,
  45. 'Content-Type': 'application/x-www-form-urlencoded',
  46. 'Accept': 'application/json'
  47. })
  48. })
  49. .then((response) => response.json())
  50. .then((responseJson) => {
  51. this.setState({
  52. isLoading: false,
  53. upcomingWebinars: responseJson
  54. })
  55. this.updateData(this.state.upcomingWebinars)
  56. })
  57. .catch((error) => {
  58. console.error(error);
  59. })
  60. } catch (error) {
  61. console.error(error);
  62. }
  63. }
  64.  
  65. countWebinars() {
  66. return this.state.upcomingWebinars.length;
  67. }
  68.  
  69. updateData(data) {
  70. data.push({
  71. "category_name": "End_Card",
  72. "name": "Show All",
  73. })
  74. this.setState({
  75. upcomingWebinars: data
  76. });
  77. }
  78.  
  79. renderCards(item) {
  80. if (item.category_name == 'End_Card') {
  81. return (
  82. <TouchableWithoutFeedback onPress={() => this.props.showAll()} >
  83. <View>
  84. <ImageBackground
  85. source={this.backgroundImage(item)}
  86. borderRadius={3}
  87. resizeMode='contain'
  88. style={{
  89. elevation: 5,
  90. paddingLeft: 20,
  91. paddingRight: 10,
  92. height: 150,
  93. margin: 10,
  94. width: 125
  95. }}>
  96. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  97. <Text style={{ fontSize: 18, color: 'black' }}>Browse Library</Text>
  98. </View>
  99. </ImageBackground>
  100. </View>
  101. </TouchableWithoutFeedback>
  102. );
  103. } else {
  104. return (
  105. <TouchableWithoutFeedback onPress={() => this.props.setWebinar(item)} >
  106. <View>
  107. <ImageBackground
  108. source={this.backgroundImage(item)}
  109. borderRadius={3}
  110. resizeMode='contain'
  111. style={{
  112. elevation: 5,
  113. padding: 20,
  114. height: 150,
  115. margin: 10,
  116. width: 250
  117. }}>
  118. <Text numberOfLines={3} style={{ minWidth: 125, maxWidth: 185, paddingRight: 7, fontWeight: 'bold', fontSize: 14, color: 'white' }}>{item.name}</Text>
  119. <Text style={{ fontSize: 11, color: 'white' }}>{item.category_name}</Text>
  120. <View style={{ flexDirection: 'row', justifyContent: 'flex-start' }}>
  121. <WebinarRating />
  122. </View>
  123. </ImageBackground>
  124. </View>
  125. </TouchableWithoutFeedback>
  126.  
  127. );
  128. }
  129.  
  130. }
  131.  
  132. backgroundImage(item) {
  133. switch (item.category_name) {
  134. case "Small Animal":
  135. return images.small_animal;
  136. case "Spanish Webinars":
  137. return images.spanish;
  138. case "Nurse":
  139. return images.nurse;
  140. case "Equine":
  141. return images.equine;
  142. case "Practice Management":
  143. return images.practice_management;
  144. case "Virtual Congress 2020":
  145. return images.vc2020;
  146. case "Virtual Congress 2019":
  147. return images.vc2019;
  148. case "Exotics":
  149. return images.exotics;
  150. case "Farm":
  151. return images.farm;
  152. case "Health & Wellbeing":
  153. return images.health_and_wellbeing;
  154. case "End_Card":
  155. return null;
  156. default:
  157. return images.webinar_one;
  158. }
  159. }
  160.  
  161. render() {
  162. if (this.state.isLoading) {
  163. return (
  164. <View style={{ flex: 1, padding: 20 }}>
  165. <ActivityIndicator size={'large'} />
  166. </View>
  167. )
  168. }
  169.  
  170. return (
  171. <View>
  172. <Text style={{ paddingLeft: 10, fontSize: 18 }}>Our Latest Webinars</Text>
  173. <FlatList
  174. horizontal={true}
  175. data={this.state.upcomingWebinars}
  176. showsHorizontalScrollIndicator={false}
  177. renderItem={({ item }) => (
  178.  
  179. <View>
  180. {this.renderCards(item)}
  181. </View>
  182. )}
  183. keyExtractor={({ index }, e) => index}
  184. extraData={this.state}
  185. />
  186. </View>
  187. );
  188. }
  189. }
  190. const mapStateToProps = (state) => {
  191. return {
  192. auth: state.auth,
  193. token: state.token
  194. }
  195. };
  196.  
  197. export default connect(mapStateToProps)(FetchExample)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement