ListCampaign.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. }
  25. toDetail(item={}) {
  26. if (item.articleId) {
  27. startPage(PageList.viewCampaign, {id: item.articleId});
  28. }
  29. }
  30. onRefresh() {
  31. this.setState({
  32. refreshing: true
  33. })
  34. this.getMessageList();
  35. }
  36. getMessageList() {
  37. apiArticle.getCampaignList().then(res => {
  38. if (res.data) {
  39. this.setState({
  40. dataList: res.data
  41. })
  42. }
  43. }).catch(err => {
  44. toastShort(err)
  45. }).finally(() => {
  46. this.setState({
  47. refreshing: false
  48. })
  49. })
  50. }
  51. getMessageListPage() {
  52. const count = AlertUtil.getNewsCount();
  53. if (this.state.dataList.length > 0 && this.state.dataList.length < count) {
  54. const last = this.state.dataList[this.state.dataList.length-1]
  55. if (last.articleId) {
  56. apiArticle.getCampaignList(last.articleId).then(res => {
  57. if (res.data) {
  58. const list = [
  59. ...this.state.dataList,
  60. ]
  61. list.push(...res.data)
  62. this.setState({
  63. dataList: list
  64. })
  65. }
  66. }).catch(err => {
  67. toastShort(err)
  68. })
  69. }
  70. }
  71. }
  72. listItem = (props) => {
  73. return (
  74. <ItemCampaign
  75. {...props}
  76. onPress={() => this.toDetail(props.item)}
  77. />
  78. )
  79. }
  80. divideView = (props) => {
  81. return (<View style={{height: 1.5/PixelRatio.get()}}></View>)
  82. }
  83. render() {
  84. return (
  85. <FlatList
  86. style={styles.listView}
  87. data={this.state.dataList}
  88. renderItem={this.listItem}
  89. ItemSeparatorComponent={this.divideView}
  90. keyExtractor={item => item.articleId}
  91. onEndReached={() => this.getMessageListPage()}
  92. refreshControl={
  93. <RefreshControl
  94. {...MyRefreshProps()}
  95. refreshing={this.state.refreshing}
  96. onRefresh={() => this.onRefresh()}
  97. />
  98. }
  99. ListEmptyComponent={<Text style={styles.noData}>{$t('notification.empty')}</Text>}
  100. />
  101. );
  102. }
  103. }
  104. const styles = StyleSheet.create({
  105. listView: {
  106. flex: 1
  107. },
  108. noData: {
  109. color: textPlacehoder,
  110. fontSize: 14,
  111. padding: 20,
  112. textAlign: 'center'
  113. }
  114. })