VoucherSelect.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. const checked = (this.state.selectedVoucher.userVoucherId == item.userVoucherId);
  126. return (
  127. <BadgeSelectItem
  128. style={styles.itemView}
  129. showBorder={false}
  130. checked={checked}
  131. onPress={() => this.onSetectVoucher(item)}>
  132. <View style={[styles.itemBox, checked ? styles.activeBorder : {}]}></View>
  133. <View style={styles.itemContent}>
  134. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  135. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  136. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  137. <View style={styles.rightDash}></View>
  138. <View style={[styles.topTikDot, checked ? styles.activeBorder : {}]}></View>
  139. <View style={[styles.bottomTikDot, checked ? styles.activeBorder : {}]}></View>
  140. </View>
  141. <TextView
  142. style={[
  143. styles.statusButton, {
  144. color: this.getColorByStatus(item.userVoucherStatus),
  145. borderColor: this.getColorByStatus(item.userVoucherStatus)
  146. }
  147. ]}>
  148. {item.userVoucherStatus}
  149. </TextView>
  150. </BadgeSelectItem>
  151. )
  152. }
  153. topView = (props) => {
  154. return (
  155. <VoucherType
  156. type={this.state.voucherType}
  157. onChange={type => this.changeType(type)}
  158. />
  159. );
  160. }
  161. bottomView = () => {
  162. if (!this.state.hasMore) {
  163. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  164. } else {
  165. return null
  166. }
  167. }
  168. render() {
  169. return (
  170. <View style={styles.container}>
  171. <FlatList
  172. style={ui.flex1}
  173. data={this.state.dataList}
  174. renderItem={this.listItem}
  175. ListHeaderComponent={this.topView}
  176. keyExtractor={item => item.userVoucherId}
  177. onEndReached={() => this.getDataListPage()}
  178. onEndReachedThreshold={0.3}
  179. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  180. ListFooterComponent={this.bottomView}
  181. refreshControl={
  182. <RefreshControl
  183. {...MyRefreshProps()}
  184. refreshing={this.state.refreshing}
  185. onRefresh={() => this.onRefresh()}
  186. />
  187. }/>
  188. <Button
  189. text={$t("nav.confirm")}
  190. borderRadius={6}
  191. onClick={() => goBack()}
  192. />
  193. </View>
  194. );
  195. }
  196. }
  197. const styles = StyleSheet.create({
  198. container: {
  199. flex: 1,
  200. paddingLeft: 16,
  201. paddingRight: 16,
  202. paddingBottom: 16,
  203. backgroundColor: pageBackground
  204. },
  205. itemView: {
  206. marginTop: 16,
  207. borderRadius: 4,
  208. alignItems: 'center',
  209. flexDirection: 'row',
  210. backgroundColor: colorLight,
  211. ...$padding(0, 16)
  212. },
  213. itemBox: {
  214. top: 0,
  215. left: 0,
  216. right: 0,
  217. bottom: 0,
  218. borderWidth: 1,
  219. borderColor: '#DADADA',
  220. borderRadius: 4,
  221. position: 'absolute'
  222. },
  223. itemContent: {
  224. flex: 1,
  225. paddingTop: 12,
  226. marginRight: 16,
  227. paddingBottom: 12,
  228. overflow: 'hidden'
  229. },
  230. rightDash: {
  231. top: 0,
  232. right: 6,
  233. bottom: 0,
  234. position: 'absolute',
  235. borderStyle: 'dashed',
  236. borderRightWidth: 1,
  237. borderRightColor: '#DADADA'
  238. },
  239. topTikDot: {
  240. top: -8,
  241. right: 0,
  242. width: 13,
  243. height: 14,
  244. borderWidth: 1,
  245. borderColor: '#DADADA',
  246. borderRadius: 30,
  247. position: 'absolute',
  248. backgroundColor: pageBackground
  249. },
  250. bottomTikDot: {
  251. bottom: -8,
  252. right: 0,
  253. width: 13,
  254. height: 14,
  255. borderWidth: 1,
  256. borderColor: '#DADADA',
  257. borderRadius: 30,
  258. position: 'absolute',
  259. backgroundColor: pageBackground
  260. },
  261. activeBorder: {
  262. borderColor: colorAccent
  263. },
  264. voucherTitle: {
  265. color: textPrimary,
  266. fontSize: 16,
  267. fontWeight: 'bold'
  268. },
  269. voucherDesc: {
  270. color: textPrimary,
  271. fontSize: 14,
  272. paddingTop: 2,
  273. paddingBottom: 4
  274. },
  275. expireDate: {
  276. color: textCancel,
  277. fontSize: 12
  278. },
  279. statusButton: {
  280. color: colorAccent,
  281. padding: 5,
  282. minWidth: 70,
  283. fontSize: 12,
  284. marginRight: 4,
  285. textAlign: 'center',
  286. borderWidth: 1,
  287. borderColor: colorAccent,
  288. borderRadius: 6,
  289. textTransform: 'uppercase',
  290. backgroundColor: colorLight
  291. },
  292. noData: {
  293. color: textPlacehoder,
  294. fontSize: 14,
  295. padding: 20,
  296. marginTop: 16,
  297. textAlign: 'center'
  298. },
  299. noMore: {
  300. color: textPlacehoder,
  301. fontSize: 14,
  302. padding: 16,
  303. textAlign: 'center'
  304. }
  305. })