| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- /**
- * 我的代金券列表
- * @邠心vbe on 2024/04/09
- */
- import React, { Component } from 'react';
- import { View, Text, StyleSheet, RefreshControl, FlatList } from 'react-native';
- import { MyRefreshProps } from '../../components/ThemesConfig';
- import TextView from '../../components/TextView';
- import ViewRedeem from './ViewRedeem';
- import VoucherType from './VoucherType';
- import apiVoucher from '../../api/apiVoucher';
- export default class ListVoucher extends Component {
- constructor(props) {
- super(props);
- this.state = {
- userInfo: {},
- dataList: [],
- voucherType: "",
- hasMore: true,
- refreshing: false
- };
- }
- componentDidMount() {
- this.props.navigation.addListener('focus', () => {
- this.refreshUserInfo();
- this.getDataList();
- });
- }
- onRefresh() {
- this.setState({
- refreshing: true
- })
- this.refreshUserInfo();
- this.getDataList();
- }
- refreshUserInfo() {
- getUserInfo(info => {
- this.setState({
- userInfo: info
- });
- }, true);
- }
- getDataList(lastId="") {
- apiVoucher.getMyVouchers({
- lastVoucherId: lastId,
- voucherType: this.state.voucherType
- }).then(res => {
- if (res.data) {
- if (lastId) {
- if (res.data.length > 0) {
- const list = this.state.dataList;
- this.setState({
- dataList: list.concat(res.data)
- });
- } else {
- this.setState({
- hasMore: false
- })
- }
- } else {
- this.setState({
- dataList: res.data,
- hasMore: true
- });
- }
- } else {
- this.setState({
- dataList: []
- });
- }
- }).catch(err => {
- toastShort(err)
- }).finally(() => {
- this.setState({
- refreshing: false
- });
- });
- }
- getDataListPage() {
- if (this.state.dataList.length > 0 && this.state.hasMore) {
- const last = this.state.dataList[this.state.dataList.length-1]
- this.getDataList(last.voucherId);
- }
- }
- getColorByStatus(status) {
- let color = colorAccent;
- switch (status) {
- case "Used":
- case "Expired":
- color = "#434343";
- break;
- case "Expiring":
- color = "#ED3F3F"
- break;
- }
- return color;
- }
- changeType(type) {
- this.setState({
- voucherType: type
- });
- this.getDataList();
- }
- listItem = ({item, index, separators}) => {
- return (
- <View
- style={styles.itemView}>
- <View style={styles.itemContent}>
- <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
- <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
- <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
- </View>
- <TextView
- style={[
- styles.statusButton, {
- color: this.getColorByStatus(item.userVoucherStatus),
- borderColor: this.getColorByStatus(item.userVoucherStatus)
- }
- ]}>
- {item.userVoucherStatus}
- </TextView>
- </View>
- )
- }
- topView = (props) => {
- return (
- <View>
- <ViewRedeem
- userInfo={this.state.userInfo}
- onChange={() => this.onRefresh()}/>
- <VoucherType
- type={this.state.voucherType}
- onChange={type => this.changeType(type)}
- />
- </View>
- );
- }
- bottomView = () => {
- if (!this.state.hasMore) {
- return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
- } else {
- return null
- }
- }
- render() {
- return (
- <FlatList
- style={styles.container}
- data={this.state.dataList}
- renderItem={this.listItem}
- ListHeaderComponent={this.topView}
- keyExtractor={item => item.userVoucherId}
- onEndReached={() => this.getDataListPage()}
- onEndReachedThreshold={0.3}
- ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
- ListFooterComponent={this.bottomView}
- refreshControl={
- <RefreshControl
- {...MyRefreshProps()}
- refreshing={this.state.refreshing}
- onRefresh={() => this.onRefresh()}
- />
- }/>
- );
- }
- }
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- padding: 16
- },
- itemView: {
- padding: 16,
- marginTop: 16,
- borderWidth: 1,
- borderColor: '#DADADA',
- borderRadius: 4,
- alignItems: 'center',
- flexDirection: 'row',
- backgroundColor: colorLight
- },
- itemContent: {
- flex: 1,
- paddingRight: 16
- },
- voucherTitle: {
- color: textPrimary,
- fontSize: 16,
- fontWeight: 'bold'
- },
- voucherDesc: {
- color: textPrimary,
- fontSize: 14,
- paddingTop: 2,
- paddingBottom: 4
- },
- expireDate: {
- color: textCancel,
- fontSize: 12
- },
- statusButton: {
- color: colorAccent,
- padding: 8,
- minWidth: 73,
- fontSize: 12,
- textAlign: 'center',
- borderWidth: 1,
- borderColor: colorAccent,
- borderRadius: 6,
- textTransform: 'uppercase',
- backgroundColor: colorLight
- },
- noData: {
- color: textPlacehoder,
- fontSize: 14,
- padding: 20,
- marginTop: 16,
- textAlign: 'center'
- },
- noMore: {
- color: textPlacehoder,
- fontSize: 14,
- padding: 16,
- textAlign: 'center'
- }
- })
|