Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. const myApiUrl = Config.urlApi;
  2.  
  3.  
  4. export default class ProvidersScreen extends React.Component {
  5.  
  6. constructor(props) {
  7. super(props);
  8. this.dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
  9. this.state = {
  10. isLoaded: false,
  11. refreshing: false,
  12. providersList: [],
  13. };
  14. }
  15.  
  16. componentDidMount() {
  17. this.fetchData()
  18. }
  19.  
  20. async fetchData() {
  21. try {
  22. const token = await AsyncStorage.getItem('appToken');
  23. if (token !== null) {
  24. fetch(`${myApiUrl}/providers?is_enabled=1`, {
  25. headers: {
  26. 'Content-Type': 'application/json',
  27. 'Authorization': 'mobile=' + Config.apiKeyMobile,
  28. 'X-AuthToken': token,
  29. }
  30. })
  31. .then((response) => response.json())
  32. .then((responseJson) => {
  33. this.setState({providersList: responseJson});
  34. this.setState({isLoaded: true});
  35. this.setState({refreshing: false});
  36. this.fetchDataAddresses()
  37. })
  38. .catch((error) => {
  39. console.error(error);
  40. });
  41. }
  42. } catch (error) {
  43. console.log('------ERROR TOKEN : ' + error);
  44. }
  45. }
  46.  
  47. async fetchDataAddresses() {
  48. try {
  49. const token = await AsyncStorage.getItem('appToken');
  50. if (token !== null) {
  51. this.state.providersList.map((provider) => {
  52. fetch(`${myApiUrl}/providers/` + provider.id + `/addresses`, {
  53. headers: {
  54. 'Content-Type': 'application/json',
  55. 'Authorization': 'mobile=' + Config.apiKeyMobile,
  56. 'X-AuthToken': token,
  57. }
  58. })
  59. .then((response) => response.json())
  60. .then((responseJson) => {
  61. let address = responseJson;
  62. console.log('PROVIDER:' + provider.name);
  63. address.map((address) => {
  64. console.log('CITY:' + address.city);
  65. console.log('ZIP_CODE:' + address.zip_code);
  66. });
  67. })
  68. .catch((error) => {
  69. console.error(error);
  70. });
  71. });
  72. }
  73. } catch (error) {
  74. console.log('------ERROR TOKEN : ' + error);
  75. }
  76. }
  77.  
  78. render() {
  79. if (!this.state.isLoaded) {
  80. return (
  81. <Container style={styles.spinner}>
  82. <Spinner color='black'/>
  83. </Container>);
  84. }
  85. else {
  86. return (
  87. <Container>
  88. <Content
  89. refreshControl={<RefreshControl
  90. refreshing={this.state.refreshing}
  91. onRefresh={() => {
  92. this.setState({refreshing: true});
  93. this.fetchData();
  94. }}
  95. title={'Mise à jour…'}
  96. />}
  97. >
  98. <List
  99. dataSource={this.dataSource.cloneWithRows(this.state.providersList)}
  100. renderRow={data =>
  101. <ListItem onPress={() => console.log('Pressed!!!!!!')}>
  102. <Thumbnail source={logoImg} style={styles.thumbnail}/>
  103. <Content style={styles.contentProvider}>
  104. <Text>{data.name}</Text>
  105. <Text note>{data.ref}</Text>
  106. </Content>
  107. </ListItem>}
  108. renderLeftHiddenRow={data =>
  109. <Button full onPress={() => this.openMap('Nancy')}>
  110. <Icon active ios="ios-pin" android="md-pin" style={{fontSize: 36}}/>
  111. </Button>}
  112. renderRightHiddenRow={data =>
  113. <Button full success onPress={() => Linking.openURL('tel:' + data.tel1)}>
  114. <Icon active ios="ios-call" android="md-call" style={{fontSize: 36}}/>
  115. </Button>}
  116. leftOpenValue={75}
  117. rightOpenValue={-75}
  118. />
  119. </Content>
  120. </Container>
  121. );
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement