VoucherSelect.js 6.0 KB

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