PaymentListV2.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * 新版支付方式选择器
  3. * @邠心vbe on 2021/04/13
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, 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 TextView from '../../components/TextView';
  12. import apiCharge from '../../api/apiCharge';
  13. import Button from '../../components/Button';
  14. import Dialog from '../../components/Dialog';
  15. export default class PaymentListV2 extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. visible: false,
  20. isSelect: true,
  21. options: [],
  22. selectIndex: 0,
  23. selectOptions: {}
  24. };
  25. }
  26. componentDidMount() {
  27. this.getPaymentOptions();
  28. this.setState({
  29. isSelect: (this.props.isSelect || true)
  30. })
  31. }
  32. componentDidUpdate() {
  33. if (this.props.isSelect != undefined && this.state.isSelect !== this.props.isSelect) {
  34. this.setState({
  35. isSelect: this.props.isSelect
  36. })
  37. }
  38. }
  39. getPaymentOptions() {
  40. apiCharge.getPaymentTypeOptions({
  41. chargeBoxId: this.props.chargeBoxId
  42. }).then(res => {
  43. if (res.data) {
  44. this.setState({
  45. options: res.data
  46. })
  47. if (this.props.payType && this.props.payType.code) {
  48. for (let i = 0; i < res.data.length; i++) {
  49. let type = res.data[i];
  50. if (type.code == this.props.payType.code) {
  51. this.changeMethod(i);
  52. break;
  53. }
  54. }
  55. } else {
  56. let index = 0;
  57. for (let i = 0; i < res.data.length; i++) {
  58. let type = res.data[i];
  59. if (type.def) {
  60. index = i;
  61. break;
  62. }
  63. }
  64. this.changeMethod(index);
  65. }
  66. }
  67. }).catch(err => {
  68. })
  69. }
  70. showDialog(visible) {
  71. if (this.state.isSelect) {
  72. this.setState({
  73. visible: visible
  74. })
  75. }
  76. }
  77. changeMethod(index) {
  78. const type = this.state.options[index];
  79. this.setState({
  80. selectIndex: index,
  81. selectOptions: type
  82. }, () => {
  83. if (this.props.onMethodChange) {
  84. this.props.onMethodChange(this.state.selectOptions);
  85. }
  86. })
  87. }
  88. setDefault() {
  89. const code = this.state.selectOptions?.code;
  90. if (code && !this.state.selectOptions.def) {
  91. Dialog.showProgressDialog();
  92. apiCharge.setDefaultPaymentType({
  93. defaultPaymentMethod: code
  94. }).then(res => {
  95. toastShort("success");
  96. this.getPaymentOptions();
  97. }).catch(err => {
  98. toastShort(err)
  99. }).finally(() => {
  100. Dialog.dismissLoading();
  101. })
  102. }
  103. }
  104. render() {
  105. return (
  106. <View>
  107. <BadgeSelectItem
  108. style={ChargeStyle.stationInfoView}
  109. checked={false}
  110. onPress={() => this.showDialog(true)}>
  111. <PaymentIcon
  112. method={"WALLET"}
  113. checked={true}/>
  114. <View style={styles.paymentView}>
  115. <TextView style={styles.valueText}>{this.state.selectOptions?.name}</TextView>
  116. <TextView style={styles.paymentText}>{this.state.selectOptions?.desc}</TextView>
  117. </View>
  118. { this.state.selectOptions?.def &&
  119. <TextView style={styles.textDefault}>Default</TextView>
  120. }
  121. { this.state.isSelect &&
  122. <FontAwesome6
  123. name={"angle-right"}
  124. size={16}
  125. color={colorCancel}
  126. />
  127. }
  128. </BadgeSelectItem>
  129. <BottomModal
  130. visible={this.state.visible}
  131. onHide={() => this.showDialog(false)}>
  132. <View style={styles.dialogContent}>
  133. <TextView style={styles.titleText}>Payment Methods</TextView>
  134. <ScrollView style={styles.paymentList}>
  135. { this.state.options.map((item, index) => (
  136. <Pressable
  137. key={index}
  138. style={styles.itemPayment}
  139. onPress={() => this.changeMethod(index)}>
  140. <View style={ui.flex1}>
  141. <TextView style={styles.valueText}>{item.name}</TextView>
  142. <TextView style={styles.descText}>{item.desc}</TextView>
  143. </View>
  144. { item.def &&
  145. <TextView style={styles.tagDefault}>Default</TextView>
  146. }
  147. <MaterialCommunityIcons
  148. name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
  149. size={24}
  150. color={index == this.state.selectIndex ? colorPrimary : textPrimary}
  151. />
  152. </Pressable>
  153. ))
  154. }
  155. </ScrollView>
  156. <EndView/><EndView/>
  157. <View style={ui.flexc}>
  158. <Button
  159. style={styles.defButton}
  160. text={"Set Default"}
  161. onClick={() => this.setDefault()}/>
  162. <EndView half/>
  163. <Button
  164. style={ui.flex1}
  165. text={"Confirm"}
  166. onClick={() => this.showDialog(false)}/>
  167. </View>
  168. </View>
  169. </BottomModal>
  170. </View>
  171. );
  172. }
  173. }
  174. const styles = StyleSheet.create({
  175. paymentView: {
  176. flex: 1,
  177. alignItems: 'center',
  178. flexDirection: 'row',
  179. justifyContent: 'space-around'
  180. },
  181. dialogContent: {
  182. padding: 16
  183. },
  184. paymentList: {
  185. height: $vh(50)
  186. },
  187. titleText: {
  188. fontSize: 16,
  189. fontWeight: 'bold',
  190. paddingBottom: 18
  191. },
  192. valueText: {
  193. color: textPrimary,
  194. fontSize: 15,
  195. fontWeight: 'bold'
  196. },
  197. paymentText: {
  198. color: textSecondary,
  199. fontSize: 12,
  200. paddingLeft: 8,
  201. paddingRight: 16
  202. },
  203. descText: {
  204. color: colorAccent,
  205. fontSize: 12,
  206. paddingTop: 2
  207. },
  208. itemPayment: {
  209. paddingTop: 8,
  210. paddingBottom: 8,
  211. alignItems: 'center',
  212. flexDirection: 'row'
  213. },
  214. tagDefault: {
  215. color: textLight,
  216. fontSize: 12,
  217. borderRadius: 4,
  218. ...$padding(4, 8),
  219. marginRight: 12,
  220. backgroundColor: colorAccent
  221. },
  222. textDefault: {
  223. color: colorAccent,
  224. fontSize: 12,
  225. marginRight: 8,
  226. fontWeight: 'bold'
  227. },
  228. defButton: {
  229. flex: 1,
  230. backgroundColor: colorPrimary
  231. }
  232. })