Advertisement
sandhysanjaya

room.js

Feb 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { View,StyleSheet,Image,TextInput,ListView, Alert} from 'react-native';
  3. import { Container, Header, Content, List, ListItem, Text } from 'native-base';
  4. import Frisbee from 'frisbee';
  5. import Spinner from 'react-native-loading-spinner-overlay';
  6.  
  7. const api = new Frisbee({
  8.   baseURI: 'https://api.lelang15.com',
  9.   headers: {
  10.     'Accept': 'application/json',
  11.     // 'Content-Type': 'application/json'
  12.     'Content-Type': 'multipart/form-data'
  13.   }
  14. });
  15. export default class Rooms extends Component {
  16.   constructor (props) {
  17.     super(props)
  18.     this.state = {
  19.       spinner: false,
  20.       ready: false,
  21.       albums: []
  22.     }
  23.     this.dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2, sectionHeaderHasChanged: (s1, s2) => s1 !== s2})
  24.  
  25.   }
  26.  
  27.   componentDidMount(){
  28.     this.setState({ spinner: true });
  29.   //   var bodyFormData = new FormData();
  30.   //   bodyFormData.append('phone', '81314984203');
  31.   //   bodyFormData.append('device_id','40bd0dd876be91d0');
  32.   //   axios({
  33.   //     method: 'post',
  34.   //     url: 'https://api.lelang15.com/room/all_rooms',
  35.   //     data: bodyFormData,
  36.   //     mode: 'no-cors',
  37.   //     config: { headers: {'Accept': 'application/json','Content-Type': 'multipart/form-data' }}
  38.   // })
  39.   //     .then(function (response) {
  40.   //         //handle success
  41.   //         alert(response)
  42.   //         console.log(response);
  43.   //         this.setState({albums:response.data});
  44.   //         alert(response.data)
  45.   //     })
  46.   //     .catch(function (response) {
  47.   //         //handle error
  48.   //         alert(response)
  49.   //         console.log(response);
  50.   //     });
  51.       setTimeout(async () => {
  52.  
  53.         try {
  54.           let formdata = new FormData();
  55.  
  56.           formdata.append("phone", "81314984203")
  57.           formdata.append("device_id", "40bd0dd876be91d0")
  58.           const res = await api.post('/room/all_rooms', {
  59.             body: formdata
  60.           });
  61.           console.log('Response');
  62.           // console.log(res)
  63.           // alert(res.body)
  64.           if (res.err) throw res.err;
  65.           alert(res.body)
  66.           this.setState({
  67.             ready:true,
  68.             albums: res.body.data,
  69.             spinner: false
  70.           });
  71.           console.log('state')
  72.           console.log(this.state.albums)
  73.  
  74.           // setTimeout(() => {
  75.           //   Alert.alert('Sent!');
  76.           // }, 100);
  77.  
  78.         } catch (err) {
  79.           // <https://github.com/niftylettuce/react-native-loading-spinner-overlay/issues/30#issuecomment-276845098>
  80.           setTimeout(() => {
  81.             Alert.alert('Oops!', err.message);
  82.           }, 100);
  83.         }
  84.  
  85.       }, 100);
  86.     }
  87.  
  88.   _onData(){
  89.     return (
  90.       <List>
  91.             {
  92.               this.state.albums.map((item, i) => (
  93.                 <ListItem key={i}>
  94.                   <Text>{item.public_rooms.name}</Text>
  95.                 </ListItem>
  96.               ))
  97.             }
  98.  
  99.             {/* {this.state.albums.rsp_desc} */}
  100.       </List>
  101.     );
  102.   }
  103.  
  104.   _blankData(){
  105.     return (
  106.       <View><Text>Kosong</Text></View>
  107.     );
  108.   }
  109.  
  110.   _renderPage(){
  111.     if (this.state.ready){
  112.     return (
  113.         <View>{this._onData()}</View>
  114.     );
  115.   }else{
  116.     return (
  117.         <View>{this._blankData}</View>
  118.     );
  119.   }
  120.   }
  121.   render() {
  122.     const data = this.state.albums
  123.     return (
  124.       <Container>
  125.         <Content>
  126.           {this._onData()}
  127.           {/* {this.state.albums} */}
  128.           <Spinner
  129.           visible={this.state.spinner}
  130.           textContent={'One moment...'}
  131.           textStyle={{ color: '#fff' }} />
  132.         </Content>
  133.       </Container>
  134.     );
  135.   }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement