ListVoucher.js 5.3 KB

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