ListVoucher.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * 我的代金券列表
  3. * @邠心vbe on 2024/04/09
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, RefreshControl, FlatList, Pressable } from 'react-native';
  7. import { MyRefreshProps } from '../../components/ThemesConfig';
  8. import TextView from '../../components/TextView';
  9. import ViewRedeem from './ViewRedeem';
  10. import VoucherType from './VoucherType';
  11. import apiVoucher from '../../api/apiVoucher';
  12. import { PageList } from '../Router';
  13. export default class ListVoucher extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. userInfo: {},
  18. dataList: [],
  19. voucherType: "",
  20. hasMore: true,
  21. refreshing: false
  22. };
  23. }
  24. componentDidMount() {
  25. this.props.navigation.addListener('focus', () => {
  26. this.refreshUserInfo();
  27. this.getDataList();
  28. });
  29. }
  30. onRefresh() {
  31. this.setState({
  32. refreshing: true
  33. })
  34. this.refreshUserInfo();
  35. this.getDataList();
  36. }
  37. refreshUserInfo() {
  38. getUserInfo(info => {
  39. this.setState({
  40. userInfo: info
  41. });
  42. }, true);
  43. }
  44. getDataList(lastId="") {
  45. apiVoucher.getMyVouchers({
  46. lastVoucherId: lastId,
  47. voucherType: this.state.voucherType
  48. }).then(res => {
  49. if (res.data) {
  50. if (lastId) {
  51. if (res.data.length > 0) {
  52. const list = this.state.dataList;
  53. this.setState({
  54. dataList: list.concat(res.data)
  55. });
  56. } else {
  57. this.setState({
  58. hasMore: false
  59. })
  60. }
  61. } else {
  62. this.setState({
  63. dataList: res.data,
  64. hasMore: res.data.length >= 10
  65. });
  66. }
  67. } else {
  68. this.setState({
  69. dataList: []
  70. });
  71. }
  72. }).catch(err => {
  73. toastShort(err)
  74. }).finally(() => {
  75. this.setState({
  76. refreshing: false
  77. });
  78. });
  79. }
  80. getDataListPage() {
  81. if (this.state.dataList.length > 0 && this.state.hasMore) {
  82. const last = this.state.dataList[this.state.dataList.length-1]
  83. this.getDataList(last.voucherId);
  84. }
  85. }
  86. getColorByStatus(status) {
  87. let color = colorAccent;
  88. switch (status) {
  89. case "Used":
  90. case "Expired":
  91. color = "#434343";
  92. break;
  93. case "Expiring":
  94. color = "#ED3F3F"
  95. break;
  96. }
  97. return color;
  98. }
  99. changeType(type) {
  100. this.setState({
  101. voucherType: type
  102. });
  103. this.getDataList();
  104. }
  105. toDetailPage(item) {
  106. startPage(PageList.voucherDetails, {id: item.voucherId});
  107. }
  108. listItem = ({item, index, separators}) => {
  109. return (
  110. <Pressable
  111. style={styles.itemView}
  112. onPress={() => this.toDetailPage(item)}>
  113. <View style={styles.itemContent}>
  114. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  115. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  116. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  117. </View>
  118. <TextView
  119. style={[
  120. styles.statusButton, {
  121. color: this.getColorByStatus(item.userVoucherStatus),
  122. borderColor: this.getColorByStatus(item.userVoucherStatus)
  123. }
  124. ]}>
  125. {item.userVoucherStatus}
  126. </TextView>
  127. </Pressable>
  128. )
  129. }
  130. topView = (props) => {
  131. return (
  132. <View>
  133. <ViewRedeem
  134. userInfo={this.state.userInfo}
  135. onChange={() => this.onRefresh()}/>
  136. <VoucherType
  137. type={this.state.voucherType}
  138. onChange={type => this.changeType(type)}
  139. />
  140. </View>
  141. );
  142. }
  143. bottomView = () => {
  144. if (!this.state.hasMore) {
  145. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  146. } else {
  147. return null
  148. }
  149. }
  150. render() {
  151. return (
  152. <FlatList
  153. style={styles.container}
  154. data={this.state.dataList}
  155. renderItem={this.listItem}
  156. ListHeaderComponent={this.topView}
  157. keyExtractor={item => item.userVoucherId}
  158. onEndReached={() => this.getDataListPage()}
  159. onEndReachedThreshold={0.3}
  160. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  161. ListFooterComponent={this.bottomView}
  162. refreshControl={
  163. <RefreshControl
  164. {...MyRefreshProps()}
  165. refreshing={this.state.refreshing}
  166. onRefresh={() => this.onRefresh()}
  167. />
  168. }/>
  169. );
  170. }
  171. }
  172. const styles = StyleSheet.create({
  173. container: {
  174. flex: 1,
  175. padding: 16
  176. },
  177. itemView: {
  178. padding: 16,
  179. marginTop: 16,
  180. borderWidth: 1,
  181. borderColor: '#DADADA',
  182. borderRadius: 4,
  183. alignItems: 'center',
  184. flexDirection: 'row',
  185. backgroundColor: colorLight
  186. },
  187. itemContent: {
  188. flex: 1,
  189. paddingRight: 16
  190. },
  191. voucherTitle: {
  192. color: textPrimary,
  193. fontSize: 16,
  194. fontWeight: 'bold'
  195. },
  196. voucherDesc: {
  197. color: textPrimary,
  198. fontSize: 14,
  199. paddingTop: 2,
  200. paddingBottom: 4
  201. },
  202. expireDate: {
  203. color: textCancel,
  204. fontSize: 12
  205. },
  206. statusButton: {
  207. color: colorAccent,
  208. padding: 8,
  209. minWidth: 73,
  210. fontSize: 12,
  211. textAlign: 'center',
  212. borderWidth: 1,
  213. borderColor: colorAccent,
  214. borderRadius: 6,
  215. textTransform: 'uppercase',
  216. backgroundColor: colorLight
  217. },
  218. noData: {
  219. color: textPlacehoder,
  220. fontSize: 14,
  221. padding: 20,
  222. marginTop: 16,
  223. textAlign: 'center'
  224. },
  225. noMore: {
  226. color: textPlacehoder,
  227. fontSize: 14,
  228. padding: 16,
  229. textAlign: 'center'
  230. }
  231. })