PaymentListV2.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. let hasDef = false;
  66. for (let i = 0; i < res.data.length; i++) {
  67. let type = res.data[i];
  68. if (type.code == "fleet_group_wallet" || type.code == "fleet_individual_wallet") {
  69. index = i;
  70. type.def = true;
  71. hasDef = true;
  72. break;
  73. }
  74. }
  75. if (!hasDef) {
  76. for (let i = 0; i < res.data.length; i++) {
  77. let type = res.data[i];
  78. if (type.def) {
  79. index = i;
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. this.setState({
  86. options: res.data,
  87. isloading: false
  88. }, () => {
  89. this.changeMethod(index);
  90. });
  91. }
  92. }).catch(err => {
  93. })
  94. }
  95. showDialog(visible) {
  96. if (this.state.isSelect) {
  97. this.setState({
  98. visible: visible
  99. })
  100. }
  101. }
  102. changeMethod(index) {
  103. const type = this.state.options[index];
  104. this.setState({
  105. selectIndex: index,
  106. selectOptions: type
  107. }, () => {
  108. if (this.props.onMethodChange) {
  109. this.props.onMethodChange(this.state.selectOptions);
  110. }
  111. })
  112. }
  113. setDefault() {
  114. const code = this.state.selectOptions?.code;
  115. if (code && !this.state.selectOptions.def) {
  116. Dialog.showProgressDialog();
  117. apiCharge.setDefaultPaymentType({
  118. defaultPaymentMethod: code
  119. }).then(res => {
  120. toastShort("success");
  121. this.getPaymentOptions();
  122. }).catch(err => {
  123. toastShort(err)
  124. }).finally(() => {
  125. Dialog.dismissLoading();
  126. })
  127. }
  128. }
  129. render() {
  130. return (
  131. <View>
  132. <BadgeSelectItem
  133. style={this.props.style ?? ChargeStyle.stationInfoView}
  134. borderColor={this.props.borderColor}
  135. checked={false}
  136. onPress={() => this.showDialog(true)}>
  137. { app.charge.version >= 4
  138. ? <MaterialIcons
  139. name="wallet"
  140. size={32}
  141. color={textPrimary}/>
  142. : <PaymentIcon
  143. method={"WALLET"}
  144. checked={true}
  145. size={35}/>
  146. }
  147. { (this.state.isSelect && this.state.options.length > 1) &&
  148. <TextView style={styles.numberDot}>{this.state.options.length}</TextView>
  149. }
  150. <View style={styles.paymentView}>
  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: 10}]}
  157. animationDirection={"horizontalRight"}>
  158. <TextView
  159. style={styles.valueText}
  160. numberOfLines={1}>{this.state.selectOptions?.name}</TextView>
  161. </VbeSkeleton>
  162. <VbeSkeleton
  163. style={ui.flex1}
  164. viewStyle={ui.flexc}
  165. isLoading={this.state.isloading}
  166. animationType="pulse"
  167. layout={[{width: '60%', height: 20, marginLeft: 20}]}
  168. animationDirection={"horizontalRight"}>
  169. <TextView
  170. style={styles.paymentText}
  171. numberOfLines={1}>{this.state.selectOptions?.desc}</TextView>
  172. </VbeSkeleton>
  173. </View>
  174. { this.state.selectOptions?.def &&
  175. <TextView style={styles.textDefault}>Default</TextView>
  176. }
  177. { this.state.isSelect &&
  178. <FontAwesome6
  179. name={"angle-right"}
  180. size={16}
  181. color={colorCancel}
  182. />
  183. }
  184. </BadgeSelectItem>
  185. <BottomModal
  186. visible={this.state.visible}
  187. onHide={() => this.showDialog(false)}>
  188. <View style={styles.dialogContent}>
  189. <TextView style={styles.titleText}>Payment Methods</TextView>
  190. <ScrollView style={styles.paymentList}>
  191. { this.state.options.map((item, index) => (
  192. <Pressable
  193. key={index}
  194. style={styles.itemPayment}
  195. onPress={() => this.changeMethod(index)}>
  196. <View style={ui.flex1}>
  197. <TextView style={styles.labelText}>{item.name}</TextView>
  198. <TextView style={styles.descText}>{item.desc}</TextView>
  199. </View>
  200. { item.def &&
  201. <TextView style={styles.tagDefault}>Default</TextView>
  202. }
  203. <MaterialCommunityIcons
  204. name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
  205. size={24}
  206. color={index == this.state.selectIndex ? colorPrimary : textPrimary}
  207. />
  208. </Pressable>
  209. ))
  210. }
  211. </ScrollView>
  212. <EndView/><EndView/>
  213. <View style={ui.flexc}>
  214. <Button
  215. style={styles.defButton}
  216. text={"Set Default"}
  217. onClick={() => this.setDefault()}/>
  218. <EndView half/>
  219. <Button
  220. style={ui.flex1}
  221. text={"Confirm"}
  222. onClick={() => this.showDialog(false)}/>
  223. </View>
  224. </View>
  225. </BottomModal>
  226. </View>
  227. );
  228. }
  229. }
  230. const styles = StyleSheet.create({
  231. paymentView: {
  232. flex: 1,
  233. flexWrap: 'wrap',
  234. alignItems: 'center',
  235. flexDirection: 'row',
  236. justifyContent: 'space-around'
  237. },
  238. dialogContent: {
  239. padding: 16
  240. },
  241. paymentList: {
  242. height: $vh(50)
  243. },
  244. titleText: {
  245. fontSize: 16,
  246. fontWeight: 'bold',
  247. paddingBottom: 18
  248. },
  249. valueText: {
  250. flex: 1,
  251. color: textPrimary,
  252. fontSize: 14,
  253. fontWeight: 'bold',
  254. paddingLeft: 16
  255. },
  256. paymentText: {
  257. flex: 1,
  258. color: textSecondary,
  259. fontSize: 12,
  260. paddingLeft: 8,
  261. paddingRight: 16,
  262. textAlign: 'center'
  263. },
  264. labelText: {
  265. color: textPrimary,
  266. fontSize: 14,
  267. fontWeight: 'bold'
  268. },
  269. descText: {
  270. color: colorAccent,
  271. fontSize: 12,
  272. paddingTop: 2
  273. },
  274. itemPayment: {
  275. paddingTop: 8,
  276. paddingBottom: 8,
  277. alignItems: 'center',
  278. flexDirection: 'row'
  279. },
  280. tagDefault: {
  281. color: textLight,
  282. fontSize: 12,
  283. borderRadius: 4,
  284. ...$padding(4, 8),
  285. marginRight: 12,
  286. backgroundColor: colorAccent
  287. },
  288. textDefault: {
  289. color: colorAccent,
  290. fontSize: 12,
  291. marginRight: 8,
  292. fontWeight: 'bold'
  293. },
  294. defButton: {
  295. flex: 1,
  296. backgroundColor: colorPrimary
  297. },
  298. numberDot: {
  299. top: 10,
  300. left: app.charge.version >= 4 ? 32 : 35,
  301. width: 16,
  302. height: 16,
  303. color: textButton,
  304. fontSize: 10,
  305. fontWeight: 'bold',
  306. borderRadius: 32,
  307. position: 'absolute',
  308. alignItems: 'center',
  309. justifyContent: 'center',
  310. backgroundColor: app.charge.version >= 4 ? colorAccent : "#FF2622"
  311. }
  312. })