Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2802 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2781 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/form/SelectField'
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
75
        show={showModal}
76
        amounts={amounts}
77
        onClose={() => setShowModal(false)}
78
      />
79
    </>
80
  )
81
}
82
 
83
const TransactionModal = ({ show, onClose, amounts = {} }) => {
84
  const [loading, setLoading] = useState(false)
85
  const dispatch = useDispatch()
86
 
87
  const { control, errors, handleSubmit } = useForm()
88
 
89
  const amountsValues = useMemo(
90
    () =>
91
      Object.entries(amounts).map(([key, value]) => ({
92
        name: 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]) =>
103
      formData.append(key, value)
104
    )
105
 
106
    const { data: response } = await axios.post(
107
      '/account-settings/transactions/add-funds',
108
      formData
109
    )
110
    const { success, data } = response
111
 
112
    if (!success) {
113
      const errorMessage =
114
        typeof data === 'string'
115
          ? data
116
          : 'Ha ocurrido un error, Por favor intente mas tarde'
117
      dispatch(
118
        addNotification({
119
          style: 'danger',
120
          msg: errorMessage
121
        })
122
      )
123
    }
124
    const paypalRoute = data
125
    window.location.replace(paypalRoute)
126
    setLoading(false)
127
  })
128
 
129
  return (
130
    <Modal
131
      show={show}
132
      title='Agregar Fondos'
133
      onClose={onClose}
134
      onReject={onClose}
135
      onAccept={onSubmit}
136
      loading={loading}
137
    >
138
      <Input
139
        control={control}
140
        type='text'
141
        name='description'
142
        placeholder='Descripción'
143
        error={errors.description?.message}
144
        rules={{
145
          required: 'Por favor ingrese una descripción'
146
        }}
147
      />
148
 
149
      <SelectField
150
        name='amount'
151
        label='Monto'
152
        control={control}
153
        error={errors.amount?.message}
154
        rules={{
155
          required: 'Por favor eliga un monto'
156
        }}
157
        options={amountsValues}
158
        defaultValue=''
159
      />
160
    </Modal>
161
  )
162
}
163
 
164
export default Transactions