VoucherSelect.js 8.7 KB

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