VoucherSelect.js 7.6 KB

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