VoucherSelect.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import React, { Component } from 'react';
  2. import { View, Text, StyleSheet, RefreshControl, FlatList, Pressable } from 'react-native';
  3. import VoucherType from './VoucherType';
  4. import apiVoucher from '../../api/apiVoucher';
  5. import { MyRefreshProps } from '../../components/ThemesConfig';
  6. import TextView from '../../components/TextView';
  7. import BadgeSelectItem from '../../components/BadgeSelectItem';
  8. import Button from '../../components/Button';
  9. import PagerUtil from '../chargeV2/PagerUtil';
  10. export default class VoucherSelect extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. dataList: [],
  15. voucherType: "",
  16. hasMore: true,
  17. refreshing: false,
  18. selectedVoucher: {}
  19. };
  20. this.params = {
  21. chargeBoxId: "",
  22. connectorId: ""
  23. }
  24. }
  25. componentDidMount() {
  26. const params = this.props.route.params;
  27. if (params.chargeBoxId) {
  28. this.params.chargeBoxId = params.chargeBoxId
  29. }
  30. if (params.connectorId) {
  31. this.params.connectorId = params.connectorId
  32. }
  33. console.log("选择优惠券", this.params);
  34. this.getDataList();
  35. this.props.navigation.addListener('beforeRemove', (e) => {
  36. PagerUtil.setSelectedVoucher(this.state.selectedVoucher);
  37. });
  38. }
  39. onRefresh() {
  40. this.setState({
  41. refreshing: true
  42. })
  43. this.getDataList();
  44. }
  45. getDataList(lastId) {
  46. apiVoucher.getSelectionVoucher({
  47. ...this.params,
  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. hasMore: true
  57. });
  58. } else {
  59. this.setState({
  60. hasMore: false
  61. })
  62. }
  63. } else {
  64. this.setState({
  65. dataList: res.data,
  66. hasMore: res.data.length == 0
  67. });
  68. }
  69. } else {
  70. this.setState({
  71. dataList: [],
  72. hasMore: true
  73. });
  74. }
  75. }).catch(err => {
  76. toastShort(err)
  77. this.setState({
  78. dataList: [],
  79. hasMore: true
  80. });
  81. }).finally(() => {
  82. this.setState({
  83. refreshing: false,
  84. selectedVoucher: {}
  85. });
  86. });
  87. }
  88. getDataListPage() {
  89. if (this.state.dataList.length > 0 && this.state.hasMore) {
  90. const last = this.state.dataList[this.state.dataList.length-1]
  91. this.getDataList(last.voucherId);
  92. }
  93. }
  94. getColorByStatus(status) {
  95. let color = colorAccent;
  96. switch (status) {
  97. case "Used":
  98. case "Expired":
  99. color = "#434343";
  100. break;
  101. case "Expiring":
  102. color = "#ED3F3F"
  103. break;
  104. }
  105. return color;
  106. }
  107. changeType(type) {
  108. this.setState({
  109. voucherType: type
  110. });
  111. this.getDataList();
  112. }
  113. onSetectVoucher(item) {
  114. if (item.userVoucherId != this.state.selectedVoucher.userVoucherId) {
  115. this.setState({
  116. selectedVoucher: item
  117. })
  118. } else {
  119. this.setState({
  120. selectedVoucher: {}
  121. })
  122. }
  123. }
  124. listItem = ({item, index, separators}) => {
  125. return (
  126. <BadgeSelectItem
  127. style={styles.itemView}
  128. borderColor='#DADADA'
  129. checked={this.state.selectedVoucher.userVoucherId == item.userVoucherId}
  130. onPress={() => this.onSetectVoucher(item)}>
  131. <View style={styles.itemContent}>
  132. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  133. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  134. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  135. </View>
  136. <TextView
  137. style={[
  138. styles.statusButton, {
  139. color: this.getColorByStatus(item.userVoucherStatus),
  140. borderColor: this.getColorByStatus(item.userVoucherStatus)
  141. }
  142. ]}>
  143. {item.userVoucherStatus}
  144. </TextView>
  145. </BadgeSelectItem>
  146. )
  147. }
  148. topView = (props) => {
  149. return (
  150. <VoucherType
  151. type={this.state.voucherType}
  152. onChange={type => this.changeType(type)}
  153. />
  154. );
  155. }
  156. bottomView = () => {
  157. if (!this.state.hasMore) {
  158. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  159. } else {
  160. return null
  161. }
  162. }
  163. render() {
  164. return (
  165. <View style={styles.container}>
  166. <FlatList
  167. style={ui.flex1}
  168. data={this.state.dataList}
  169. renderItem={this.listItem}
  170. ListHeaderComponent={this.topView}
  171. keyExtractor={item => item.userVoucherId}
  172. onEndReached={() => this.getDataListPage()}
  173. onEndReachedThreshold={0.3}
  174. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  175. ListFooterComponent={this.bottomView}
  176. refreshControl={
  177. <RefreshControl
  178. {...MyRefreshProps()}
  179. refreshing={this.state.refreshing}
  180. onRefresh={() => this.onRefresh()}
  181. />
  182. }/>
  183. <Button
  184. text={$t("nav.confirm")}
  185. borderRadius={6}
  186. onClick={() => goBack()}
  187. />
  188. </View>
  189. );
  190. }
  191. }
  192. const styles = StyleSheet.create({
  193. container: {
  194. flex: 1,
  195. paddingLeft: 16,
  196. paddingRight: 16,
  197. paddingBottom: 16
  198. },
  199. itemView: {
  200. padding: 16,
  201. marginTop: 16,
  202. borderWidth: 1,
  203. borderColor: '#DADADA',
  204. borderRadius: 4,
  205. alignItems: 'center',
  206. flexDirection: 'row',
  207. backgroundColor: colorLight
  208. },
  209. itemContent: {
  210. flex: 1,
  211. paddingRight: 16
  212. },
  213. voucherTitle: {
  214. color: textPrimary,
  215. fontSize: 16,
  216. fontWeight: 'bold'
  217. },
  218. voucherDesc: {
  219. color: textPrimary,
  220. fontSize: 14,
  221. paddingTop: 2,
  222. paddingBottom: 4
  223. },
  224. expireDate: {
  225. color: textCancel,
  226. fontSize: 12
  227. },
  228. statusButton: {
  229. color: colorAccent,
  230. padding: 8,
  231. minWidth: 73,
  232. fontSize: 12,
  233. textAlign: 'center',
  234. borderWidth: 1,
  235. borderColor: colorAccent,
  236. borderRadius: 6,
  237. textTransform: 'uppercase',
  238. backgroundColor: colorLight
  239. },
  240. noData: {
  241. color: textPlacehoder,
  242. fontSize: 14,
  243. padding: 20,
  244. marginTop: 16,
  245. textAlign: 'center'
  246. },
  247. noMore: {
  248. color: textPlacehoder,
  249. fontSize: 14,
  250. padding: 16,
  251. textAlign: 'center'
  252. }
  253. })