| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /**
- * 新版支付方式选择器
- * @邠心vbe on 2021/04/13
- */
- import React, { Component } from 'react';
- import { View, Text, StyleSheet, Pressable, ScrollView } from 'react-native';
- import BottomModal from '../../components/BottomModal';
- import BadgeSelectItem from '../../components/BadgeSelectItem';
- import { ChargeStyle } from './Charging';
- import { PAYTYPE, PaymentIcon } from '../payment/PaymentConfig';
- import TextView from '../../components/TextView';
- import apiCharge from '../../api/apiCharge';
- import Button from '../../components/Button';
- import Dialog from '../../components/Dialog';
- import app from '../../../app.json';
- export default class PaymentListV2 extends Component {
- constructor(props) {
- super(props);
- this.state = {
- visible: false,
- isSelect: true,
- options: [],
- selectIndex: 0,
- selectOptions: {}
- };
- }
- componentDidMount() {
- this.getPaymentOptions();
- this.setState({
- isSelect: (this.props.isSelect || true)
- })
- }
- componentDidUpdate() {
- if (this.props.isSelect != undefined && this.state.isSelect !== this.props.isSelect) {
- this.setState({
- isSelect: this.props.isSelect
- })
- }
- }
- getPaymentOptions() {
- apiCharge.getPaymentTypeOptions({
- chargeBoxId: this.props.chargeBoxId
- }).then(res => {
- if (res.data) {
- this.setState({
- options: res.data
- })
- if (this.props.payType && this.props.payType.code) {
- for (let i = 0; i < res.data.length; i++) {
- let type = res.data[i];
- if (type.code == this.props.payType.code) {
- this.changeMethod(i);
- break;
- }
- }
- } else {
- let index = 0;
- for (let i = 0; i < res.data.length; i++) {
- let type = res.data[i];
- if (type.def) {
- index = i;
- break;
- }
- }
- this.changeMethod(index);
- }
- }
- }).catch(err => {
- })
- }
- showDialog(visible) {
- if (this.state.isSelect) {
- this.setState({
- visible: visible
- })
- }
- }
- changeMethod(index) {
- const type = this.state.options[index];
- this.setState({
- selectIndex: index,
- selectOptions: type
- }, () => {
- if (this.props.onMethodChange) {
- this.props.onMethodChange(this.state.selectOptions);
- }
- })
- }
- setDefault() {
- const code = this.state.selectOptions?.code;
- if (code && !this.state.selectOptions.def) {
- Dialog.showProgressDialog();
- apiCharge.setDefaultPaymentType({
- defaultPaymentMethod: code
- }).then(res => {
- toastShort("success");
- this.getPaymentOptions();
- }).catch(err => {
- toastShort(err)
- }).finally(() => {
- Dialog.dismissLoading();
- })
- }
- }
- render() {
- return (
- <View>
- <BadgeSelectItem
- style={this.props.style ?? ChargeStyle.stationInfoView}
- borderColor={this.props.borderColor}
- checked={false}
- onPress={() => this.showDialog(true)}>
- { app.charge.version >= 4
- ? <MaterialIcons
- name="wallet"
- size={32}
- color={textPrimary}/>
- : <PaymentIcon
- method={"WALLET"}
- checked={true}
- size={35}/>
- }
- <View style={styles.paymentView}>
- <TextView
- style={styles.valueText}
- numberOfLines={1}>{this.state.selectOptions?.name}</TextView>
- <TextView
- style={styles.paymentText}
- numberOfLines={1}>{this.state.selectOptions?.desc}</TextView>
- </View>
- { this.state.selectOptions?.def &&
- <TextView style={styles.textDefault}>Default</TextView>
- }
- { this.state.isSelect &&
- <FontAwesome6
- name={"angle-right"}
- size={16}
- color={colorCancel}
- />
- }
- </BadgeSelectItem>
- <BottomModal
- visible={this.state.visible}
- onHide={() => this.showDialog(false)}>
- <View style={styles.dialogContent}>
- <TextView style={styles.titleText}>Payment Methods</TextView>
- <ScrollView style={styles.paymentList}>
- { this.state.options.map((item, index) => (
- <Pressable
- key={index}
- style={styles.itemPayment}
- onPress={() => this.changeMethod(index)}>
- <View style={ui.flex1}>
- <TextView style={styles.valueText}>{item.name}</TextView>
- <TextView style={styles.descText}>{item.desc}</TextView>
- </View>
- { item.def &&
- <TextView style={styles.tagDefault}>Default</TextView>
- }
- <MaterialCommunityIcons
- name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
- size={24}
- color={index == this.state.selectIndex ? colorPrimary : textPrimary}
- />
- </Pressable>
- ))
- }
- </ScrollView>
- <EndView/><EndView/>
- <View style={ui.flexc}>
- <Button
- style={styles.defButton}
- text={"Set Default"}
- onClick={() => this.setDefault()}/>
- <EndView half/>
- <Button
- style={ui.flex1}
- text={"Confirm"}
- onClick={() => this.showDialog(false)}/>
- </View>
- </View>
- </BottomModal>
- </View>
- );
- }
- }
- const styles = StyleSheet.create({
- paymentView: {
- flex: 1,
- flexWrap: 'wrap',
- alignItems: 'center',
- flexDirection: 'row',
- justifyContent: 'space-around'
- },
- dialogContent: {
- padding: 16
- },
- paymentList: {
- height: $vh(50)
- },
- titleText: {
- fontSize: 16,
- fontWeight: 'bold',
- paddingBottom: 18
- },
- valueText: {
- color: textPrimary,
- fontSize: 15,
- fontWeight: 'bold'
- },
- paymentText: {
- color: textSecondary,
- fontSize: 12,
- paddingLeft: 8,
- paddingRight: 16
- },
- descText: {
- color: colorAccent,
- fontSize: 12,
- paddingTop: 2
- },
- itemPayment: {
- paddingTop: 8,
- paddingBottom: 8,
- alignItems: 'center',
- flexDirection: 'row'
- },
- tagDefault: {
- color: textLight,
- fontSize: 12,
- borderRadius: 4,
- ...$padding(4, 8),
- marginRight: 12,
- backgroundColor: colorAccent
- },
- textDefault: {
- color: colorAccent,
- fontSize: 12,
- marginRight: 8,
- fontWeight: 'bold'
- },
- defButton: {
- flex: 1,
- backgroundColor: colorPrimary
- }
- })
|