Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * @Author: Thomas
  3. * @Date:   2017-07-17 12:04:17
  4. * @Last Modified by:   Thomas
  5. * @Last Modified time: 2017-07-26 12:50:10
  6. */
  7. "use strict";
  8.  
  9. import React from 'react';
  10. import { View, StatusBar, Alert, Keyboard, FlatList, ScrollView, AppRegistry, RefreshControl } from 'react-native';
  11. import { SearchBar, Icon } from 'react-native-elements'
  12. import TopBar from './components/topbar'
  13. import Card from './components/card'
  14. import { Col, Row, Grid } from "react-native-easy-grid";
  15.  
  16. const styles = require('./stylesheet.js');
  17. const globals = require('./const.js');
  18. var nav = {};
  19. var navFunction = {};
  20.  
  21. export default class MainView extends React.Component {
  22.   static navigationOptions = {
  23.     header: null
  24.   }
  25.  
  26.   constructor(props) {
  27.     super(props);
  28.  
  29.     this.getResponse({
  30.             'user': {
  31.                 'userId': 5
  32.             },
  33.             device: {
  34.                 locale: 'fr-FR'
  35.             },
  36.             conversation: {
  37.                 conversationId: 5,
  38.                 conversationState: ""
  39.             },
  40.             input: {
  41.               inputType: 'KEYBOARD',
  42.               rawInput: 'Quel est le président du Ghana ?',
  43.               intent: 'ai.xact.intent.TEXT'
  44.             }
  45.         }).then((response) => response.text())
  46.         .then((responseText) => {
  47.             console.log("MAIN " + responseText);
  48.         });
  49.  
  50.     this.state = {
  51.       db: require('./localTest.json'),
  52.       refreshing: false,
  53.     };
  54.   }
  55.  
  56.  
  57.   getResponse(request) {
  58.     return fetch(globals.API_URL, {
  59.         method: 'POST',
  60.         headers: {
  61.           'Accept': 'application/json',
  62.           'Content-Type': 'application/json',
  63.         },
  64.         body: JSON.stringify(request)
  65.     });
  66.   }
  67.  
  68.   /*
  69.   Called when text is submitted in the search bar
  70.   */
  71.   _sendText (text) {
  72.     Keyboard.dismiss();
  73.     Alert.alert(text);
  74.     //TODO: WS call
  75.   }
  76.  
  77.   _renderRow (item) {
  78.     return (
  79.       <Card
  80.       key={item.id}
  81.       data={item}
  82.       navFunc={navFunction}
  83.       />
  84.     );
  85.   }
  86.  
  87.   _openWebView (link, title, isVideo) {
  88.     if (isVideo) {
  89.       nav('Video', { link: link});
  90.     } else {
  91.       nav('Web', { link: link, title: title });
  92.     }
  93.   }
  94.  
  95.   _onRefresh() {
  96.     this.setState({refreshing: true});
  97.     //TODO: refresh data from API
  98.     /*this.fetchData().then(() => {
  99.       this.setState({refreshing: false});
  100.     });*/
  101.     this.setState({refreshing: false});
  102.   }
  103.  
  104.   fetchData() {
  105.     this.setState({
  106.       db: this.state.db
  107.     })
  108.   }
  109.  
  110.   render() {
  111.     const { navigate } = this.props.navigation;
  112.     nav = navigate;
  113.     navFunction = this._openWebView;
  114.  
  115.     return (
  116.       <Grid style={styles.container}>
  117.         <TopBar sendText={this._sendText}/>
  118.         <Row>
  119.           <ScrollView
  120.               ref={ (ref) => this.scrollView = ref }
  121.               refreshControl={
  122.                 <RefreshControl
  123.                   refreshing={this.state.refreshing}
  124.                   onRefresh={this._onRefresh.bind(this)}
  125.                 />
  126.               }
  127.             >
  128.             {this.state.db.map(item =>  this._renderRow(item) )}
  129.           </ScrollView>
  130.         </Row>
  131.       </Grid>
  132.     );
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement