ListVoucher.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. import app from '../../../app.json';
  14. export default class ListVoucher extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. userInfo: {},
  19. dataList: [],
  20. voucherType: "",
  21. hasMore: true,
  22. refreshing: false
  23. };
  24. }
  25. componentDidMount() {
  26. this.props.navigation.addListener('focus', () => {
  27. this.refreshUserInfo();
  28. this.getDataList();
  29. });
  30. }
  31. onRefresh() {
  32. this.setState({
  33. refreshing: true
  34. })
  35. this.refreshUserInfo();
  36. this.getDataList();
  37. }
  38. refreshUserInfo() {
  39. getUserInfo(info => {
  40. this.setState({
  41. userInfo: info
  42. });
  43. }, true);
  44. }
  45. getDataList(lastId="") {
  46. apiVoucher.getMyVouchers({
  47. lastVoucherId: lastId,
  48. voucherType: this.state.voucherType
  49. }).then(res => {
  50. if (res.data) {
  51. if (lastId) {
  52. if (res.data.length > 0) {
  53. const list = this.state.dataList;
  54. this.setState({
  55. dataList: list.concat(res.data)
  56. });
  57. } else {
  58. this.setState({
  59. hasMore: false
  60. })
  61. }
  62. } else {
  63. this.setState({
  64. dataList: res.data,
  65. hasMore: res.data.length >= 10
  66. });
  67. }
  68. } else {
  69. this.setState({
  70. dataList: []
  71. });
  72. }
  73. }).catch(err => {
  74. toastShort(err)
  75. }).finally(() => {
  76. this.setState({
  77. refreshing: false
  78. });
  79. });
  80. }
  81. getDataListPage() {
  82. if (this.state.dataList.length > 0 && this.state.hasMore) {
  83. const last = this.state.dataList[this.state.dataList.length-1]
  84. this.getDataList(last.voucherId);
  85. }
  86. }
  87. getColorByStatus(status) {
  88. let color = colorAccent;
  89. switch (status) {
  90. case "Used":
  91. case "Expired":
  92. color = "#434343";
  93. break;
  94. case "Expiring":
  95. color = "#ED3F3F"
  96. break;
  97. }
  98. return color;
  99. }
  100. changeType(type) {
  101. this.setState({
  102. voucherType: type
  103. });
  104. this.getDataList();
  105. }
  106. toDetailPage(item) {
  107. startPage(PageList.voucherDetails, {id: item.voucherId});
  108. }
  109. listItem = ({item, index, separators}) => {
  110. return (
  111. <Pressable
  112. style={styles.itemView}
  113. onPress={() => this.toDetailPage(item)}>
  114. <View style={styles.itemBox}></View>
  115. <View style={styles.itemContent}>
  116. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  117. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  118. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  119. { !app.isLumiWhitelabel && <>
  120. <View style={styles.rightDash}></View>
  121. <View style={styles.topTikDot}></View>
  122. <View style={styles.bottomTikDot}></View>
  123. </> }
  124. </View>
  125. <TextView
  126. style={[
  127. styles.statusButton, {
  128. color: this.getColorByStatus(item.userVoucherStatus),
  129. borderColor: this.getColorByStatus(item.userVoucherStatus)
  130. }
  131. ]}>
  132. {item.userVoucherStatus}
  133. </TextView>
  134. </Pressable>
  135. )
  136. }
  137. topView = (props) => {
  138. return (
  139. <View>
  140. <ViewRedeem
  141. userInfo={this.state.userInfo}
  142. onChange={() => this.onRefresh()}/>
  143. <VoucherType
  144. type={this.state.voucherType}
  145. onChange={type => this.changeType(type)}
  146. />
  147. </View>
  148. );
  149. }
  150. bottomView = () => {
  151. if (this.state.dataList.length > 0 && !this.state.hasMore) {
  152. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  153. } else {
  154. return null
  155. }
  156. }
  157. render() {
  158. return (
  159. <FlatList
  160. style={styles.container}
  161. data={this.state.dataList}
  162. renderItem={this.listItem}
  163. ListHeaderComponent={this.topView}
  164. keyExtractor={item => item.userVoucherId}
  165. onEndReached={() => this.getDataListPage()}
  166. onEndReachedThreshold={0.3}
  167. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  168. ListFooterComponent={this.bottomView}
  169. refreshControl={
  170. <RefreshControl
  171. {...MyRefreshProps()}
  172. refreshing={this.state.refreshing}
  173. onRefresh={() => this.onRefresh()}
  174. />
  175. }/>
  176. );
  177. }
  178. }
  179. const styles = StyleSheet.create({
  180. container: {
  181. flex: 1,
  182. padding: 16
  183. },
  184. itemView: {
  185. marginTop: 16,
  186. borderRadius: 4,
  187. alignItems: 'center',
  188. flexDirection: 'row',
  189. backgroundColor: colorLight,
  190. ...$padding(0, 16)
  191. },
  192. itemBox: {
  193. top: 0,
  194. left: 0,
  195. right: 0,
  196. bottom: 0,
  197. borderWidth: 1,
  198. borderColor: '#DADADA',
  199. borderRadius: 4,
  200. position: 'absolute'
  201. },
  202. itemContent: {
  203. flex: 1,
  204. paddingTop: 12,
  205. marginRight: 16,
  206. paddingBottom: 12,
  207. overflow: 'hidden'
  208. },
  209. rightDash: {
  210. top: 0,
  211. right: 6,
  212. bottom: 0,
  213. position: 'absolute',
  214. borderStyle: 'dashed',
  215. borderRightWidth: 1,
  216. borderRightColor: '#DADADA'
  217. },
  218. topTikDot: {
  219. top: -8,
  220. right: 0,
  221. width: 13,
  222. height: 14,
  223. borderWidth: 1,
  224. borderColor: '#DADADA',
  225. borderRadius: 30,
  226. position: 'absolute',
  227. backgroundColor: pageBackground
  228. },
  229. bottomTikDot: {
  230. bottom: -8,
  231. right: 0,
  232. width: 13,
  233. height: 14,
  234. borderWidth: 1,
  235. borderColor: '#DADADA',
  236. borderRadius: 30,
  237. position: 'absolute',
  238. backgroundColor: pageBackground
  239. },
  240. voucherTitle: {
  241. color: textPrimary,
  242. fontSize: 16,
  243. fontWeight: 'bold'
  244. },
  245. voucherDesc: {
  246. color: textPrimary,
  247. fontSize: 14,
  248. paddingTop: 2,
  249. paddingBottom: 4
  250. },
  251. expireDate: {
  252. color: textCancel,
  253. fontSize: 12
  254. },
  255. statusButton: {
  256. color: colorAccent,
  257. padding: 5,
  258. minWidth: 70,
  259. fontSize: 12,
  260. marginRight: 4,
  261. textAlign: 'center',
  262. borderWidth: 1,
  263. borderColor: colorAccent,
  264. borderRadius: 6,
  265. textTransform: 'uppercase',
  266. backgroundColor: colorLight
  267. },
  268. noData: {
  269. color: textPlacehoder,
  270. fontSize: 14,
  271. padding: 20,
  272. marginTop: 16,
  273. textAlign: 'center'
  274. },
  275. noMore: {
  276. color: textPlacehoder,
  277. fontSize: 14,
  278. padding: 16,
  279. marginBottom: 16,
  280. textAlign: 'center'
  281. }
  282. })