ListVoucher.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.itemBox}></View>
  114. <View style={styles.itemContent}>
  115. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  116. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  117. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  118. <View style={styles.rightDash}></View>
  119. <View style={styles.topTikDot}></View>
  120. <View style={styles.bottomTikDot}></View>
  121. </View>
  122. <TextView
  123. style={[
  124. styles.statusButton, {
  125. color: this.getColorByStatus(item.userVoucherStatus),
  126. borderColor: this.getColorByStatus(item.userVoucherStatus)
  127. }
  128. ]}>
  129. {item.userVoucherStatus}
  130. </TextView>
  131. </Pressable>
  132. )
  133. }
  134. topView = (props) => {
  135. return (
  136. <View>
  137. <ViewRedeem
  138. userInfo={this.state.userInfo}
  139. onChange={() => this.onRefresh()}/>
  140. <VoucherType
  141. type={this.state.voucherType}
  142. onChange={type => this.changeType(type)}
  143. />
  144. </View>
  145. );
  146. }
  147. bottomView = () => {
  148. if (this.state.dataList.length > 0 && !this.state.hasMore) {
  149. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  150. } else {
  151. return null
  152. }
  153. }
  154. render() {
  155. return (
  156. <FlatList
  157. style={styles.container}
  158. data={this.state.dataList}
  159. renderItem={this.listItem}
  160. ListHeaderComponent={this.topView}
  161. keyExtractor={item => item.userVoucherId}
  162. onEndReached={() => this.getDataListPage()}
  163. onEndReachedThreshold={0.3}
  164. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  165. ListFooterComponent={this.bottomView}
  166. refreshControl={
  167. <RefreshControl
  168. {...MyRefreshProps()}
  169. refreshing={this.state.refreshing}
  170. onRefresh={() => this.onRefresh()}
  171. />
  172. }/>
  173. );
  174. }
  175. }
  176. const styles = StyleSheet.create({
  177. container: {
  178. flex: 1,
  179. padding: 16
  180. },
  181. itemView: {
  182. marginTop: 16,
  183. borderRadius: 4,
  184. alignItems: 'center',
  185. flexDirection: 'row',
  186. backgroundColor: colorLight,
  187. ...$padding(0, 16)
  188. },
  189. itemBox: {
  190. top: 0,
  191. left: 0,
  192. right: 0,
  193. bottom: 0,
  194. borderWidth: 1,
  195. borderColor: '#DADADA',
  196. borderRadius: 4,
  197. position: 'absolute'
  198. },
  199. itemContent: {
  200. flex: 1,
  201. paddingTop: 12,
  202. marginRight: 16,
  203. paddingBottom: 12,
  204. overflow: 'hidden'
  205. },
  206. rightDash: {
  207. top: 0,
  208. right: 6,
  209. bottom: 0,
  210. position: 'absolute',
  211. borderStyle: 'dashed',
  212. borderRightWidth: 1,
  213. borderRightColor: '#DADADA'
  214. },
  215. topTikDot: {
  216. top: -8,
  217. right: 0,
  218. width: 13,
  219. height: 14,
  220. borderWidth: 1,
  221. borderColor: '#DADADA',
  222. borderRadius: 30,
  223. position: 'absolute',
  224. backgroundColor: pageBackground
  225. },
  226. bottomTikDot: {
  227. bottom: -8,
  228. right: 0,
  229. width: 13,
  230. height: 14,
  231. borderWidth: 1,
  232. borderColor: '#DADADA',
  233. borderRadius: 30,
  234. position: 'absolute',
  235. backgroundColor: pageBackground
  236. },
  237. voucherTitle: {
  238. color: textPrimary,
  239. fontSize: 16,
  240. fontWeight: 'bold'
  241. },
  242. voucherDesc: {
  243. color: textPrimary,
  244. fontSize: 14,
  245. paddingTop: 2,
  246. paddingBottom: 4
  247. },
  248. expireDate: {
  249. color: textCancel,
  250. fontSize: 12
  251. },
  252. statusButton: {
  253. color: colorAccent,
  254. padding: 5,
  255. minWidth: 70,
  256. fontSize: 12,
  257. marginRight: 4,
  258. textAlign: 'center',
  259. borderWidth: 1,
  260. borderColor: colorAccent,
  261. borderRadius: 6,
  262. textTransform: 'uppercase',
  263. backgroundColor: colorLight
  264. },
  265. noData: {
  266. color: textPlacehoder,
  267. fontSize: 14,
  268. padding: 20,
  269. marginTop: 16,
  270. textAlign: 'center'
  271. },
  272. noMore: {
  273. color: textPlacehoder,
  274. fontSize: 14,
  275. padding: 16,
  276. textAlign: 'center'
  277. }
  278. })