PaymentListV2.js 8.3 KB

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