Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2802 | Rev 3432 | Ir a la última revisión | | Comparar con el anterior | 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'
2865 stevensc 12
import SelectField from '@components/UI/inputs/Select'
2781 stevensc 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
 
2802 stevensc 87
  const {
88
    control,
89
    handleSubmit,
90
    formState: { errors }
91
  } = useForm()
2781 stevensc 92
 
93
  const amountsValues = useMemo(
94
    () =>
95
      Object.entries(amounts).map(([key, value]) => ({
96
        name: key,
97
        value
98
      })),
99
    [amounts]
100
  )
101
 
102
  const onSubmit = handleSubmit(async (sendData) => {
103
    setLoading(true)
104
    const formData = new FormData()
105
 
106
    Object.entries(sendData).forEach(([key, value]) =>
107
      formData.append(key, value)
108
    )
109
 
110
    const { data: response } = await axios.post(
111
      '/account-settings/transactions/add-funds',
112
      formData
113
    )
114
    const { success, data } = response
115
 
116
    if (!success) {
117
      const errorMessage =
118
        typeof data === 'string'
119
          ? data
120
          : 'Ha ocurrido un error, Por favor intente mas tarde'
121
      dispatch(
122
        addNotification({
123
          style: 'danger',
124
          msg: errorMessage
125
        })
126
      )
127
    }
128
    const paypalRoute = data
129
    window.location.replace(paypalRoute)
130
    setLoading(false)
131
  })
132
 
133
  return (
134
    <Modal
135
      show={show}
136
      title='Agregar Fondos'
137
      onClose={onClose}
138
      onReject={onClose}
139
      onAccept={onSubmit}
140
      loading={loading}
141
    >
142
      <Input
143
        control={control}
144
        type='text'
145
        name='description'
146
        placeholder='Descripción'
147
        error={errors.description?.message}
148
        rules={{
149
          required: 'Por favor ingrese una descripción'
150
        }}
151
      />
152
 
153
      <SelectField
154
        name='amount'
155
        label='Monto'
156
        control={control}
157
        error={errors.amount?.message}
2865 stevensc 158
        rules={{ required: 'Por favor eliga un monto' }}
2781 stevensc 159
        options={amountsValues}
160
        defaultValue=''
161
      />
162
    </Modal>
163
  )
164
}
165
 
166
export default Transactions