WalletWithdraw.js 7.88 KB
Newer Older
Giang Tran committed
1 2 3 4 5 6 7 8 9
import React, {useState, useEffect} from 'react';
import {
  View,
  Text,
  StyleSheet,
  Dimensions,
  TouchableOpacity,
  Platform,
  Alert,
Giang Tran committed
10 11 12
  TouchableWithoutFeedback,
  Keyboard,
  KeyboardAvoidingView,
Giang Tran committed
13
  ScrollView,
Giang Tran committed
14
} from 'react-native';
Giang Tran committed
15 16 17 18 19
import R from '../../../assets/R';
import HeaderBack from '../../../components/Header/HeaderBack';
import TextField from '../../../components/Input/TextField';
import TextMulti from '../../../components/Input/TextMulti';
import TextDisable from '../../../components/Input/TextDisable';
Giang Tran committed
20 21 22 23 24 25 26
import {connect} from 'react-redux';
import {
  getFontXD,
  HEIGHTXD,
  toPriceVnd,
  checkFormatArray,
} from '../../../Config/Functions';
Giang Tran committed
27
import PickerItem from '../../../components/Picker/PickerItem';
Giang Tran committed
28 29 30 31
import RadioForm from 'react-native-simple-radio-button';
import {widthDraw, getListWidthDraw} from '../../../apis/Functions/Widthdraw';
import {useNavigation} from '@react-navigation/native';
import {showLoading, hideLoading} from '../../../actions/loadingAction';
Giang Tran committed
32 33
import {confirmAlert} from '../../../components/Aleart';
import {ADDMETHODPAY} from '../../../routers/ScreenNames';
Giang Tran committed
34
import TextMoney from '../../../components/Input/InputMoney';
Giang Tran committed
35
import I18n from '../../../helper/i18/i18n';
Giang Tran committed
36 37
import {getOTPApi} from '../../../apis/Functions/users';
import {OTPWALLET} from '../../../routers/ScreenNames';
Giang Tran committed
38
import {showAlert, TYPE} from '../../../components/DropdownAlert';
Giang Tran committed
39
var radio_props = [
40 41
  {label: I18n.t('FromWallet'), value: 'WALLET'},
  {label: I18n.t('FromCQGAccount'), value: 'INVESTMENT'},
Giang Tran committed
42 43 44
];
const {width} = Dimensions.get('window');

