Payments.js 2.29 KB
Newer Older
Giang Tran committed
1 2
import React, {useState, useEffect} from 'react';
import {View, FlatList, Text, Alert} from 'react-native';
Giang Tran committed
3
import HeaderBack from '../../../components/Header/HeaderBack';
4
import Item from './Item';
Giang Tran committed
5
import AppText from '../../../components/AppText';
Giang Tran committed
6 7
import {getListTransaction} from '../../../apis/Functions/Widthdraw';
import I18n from '../../../helper/i18/i18n';
Giang Tran committed
8
import {showAlert, TYPE} from '../../../components/DropdownAlert';
Giang Tran committed
9 10

const Payments = (props) => {
Giang Tran committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  const [page, setPage] = useState(1);
  const [data, setData] = useState([]);
  const [tottalPage, setTotalPage] = useState(1);
  const [isRefresh, setisRefresh] = useState(false);
  useEffect(() => {
    getData();
  }, []);
  const getData = async () => {
    setisRefresh(true);
    setPage(1);
    const res = await getListTransaction({
      keyword: '',
      type: 'PAYMENT',
      platform: Platform.OS,
      page_size: 10,
      status: -1,
      page_index: 1,
      start_date: '',
      end_date: '',
    });
    setisRefresh(false);
    if ((res.data.code = 200 && res.data.data)) {
      setData(res.data.data);
      setTotalPage(res.data.meta.pages);
    } else {
Giang Tran committed
36
      showAlert(TYPE.ERROR, I18n.t('Notification'), res.data.message);
Giang Tran committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    }
  };

  const onRefresh = () => {
    getData();
  };

  const onLoadMore = () => {
    if (page < tottalPage) getDataLoadMore();
  };

  const getDataLoadMore = async () => {
    setisRefresh(true);
    const res = await getListTransaction({
      keyword: '',
      type: 'PAYMENT',
      platform: Platform.OS,
      page_size: 10,
      status: -1,
      page_index: page + 1,
      start_date: '',
      end_date: '',
    });
    setPage(page + 1);

    if (res.data.code == 200) {
      setData(data.concat(res.data.data));
    }
    setisRefresh(false);
  };

Giang Tran committed
68
  return (
Giang Tran committed
69
    <View style={{flex: 1}}>
Giang Tran committed
70
      <HeaderBack isWhite={false} title={'Payments'} />
71

Giang Tran committed
72
      <FlatList
73 74
        keyExtractor={(item) => item.id}
        data={data}
Giang Tran committed
75 76 77 78 79 80 81 82
        refreshing={isRefresh}
        showsVerticalScrollIndicator={false}
        onRefresh={onRefresh}
        onEndReachedThreshold={0.01}
        onEndReached={(info) => {
          onLoadMore();
        }}
        keyExtractor={(item) => item.transection_id + 'a'}
83
        renderItem={({item}) => <Item item={item} />}
Giang Tran committed
84
      />
Giang Tran committed
85 86 87 88 89
    </View>
  );
};

export default Payments;