PaymentListV2.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * 新版支付方式选择器
  3. * @邠心vbe on 2021/04/13
  4. */
  5. import React, { Component } from 'react';
  6. import { View, StyleSheet, Pressable, ScrollView } from 'react-native';
  7. import BottomModal from '../../components/BottomModal';
  8. import BadgeSelectItem from '../../components/BadgeSelectItem';
  9. import { ChargeStyle } from './Charging';
  10. import { PAYTYPE, PaymentIcon } from '../payment/PaymentConfig';
  11. import Skeleton from "react-native-reanimated-skeleton";
  12. import TextView from '../../components/TextView';
  13. import apiCharge from '../../api/apiCharge';
  14. import Button from '../../components/Button';
  15. import Dialog from '../../components/Dialog';
  16. import app from '../../../app.json';
  17. import utils from '../../utils/utils';
  18. import VbeSkeleton from '../../components/VbeSkeleton';
  19. export default class PaymentListV2 extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. visible: false,
  24. isSelect: true,
  25. options: [],
  26. selectIndex: 0,
  27. selectOptions: {},
  28. isloading: true
  29. };
  30. }
  31. componentDidMount() {
  32. this.getPaymentOptions();
  33. this.setState({
  34. isSelect: (this.props.isSelect || true)
  35. })
  36. }
  37. componentDidUpdate() {
  38. if (this.props.isSelect != undefined && this.state.isSelect !== this.props.isSelect) {
  39. this.setState({
  40. isSelect: this.props.isSelect
  41. })
  42. }
  43. }
  44. getPaymentOptions() {
  45. if (utils.isEmpty(this.props.chargeBoxId)) {
  46. setTimeout(() => {
  47. this.getPaymentOptions();
  48. }, 1000);
  49. return;
  50. }
  51. apiCharge.getPaymentTypeOptions({
  52. chargeBoxId: this.props.chargeBoxId
  53. }).then(res => {
  54. if (res.data) {
  55. let index = 0;
  56. if (this.props.payType && this.props.payType.code) {
  57. for (let i = 0; i < res.data.length; i++) {
  58. let type = res.data[i];
  59. if (type.code == this.props.payType.code) {
  60. index = i;
  61. break;
  62. }
  63. }
  64. } else {
  65. for (let i = 0; i < res.data.length; i++) {
  66. let type = res.data[i];
  67. if (type.code == "fleet_group_wallet" || type.code == "fleet_individual_wallet") {
  68. index = i;
  69. type.def = true;
  70. break;
  71. }
  72. }
  73. }
  74. this.setState({
  75. options: res.data,
  76. isloading: false
  77. }, () => {
  78. this.changeMethod(index);
  79. });
  80. }
  81. }).catch(err => {
  82. })
  83. }
  84. showDialog(visible) {
  85. if (this.state.isSelect) {
  86. this.setState({
  87. visible: visible
  88. })
  89. }
  90. }
  91. changeMethod(index) {
  92. const type = this.state.options[index];
  93. this.setState({
  94. selectIndex: index,
  95. selectOptions: type
  96. }, () => {
  97. if (this.props.onMethodChange) {
  98. this.props.onMethodChange(this.state.selectOptions);
  99. }
  100. })
  101. }
  102. setDefault() {
  103. const code = this.state.selectOptions?.code;
  104. if (code && !this.state.selectOptions.def) {
  105. Dialog.showProgressDialog();
  106. apiCharge.setDefaultPaymentType({
  107. defaultPaymentMethod: code
  108. }).then(res => {
  109. toastShort("success");
  110. this.getPaymentOptions();
  111. }).catch(err => {
  112. toastShort(err)
  113. }).finally(() => {
  114. Dialog.dismissLoading();
  115. })
  116. }
  117. }
  118. render() {
  119. return (
  120. <View>
  121. <BadgeSelectItem
  122. style={this.props.style ?? ChargeStyle.stationInfoView}
  123. borderColor={this.props.borderColor}
  124. checked={false}
  125. onPress={() => this.showDialog(true)}>
  126. { app.charge.version >= 4
  127. ? <MaterialIcons
  128. name="wallet"
  129. size={32}
  130. color={textPrimary}/>
  131. : <PaymentIcon
  132. method={"WALLET"}
  133. checked={true}
  134. size={35}/>
  135. }
  136. { (this.state.isSelect && this.state.options.length > 1) &&
  137. <TextView style={styles.numberDot}>{this.state.options.length}</TextView>
  138. }
  139. <View style={styles.paymentView}>
  140. <VbeSkeleton
  141. style={ui.flex1}
  142. viewStyle={ui.flexc}
  143. isLoading={this.state.isloading}
  144. animationType="pulse"
  145. layout={[{width: '60%', height: 20, marginLeft: 10}]}
  146. animationDirection={"horizontalRight"}>
  147. <TextView
  148. style={styles.valueText}
  149. numberOfLines={1}>{this.state.selectOptions?.name}</TextView>
  150. </VbeSkeleton>
  151. <VbeSkeleton
  152. style={ui.flex1}
  153. viewStyle={ui.flexc}
  154. isLoading={this.state.isloading}
  155. animationType="pulse"
  156. layout={[{width: '60%', height: 20, marginLeft: 20}]}
  157. animationDirection={"horizontalRight"}>
  158. <TextView
  159. style={styles.paymentText}
  160. numberOfLines={1}>{this.state.selectOptions?.desc}</TextView>
  161. </VbeSkeleton>
  162. </View>
  163. { this.state.selectOptions?.def &&
  164. <TextView style={styles.textDefault}>Default</TextView>
  165. }
  166. { this.state.isSelect &&
  167. <FontAwesome6
  168. name={"angle-right"}
  169. size={16}
  170. color={colorCancel}
  171. />
  172. }
  173. </BadgeSelectItem>
  174. <BottomModal
  175. visible={this.state.visible}
  176. onHide={() => this.showDialog(false)}>
  177. <View style={styles.dialogContent}>
  178. <TextView style={styles.titleText}>Payment Methods</TextView>
  179. <ScrollView style={styles.paymentList}>
  180. { this.state.options.map((item, index) => (
  181. <Pressable
  182. key={index}
  183. style={styles.itemPayment}
  184. onPress={() => this.changeMethod(index)}>
  185. <View style={ui.flex1}>
  186. <TextView style={styles.labelText}>{item.name}</TextView>
  187. <TextView style={styles.descText}>{item.desc}</TextView>
  188. </View>
  189. { item.def &&
  190. <TextView style={styles.tagDefault}>Default</TextView>
  191. }
  192. <MaterialCommunityIcons
  193. name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
  194. size={24}
  195. color={index == this.state.selectIndex ? colorPrimary : textPrimary}
  196. />
  197. </Pressable>
  198. ))
  199. }
  200. </ScrollView>
  201. <EndView/><EndView/>
  202. <View style={ui.flexc}>
  203. <Button
  204. style={styles.defButton}
  205. text={"Set Default"}
  206. onClick={() => this.setDefault()}/>
  207. <EndView half/>
  208. <Button
  209. style={ui.flex1}
  210. text={"Confirm"}
  211. onClick={() => this.showDialog(false)}/>
  212. </View>
  213. </View>
  214. </BottomModal>
  215. </View>
  216. );
  217. }
  218. }
  219. const styles = StyleSheet.create({
  220. paymentView: {
  221. flex: 1,
  222. flexWrap: 'wrap',
  223. alignItems: 'center',
  224. flexDirection: 'row',
  225. justifyContent: 'space-around'
  226. },
  227. dialogContent: {
  228. padding: 16
  229. },
  230. paymentList: {
  231. height: $vh(50)
  232. },
  233. titleText: {
  234. fontSize: 16,
  235. fontWeight: 'bold',
  236. paddingBottom: 18
  237. },
  238. valueText: {
  239. flex: 1,
  240. color: textPrimary,
  241. fontSize: 14,
  242. fontWeight: 'bold',
  243. paddingLeft: 16
  244. },
  245. paymentText: {
  246. flex: 1,
  247. color: textSecondary,
  248. fontSize: 12,
  249. paddingLeft: 8,
  250. paddingRight: 16,
  251. textAlign: 'center'
  252. },
  253. labelText: {
  254. color: textPrimary,
  255. fontSize: 14,
  256. fontWeight: 'bold'
  257. },
  258. descText: {
  259. color: colorAccent,
  260. fontSize: 12,
  261. paddingTop: 2
  262. },
  263. itemPayment: {
  264. paddingTop: 8,
  265. paddingBottom: 8,
  266. alignItems: 'center',
  267. flexDirection: 'row'
  268. },
  269. tagDefault: {
  270. color: textLight,
  271. fontSize: 12,
  272. borderRadius: 4,
  273. ...$padding(4, 8),
  274. marginRight: 12,
  275. backgroundColor: colorAccent
  276. },
  277. textDefault: {
  278. color: colorAccent,
  279. fontSize: 12,
  280. marginRight: 8,
  281. fontWeight: 'bold'
  282. },
  283. defButton: {
  284. flex: 1,
  285. backgroundColor: colorPrimary
  286. },
  287. numberDot: {
  288. top: 10,
  289. left: app.charge.version >= 4 ? 32 : 35,
  290. width: 16,
  291. height: 16,
  292. color: textButton,
  293. fontSize: 10,
  294. fontWeight: 'bold',
  295. borderRadius: 32,
  296. position: 'absolute',
  297. alignItems: 'center',
  298. justifyContent: 'center',
  299. backgroundColor: app.charge.version >= 4 ? colorAccent : "#FF2622"
  300. }
  301. })