Notification.js 1.76 KB
Newer Older
Giang Tran committed
1 2
import React, {useState, useEffect} from 'react';
import {getListNotification} from '../../apis/Functions/users';
Giang Tran committed
3
import NotificationView from './NotificaitonView';
Giang Tran committed
4
import I18n from '../../helper/i18/i18n';
Giang Tran committed
5
const Notifcation = (props) => {
Giang Tran committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  const [selected, setSelected] = useState('');
  const [page, setPage] = useState(1);
  const [data, setData] = useState([]);
  const [tottalPage, setTotalPage] = useState(1);
  const [isRefresh, setisRefresh] = useState(false);
  const [fillter, setFillters] = useState('ALL');

  useEffect(() => {
    getData();
  }, [fillter]);

  const getData = async () => {
    setisRefresh(true);
    setPage(1);
    const res = await getListNotification({
      keyword: '',
      platform: Platform.OS,
      page_size: 10,
      page_index: 1,
      type: fillter,
    });
    setisRefresh(false);
    if ((res.data.code = 200 && res.data.data)) {
      setData(res.data.data);
      setTotalPage(res.data.meta.pages);
    } else {
Giang Tran committed
32
      Alert.alert(I18n.t('Notification'), res.data.message);
Giang Tran committed
33 34 35 36 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 68 69 70
    }
  };

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

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

  const getDataLoadMore = async () => {
    setisRefresh(true);
    const res = await getListNotification({
      keyword: '',
      platform: Platform.OS,
      page_size: 10,
      page_index: 1,
      type: fillter,
    });
    setPage(page + 1);
    if (res.data.code == 200) {
      setData(data.concat(res.data.data));
    }
    setisRefresh(false);
  };

  return (
    <NotificationView
      onRefresh={onRefresh}
      isRefresh={isRefresh}
      onLoadMore={onLoadMore}
      setFillters={setFillters}
      fillter={fillter}
      data={data}
    />
  );
Giang Tran committed
71 72 73
};

export default Notifcation;