Giang Tran committed
45 46 47
const WalletWithdraw = (props) => {
  const [data, setData] = useState([]);
  const [src, setSrc] = useState('WALLET');
Giang Tran committed
48
  const [type, setType] = useState();
Giang Tran committed
49 50 51 52 53 54
  const [amount, setAmount] = useState();
  const [notes, setNotes] = useState('');

  const navigate = useNavigation();

  useEffect(() => {
Giang Tran committed
55 56 57 58 59 60
    const unsubscribe = navigate.addListener('focus', () => {
      getData();
    });

    return unsubscribe;
  }, [navigate]);
Giang Tran committed
61 62 63 64 65 66

  const getData = async () => {
    const res = await getListWidthDraw({
      platform: Platform.OS,
    });
    if (res.data.code == 200 && res.data.data) {
Giang Tran committed
67
      if (res.data.data.length == 0) {
Giang Tran committed
68 69
        confirmAlert(I18n.t('SettingPaymentMethodConfirm'), () =>
          navigate.navigate(ADDMETHODPAY),
Giang Tran committed
70 71 72 73 74 75 76
        );
      } else {
        const newList = res.data.data.map((e) => {
          return {...e, value: e.id, name: e.method};
        });
        setData(newList);
      }
Giang Tran committed
77
    } else {
Giang Tran committed
78
      showAlert(TYPE.ERROR, I18n.t('Notification'), I18n.t('Can_not_get_data'));
Giang Tran committed
79 80 81 82
    }
  };

  const onPressWithdraw = async () => {
Giang Tran committed
83 84 85 86
    const titles = [
      I18n.t('BeneficiaryAccount'),
      I18n.t('AmountOfMoney').toLowerCase(),
    ];
Giang Tran committed
87 88 89 90 91

    const index = checkFormatArray([type, amount]);

    if (index === true) {
      props.showLoading();
Giang Tran committed
92
      const res = await getOTPApi({
Giang Tran committed
93
        platform: Platform.OS,
Giang Tran committed
94
        otp_by: props.user.email,
Giang Tran committed
95 96 97
      });
      props.hideLoading();
      if (res.data.code == 200) {
Giang Tran committed
98 99 100 101 102 103 104
        navigate.navigate(OTPWALLET, {
          type: 'WITHDRAW',
          src,
          receiving_account: type.id,
          amount,
          notes,
        });
Giang Tran committed
105
      } else {
Giang Tran committed
106
        showAlert(TYPE.ERROR, I18n.t('Notification'), res.data.message);
Giang Tran committed
107
      }
Giang Tran committed
108 109

      props.hideLoading();
Giang Tran committed
110
    } else {
Giang Tran committed
111 112
      showAlert(
        TYPE.WARN,
Giang Tran committed
113 114 115
        I18n.t('Notification'),
        I18n.t('Please_fill_in') + titles[index],
      );
Giang Tran committed
116 117 118
    }
  };

Giang Tran committed
119
  return (
Giang Tran committed
120 121 122 123 124 125
    <KeyboardAvoidingView
      behavior={Platform.Os === 'ios' ? 'padding' : 'height'}
      style={{flex: 1}}
      keyboardVerticalOffset={-50}>
      <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
        <View style={{flex: 1}}>
Giang Tran committed
126
          <HeaderBack title={'Withdraw'} />
Giang Tran committed
127 128 129 130 131 132 133 134 135 136 137 138
          <ScrollView showsVerticalScrollIndicator={false}>
            <View style={styles.container}>
              <View style={styles.wrapTop}>
                <View style={styles.itemTop}>
                  <Text style={styles.txtTitle}>{I18n.t('Wallet')}</Text>
                  <Text style={styles.txtMoney}>
                    {' '}
                    {props.user.current_money != 0
                      ? toPriceVnd(props.user.current_money)
                      : 0}{' '}
                  </Text>
                </View>
Giang Tran committed
139

Giang Tran committed
140
                <View style={{width: 1, backgroundColor: '#DBDBDB'}} />
Giang Tran committed
141

Giang Tran committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                <View style={styles.itemTop}>
                  <Text style={styles.txtTitle}>CQG</Text>
                  <Text style={styles.txtMoney}>
                    {props.user.current_cqg_money != 0
                      ? toPriceVnd(props.user.current_cqg_money)
                      : 0}{' '}
                    đ
                  </Text>
                </View>
              </View>
              <View style={styles.wrapBody}>
                <Text
                  style={{
                    fontSize: getFontXD(42),
                    color: R.colors.color777,
                  }}>
                  {I18n.t('SourceAccount')}
Giang Tran committed
159
                </Text>
Giang Tran committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
                {/* <View style={styles.row}> */}
                <RadioForm
                  radio_props={radio_props}
                  labelStyle={{fontSize: getFontXD(42)}}
                  formHorizontal={true}
                  style={styles.row}
                  initial={0}
                  onPress={(value) => {
                    setSrc(value);
                  }}
                />
                {/* </View> */}
                <Text
                  style={{
                    fontSize: getFontXD(42),
                    color: R.colors.color777,
                  }}>
                  {I18n.t('ChooseBeneficiaryAccount')}
                </Text>
                <PickerItem
                  width={width - 20}
                  data={data}
                  onValueChange={(value, items) => {
                    setType(items);
                  }}
                />
                <TextMoney
                  onChangeText={(val) => setAmount(val)}
                  title={I18n.t('AmountOfMoney')}
                  value={amount}
                />
                <TextMulti
                  onChangeText={(val) => setNotes(val)}
                  title={I18n.t('Note')}
                />
Giang Tran committed
195 196
              </View>
            </View>
Giang Tran committed
197 198 199 200 201 202 203 204 205 206
          </ScrollView>
          <View
            style={{
              paddingVertical: 10,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <TouchableOpacity onPress={onPressWithdraw} style={styles.btnSend}>
              <Text style={styles.txtSend}>{I18n.t('Withdraw')}</Text>
            </TouchableOpacity>
Giang Tran committed
207
          </View>
Giang Tran committed
208
        </View>
Giang Tran committed
209 210
      </TouchableWithoutFeedback>
    </KeyboardAvoidingView>
Giang Tran committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  );
};

const styles = StyleSheet.create({
  container: {
    backgroundColor: R.colors.white,
    marginVertical: 10,
    shadowColor: '#AFA9A9',
    shadowOffset: {
      width: 0,
      height: 1,
    },
    shadowOpacity: 0.25,
    shadowRadius: 1.84,
    elevation: 1,
  },
  wrapTop: {
    flexDirection: 'row',
    borderBottomWidth: 1,
    borderBottomColor: '#DBDBDB',
  },
  itemTop: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    paddingVertical: 20,
  },
  wrapBody: {
    paddingHorizontal: 10,
    paddingVertical: 20,
  },
  txtMoney: {
    color: '#1C6AF6',
    fontSize: getFontXD(52),
  },
  txtTitle: {
    color: R.colors.black,
    fontSize: getFontXD(42),
  },
  txtSend: {
    fontSize: getFontXD(42),
    color: R.colors.white,
    fontWeight: 'bold',
  },
  btnSend: {
    width: 140,
    height: 40,
    backgroundColor: R.colors.main,
    borderRadius: 10,
    justifyContent: 'center',
    alignItems: 'center',
  },
  row: {
    height: HEIGHTXD(109),
    width: '100%',
    justifyContent: 'space-between',
    marginVertical: 5,
  },
});

Giang Tran committed
271 272 273 274 275 276 277 278 279
const mapStateToProps = (state) => {
  return {
    user: state.userReducer,
  };
};
export default connect(mapStateToProps, {
  showLoading,
  hideLoading,
})(WalletWithdraw);