Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3697 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useMemo, useState } from 'react';
2
import { useDispatch } from 'react-redux';
3
import { useForm } from 'react-hook-form';
4
 
5
import { axios } from '@utils';
6
import { useFetch } from '@hooks';
7
import { addNotification } from '@app/redux/notification/notification.actions';
8
 
9
import Table from '@components/table/Table';
10
import Modal from '@components/UI/modal/Modal';
11
import Input from '@components/UI/inputs/Input';
12
import SelectField from '@components/UI/inputs/Select';
13
 
14
const TransactionTableColumns = [
15
  {
16
    field: 'type',
17
    headerName: 'Tipo'
18
  },
19
  {
20
    field: 'amount',
21
    headerName: 'Monto'
22
  },
23
  {
24
    field: 'status',
25
    headerName: 'Estado'
26
  },
27
  {
28
    field: 'description',
29
    headerName: 'Descripción'
30
  },
31
  {
32
    field: 'updated_on',
33
    headerName: 'Fecha'
34
  },
35
  {
36
    field: 'provider',
37
    headerName: 'Proveedor'
38
  }
39
];
40
 
41
const Transactions = () => {
42
  const { data: transactions } = useFetch('/account-settings/transactions');
43
  const {
44
    data: { balance, amounts }
45
  } = useFetch('/account-settings');
46
  const [showModal, setShowModal] = useState(false);
47
 
48
  return (
49
    <>
50
      <div className='acc-setting'>
51
        <h3>Transacciones</h3>
52
 
53
        <div className='cp-field'>
54
          <h5 id='location-formatted-address'>Balance (USD)</h5>
55
 
56
          <div className='cpp-fiel'>
57
            <input type='text' readOnly value={balance} />
58
          </div>
59
 
60
          <div className='cpp-fiel'>
61
            <input
62
              type='button'
63
              id='btn-add-fund'
64
              value='Agregar fondos'
65
              onClick={() => setShowModal(true)}
66
            />
67
          </div>
68
        </div>
69
 
70
        <div className='cp-field'>
71
          <Table columns={TransactionTableColumns} rows={transactions.items} />
72
        </div>
73
      </div>
74
      <TransactionModal show={showModal} amounts={amounts} onClose={() => setShowModal(false)} />
75
    </>
76
  );
77
};
78
 
79
const TransactionModal = ({ show, onClose, amounts = {} }) => {
80
  const [loading, setLoading] = useState(false);
81
  const dispatch = useDispatch();
82
 
83
  const {
84
    control,
85
    handleSubmit,
86
    formState: { errors }
87
  } = useForm();
88
 
89
  const amountsValues = useMemo(
90
    () =>
91
      Object.entries(amounts).map(([key, value]) => ({
92
        label: key,
93
        value
94
      })),
95
    [amounts]
96
  );
97
 
98
  const onSubmit = handleSubmit(async (sendData) => {
99
    setLoading(true);
100
    const formData = new FormData();
101
 
102
    Object.entries(sendData).forEach(([key, value]) => formData.append(key, value));
103
 
104
    const response = await axios.post('/account-settings/transactions/add-funds', formData);
105
    const { success, data } = response.data;
106
 
107
    if (!success) {
108
      const errorMessage =
109
        typeof data === 'string' ? data : 'Ha ocurrido un error, Por favor intente mas tarde';
110
      dispatch(
111
        addNotification({
112
          style: 'danger',
113
          msg: errorMessage
114
        })
115
      );
116
    }
117
    const paypalRoute = data;
118
    window.location.replace(paypalRoute);
119
    setLoading(false);
120
  });
121
 
122
  return (
123
    <Modal
124
      show={show}
125
      title='Agregar Fondos'
126
      onClose={onClose}
127
      onReject={onClose}
128
      onAccept={onSubmit}
129
      loading={loading}
130
    >
131
      <Input
132
        control={control}
133
        type='text'
134
        name='description'
135
        placeholder='Descripción'
136
        error={errors.description?.message}
137
        rules={{
138
          required: 'Por favor ingrese una descripción'
139
        }}
140
      />
141
 
142
      <SelectField
143
        name='amount'
144
        label='Monto'
145
        control={control}
146
        error={errors.amount?.message}
147
        rules={{ required: 'Por favor eliga un monto' }}
148
        options={amountsValues}
149
        defaultValue=''
150
      />
151
    </Modal>
152
  );
153
};
154
 
155
export default Transactions;