ListVoucher.js 8.0 KB

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