ListPoints.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 ViewRedeem from './ViewRedeem';
  8. import apiVoucher from '../../api/apiVoucher';
  9. import { MyRefreshProps } from '../../components/ThemesConfig';
  10. import TextView from '../../components/TextView';
  11. import Button from '../../components/Button';
  12. import Dialog from '../../components/Dialog';
  13. import VoucherType from './VoucherType';
  14. import app from '../../../app.json';
  15. import VbeSkeleton from '../../components/VbeSkeleton';
  16. export default class ListPoints extends Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. loading: true,
  21. userInfo: {},
  22. dataList: [],
  23. voucherType: "",
  24. hasMore: true,
  25. refreshing: false,
  26. loadingList: ["","","",""]
  27. };
  28. }
  29. componentDidMount() {
  30. this.props.navigation.addListener('focus', () => {
  31. this.refreshUserInfo();
  32. this.getDataList();
  33. });
  34. }
  35. onRefresh() {
  36. this.setState({
  37. refreshing: true
  38. })
  39. this.refreshUserInfo();
  40. this.getDataList();
  41. }
  42. refreshUserInfo() {
  43. getUserInfo(info => {
  44. this.setState({
  45. userInfo: info
  46. });
  47. }, true);
  48. }
  49. getDataList(lastId="") {
  50. apiVoucher.getDealVouchers({
  51. lastVoucherId: lastId,
  52. voucherType: this.state.voucherType
  53. }).then(res => {
  54. if (res.data) {
  55. if (lastId) {
  56. if (res.data.length > 0) {
  57. const list = this.state.dataList;
  58. this.setState({
  59. dataList: list.concat(res.data)
  60. });
  61. } else {
  62. this.setState({
  63. hasMore: false
  64. })
  65. }
  66. } else {
  67. this.setState({
  68. dataList: res.data,
  69. hasMore: res.data.length >= 10
  70. });
  71. }
  72. } else {
  73. this.setState({
  74. dataList: []
  75. });
  76. }
  77. }).catch(err => {
  78. toastShort(err)
  79. }).finally(() => {
  80. this.setState({
  81. loading: false,
  82. refreshing: false
  83. });
  84. });
  85. }
  86. getDataListPage() {
  87. if (this.state.dataList.length > 0 && this.state.hasMore) {
  88. const last = this.state.dataList[this.state.dataList.length-1]
  89. this.getDataList(last.voucherId);
  90. }
  91. }
  92. confirmPurchase(item) {
  93. let msg = "" + $t("voucher.confirmPurchase");
  94. if (item.purchasePoints) {
  95. msg = msg.replace("{value}", item.purchasePoints);
  96. } else {
  97. msg = msg.replace("{value}", "0");
  98. }
  99. Dialog.showDialog({
  100. title: $t("voucher.purchaseVoucher"),
  101. message: msg,
  102. ok: $t("nav.confirm"),
  103. callback: btn => {
  104. if (btn == Dialog.BUTTON_OK) {
  105. this.onPurchase(item)
  106. }
  107. }
  108. })
  109. }
  110. onPurchase(item) {
  111. Dialog.showProgressDialog();
  112. apiVoucher.purchaseVoucher({
  113. voucherId: item.voucherId
  114. }).then(res => {
  115. Dialog.dismissLoading();
  116. if (res.msg) {
  117. setTimeout(() => {
  118. Dialog.showDialog({
  119. title: $t("voucher.purchaseVoucher"),
  120. message: res.msg,
  121. showCancel: false
  122. })
  123. }, 500);
  124. }
  125. setTimeout(() => {
  126. this.refreshUserInfo();
  127. }, 2000)
  128. }).catch(err => {
  129. Dialog.dismissLoading();
  130. if (err.err) {
  131. setTimeout(() => {
  132. Dialog.showDialog({
  133. title: $t("voucher.vouchers"),
  134. message: err.err,
  135. showCancel: false
  136. })
  137. }, 500);
  138. }
  139. })
  140. }
  141. changeType(type) {
  142. this.setState({
  143. voucherType: type
  144. });
  145. this.getDataList();
  146. }
  147. listItem = ({item, index, separators}) => {
  148. return (
  149. <View
  150. style={styles.itemView}>
  151. <View style={styles.itemBox}></View>
  152. <View style={styles.itemContent}>
  153. <TextView style={styles.voucherTitle} numberOfLines={1}>{item.voucherName}</TextView>
  154. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  155. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  156. { !app.isLumiWhitelabel && <>
  157. <View style={styles.rightDash}></View>
  158. <View style={styles.topTikDot}></View>
  159. <View style={styles.bottomTikDot}></View>
  160. </> }
  161. </View>
  162. { item.purchasePoints > 0
  163. ? <Button
  164. viewStyle={styles.purchaseButton}
  165. borderRadius={6}
  166. onClick={() => this.confirmPurchase(item)}>
  167. <TextView
  168. style={styles.getForText}>
  169. {$t("voucher.btnGetFor")}
  170. </TextView>
  171. <TextView
  172. style={styles.getValueText}>
  173. {item.purchasePoints}
  174. {$t("voucher.btnPoints")}
  175. </TextView>
  176. </Button>
  177. : <Button
  178. style={styles.claimeButton}
  179. viewStyle={styles.claimeButtonView}
  180. textStyle={styles.claimeButtonText}
  181. text={$t("voucher.btnClaimed")}
  182. onClick={() => this.onPurchase(item)}/>
  183. }
  184. </View>
  185. )
  186. }
  187. topView = (props) => {
  188. return (
  189. <View>
  190. <ViewRedeem
  191. userInfo={this.state.userInfo}/>
  192. <VoucherType
  193. type={this.state.voucherType}
  194. onChange={type => this.changeType(type)}
  195. />
  196. </View>
  197. )
  198. }
  199. bottomView = () => {
  200. if (this.state.dataList.length > 0 && !this.state.hasMore) {
  201. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  202. } else {
  203. return null
  204. }
  205. }
  206. render() {
  207. if (this.state.loading) {
  208. return (
  209. <View style={styles.container}>
  210. <VbeSkeleton
  211. style={{}}
  212. layout={[
  213. {width: '100%', height: 42, borderRadius: 10},
  214. {width: '40%', height: 18, marginTop: 16}
  215. ]}
  216. animationDirection={"horizontalRight"}
  217. />
  218. <EndView/>
  219. { this.state.loadingList.map((item, index) =>
  220. <View style={styles.loadingView} key={index}>
  221. <VbeSkeleton
  222. style={ui.flex1}
  223. layout={[
  224. {width: '50%', height: 18},
  225. {width: '90%', height: 12, marginTop: 8},
  226. {width: '40%', height: 12, marginTop: 4}
  227. ]}
  228. animationDirection={"horizontalRight"}
  229. />
  230. <VbeSkeleton
  231. style={{width: 56}}
  232. layout={[
  233. {width: 56, height: 30, borderRadius: 30}
  234. ]}
  235. animationDirection={"horizontalRight"}
  236. />
  237. </View>
  238. )}
  239. </View>
  240. )
  241. }
  242. return (
  243. <FlatList
  244. style={styles.container}
  245. data={this.state.dataList}
  246. renderItem={this.listItem}
  247. ListHeaderComponent={this.topView}
  248. keyExtractor={item => item.voucherId}
  249. onEndReached={() => this.getDataListPage()}
  250. onEndReachedThreshold={0.3}
  251. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  252. ListFooterComponent={this.bottomView}
  253. refreshControl={
  254. <RefreshControl
  255. {...MyRefreshProps()}
  256. refreshing={this.state.refreshing}
  257. onRefresh={() => this.onRefresh()}
  258. />
  259. }/>
  260. );
  261. }
  262. }
  263. const styles = StyleSheet.create({
  264. container: {
  265. flex: 1,
  266. padding: 16
  267. },
  268. itemView: {
  269. marginTop: 16,
  270. borderRadius: 4,
  271. alignItems: 'center',
  272. flexDirection: 'row',
  273. backgroundColor: colorLight,
  274. ...$padding(0, 20, 0, 16)
  275. },
  276. itemBox: {
  277. top: 0,
  278. left: 0,
  279. right: 0,
  280. bottom: 0,
  281. borderWidth: 1,
  282. borderColor: '#DADADA',
  283. borderRadius: 4,
  284. position: 'absolute'
  285. },
  286. itemContent: {
  287. flex: 1,
  288. paddingTop: 12,
  289. marginRight: 16,
  290. paddingBottom: 12,
  291. overflow: 'hidden'
  292. },
  293. rightDash: {
  294. top: 0,
  295. right: 6,
  296. bottom: 0,
  297. position: 'absolute',
  298. borderStyle: 'dashed',
  299. borderRightWidth: 1,
  300. borderRightColor: '#DADADA'
  301. },
  302. topTikDot: {
  303. top: -8,
  304. right: 0,
  305. width: 13,
  306. height: 14,
  307. borderWidth: 1,
  308. borderColor: '#DADADA',
  309. borderRadius: 30,
  310. position: 'absolute',
  311. backgroundColor: pageBackground
  312. },
  313. bottomTikDot: {
  314. bottom: -8,
  315. right: 0,
  316. width: 13,
  317. height: 14,
  318. borderWidth: 1,
  319. borderColor: '#DADADA',
  320. borderRadius: 30,
  321. position: 'absolute',
  322. backgroundColor: pageBackground
  323. },
  324. voucherTitle: {
  325. color: textPrimary,
  326. fontSize: 16,
  327. fontWeight: 'bold'
  328. },
  329. voucherDesc: {
  330. color: textPrimary,
  331. fontSize: 14,
  332. paddingTop: 2,
  333. paddingBottom: 4
  334. },
  335. expireDate: {
  336. color: textCancel,
  337. fontSize: 12
  338. },
  339. purchaseButton: {
  340. padding: 4,
  341. minWidth: 75,
  342. marginRight: 4,
  343. alignItems: 'center'
  344. },
  345. claimeButton: {
  346. borderWidth: 1,
  347. borderColor: colorAccent,
  348. borderRadius: 6,
  349. backgroundColor: colorLight
  350. },
  351. claimeButtonView: {
  352. padding: 8,
  353. minWidth: 73,
  354. alignItems: 'center'
  355. },
  356. claimeButtonText: {
  357. color: colorAccent,
  358. fontSize: 12,
  359. fontWeight: 'bold'
  360. },
  361. getForText: {
  362. color: textLight,
  363. fontSize: 12,
  364. fontWeight: 'bold'
  365. },
  366. getValueText: {
  367. color: textLight,
  368. fontSize: 10
  369. },
  370. noData: {
  371. color: textPlacehoder,
  372. fontSize: 14,
  373. padding: 20,
  374. marginTop: 16,
  375. textAlign: 'center'
  376. },
  377. noMore: {
  378. color: textPlacehoder,
  379. fontSize: 14,
  380. padding: 16,
  381. marginBottom: 16,
  382. textAlign: 'center'
  383. },
  384. loadingView: {
  385. paddingTop: 16,
  386. paddingBottom: 16,
  387. alignItems: 'center',
  388. flexDirection: 'row'
  389. }
  390. })