ListCampaign.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * 活动信息列表
  3. * @邠心vbe on 2023/10/24
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, FlatList, StyleSheet, RefreshControl, PixelRatio } from 'react-native';
  7. import apiArticle from '../../api/apiArticle';
  8. import { MyRefreshProps } from '../../components/ThemesConfig';
  9. import { PageList } from '../Router';
  10. import AlertUtil from './AlertUtil';
  11. import ItemCampaign from './ItemCampaign';
  12. export default class ListCampaign extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. dataList: [],
  17. refreshing: false
  18. };
  19. }
  20. componentDidMount() {
  21. this.props.navigation.addListener('focus', () => {
  22. this.getMessageList();
  23. });
  24. setTimeout(() => {
  25. this.getMessageList();
  26. }, 500);
  27. }
  28. toDetail(item={}) {
  29. if (item.articleId) {
  30. startPage(PageList.viewCampaign, {id: item.articleId});
  31. }
  32. }
  33. onRefresh() {
  34. this.setState({
  35. refreshing: true
  36. })
  37. this.getMessageList();
  38. }
  39. getMessageList() {
  40. apiArticle.getCampaignList().then(res => {
  41. if (res.data) {
  42. this.setState({
  43. dataList: res.data
  44. })
  45. }
  46. }).catch(err => {
  47. toastShort(err)
  48. }).finally(() => {
  49. this.setState({
  50. refreshing: false
  51. })
  52. })
  53. }
  54. getMessageListPage() {
  55. const count = AlertUtil.getNewsCount();
  56. if (this.state.dataList.length > 0 && this.state.dataList.length < count) {
  57. const last = this.state.dataList[this.state.dataList.length-1]
  58. if (last.articleId) {
  59. apiArticle.getCampaignList(last.articleId).then(res => {
  60. if (res.data) {
  61. const list = [
  62. ...this.state.dataList,
  63. ]
  64. list.push(...res.data)
  65. this.setState({
  66. dataList: list
  67. })
  68. }
  69. }).catch(err => {
  70. toastShort(err)
  71. })
  72. }
  73. }
  74. }
  75. listItem = (props) => {
  76. return (
  77. <ItemCampaign
  78. {...props}
  79. onPress={() => this.toDetail(props.item)}
  80. />
  81. )
  82. }
  83. divideView = (props) => {
  84. return (<View style={{height: 1.5/PixelRatio.get()}}></View>)
  85. }
  86. render() {
  87. return (
  88. <FlatList
  89. style={styles.listView}
  90. data={this.state.dataList}
  91. renderItem={this.listItem}
  92. ItemSeparatorComponent={this.divideView}
  93. keyExtractor={item => item.articleId}
  94. onEndReached={() => this.getMessageListPage()}
  95. onEndReachedThreshold={0.3}
  96. refreshControl={
  97. <RefreshControl
  98. {...MyRefreshProps()}
  99. refreshing={this.state.refreshing}
  100. onRefresh={() => this.onRefresh()}
  101. />
  102. }
  103. ListEmptyComponent={<Text style={styles.noData}>{$t('notification.empty')}</Text>}
  104. />
  105. );
  106. }
  107. }
  108. const styles = StyleSheet.create({
  109. listView: {
  110. flex: 1
  111. },
  112. noData: {
  113. color: textPlacehoder,
  114. fontSize: 14,
  115. padding: 20,
  116. textAlign: 'center'
  117. }
  118. })