/* eslint-disable no-undef */
import React, { Component } from 'react';
import { ScrollView, Text, View, ActivityIndicator, FlatList, TouchableHighlight } from 'react-native';
import { ListItem } from 'react-native-elements';
import * as shortid from 'shortid';
import Helper from '../../../utils/helper';
import AsyncStorageHelper from '../../../utils/AsyncStorageHelper';

class FreeBookList extends Component {
  static componentName = 'FreeBookList';

  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      rowData: [],
    };
  }

  componentDidMount() {
    AsyncStorageHelper.get('discover.freebook', (result) => {
      if (result) {
        this.setState({
          loading: false,
          rowData: JSON.parse(result),
        });
      }
    });

    fetch('https://phodal.github.io/growth-api-gitbook/api.json')
    .then(response => response.json())
    .then((data) => {
      const rowData = Array.from(new Array(data.length))
      .map((val, index) => (data[index]));

      AsyncStorageHelper.set('discover.freebook', JSON.stringify(data));
      this.setState({
        loading: false,
        rowData,
      });
    });
  }

  render() {
    const { loading, rowData } = this.state;

    if (loading) {
      return (<View
        style={{ marginTop: 20 }}
      >
        <ActivityIndicator
          animating
          size={'large'}
          color={'#000'}
        />
      </View>);
    }

    return (
      <ScrollView containerStyle={{ marginBottom: 20 }}>
        {
          rowData.map(sections => (
            <View
              key={shortid.generate()}
            >
              <View style={{ padding: 10 }}><Text style={{ textAlign: 'center' }}>{sections.heading}</Text></View>
              <FlatList
                data={sections.childrens}
                renderItem={({ item }) => (
                  <TouchableHighlight
                    onPress={() => Helper.openLink(item.href)}
                  >
                    <View style={{ backgroundColor: 'white' }}>
                      <ListItem
                        title={item.title}
                        subtitle={`类型：${item.type}`}
                      />
                    </View>
                  </TouchableHighlight>
                )}
              />
            </View>
          ))
        }
      </ScrollView>
    );
  }
}

export default FreeBookList;

@@ -780,17 +780,11 @@
   %7D%0A
+%0A
   
-  %7D);%0A%0A
     
@@ -845,24 +845,26 @@
 .json')%0A    
+ 
+ 
 .then(respon
@@ -886,24 +886,26 @@
 json())%0A    
+  
 .then((data)
@@ -906,24 +906,26 @@
 (data) =%3E %7B%0A
+  
       const 
@@ -975,16 +975,18 @@
 )%0A      
+  
 .map((va
@@ -1015,16 +1015,18 @@
 ex%5D));%0A%0A
+  
       As
@@ -1092,24 +1092,26 @@
 ta));%0A      
+  
 this.setStat
@@ -1106,32 +1106,34 @@
 this.setState(%7B%0A
+  
         loading:
@@ -1144,24 +1144,26 @@
 se,%0A        
+ 
+ 
 rowData,%0A   
@@ -1159,16 +1159,28 @@
 owData,%0A
+        %7D);%0A
       %7D)

add basic cache