Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3452 stevensc 1
import React from 'react'
2
import { useDispatch, useSelector } from 'react-redux'
3
import { useForm } from 'react-hook-form'
4
 
5
import { useFetch } from '@hooks'
6
import { axios } from '@app/utils'
7
import { addNotification } from '@app/redux/notification/notification.actions'
8
 
9
import Modal from '@components/UI/modal/Modal'
10
import Input from '@components/UI/inputs/Input'
11
import Select from '@components/UI/inputs/Select'
12
import Ckeditor from '@components/common/ckeditor/Ckeditor'
13
 
14
const KnowledgeEditModal = ({
15
  show,
16
  categories = [],
17
  url,
18
  onComplete,
19
  onClose
20
}) => {
21
  const { data: formValues } = useFetch(url)
22
  const labels = useSelector(({ intl }) => intl.labels)
23
  const dispatch = useDispatch()
24
 
25
  const {
26
    control,
27
    formState: { errors },
28
    handleSubmit,
29
    reset
30
  } = useForm({
31
    defaultValues: {
32
      category_id: '',
33
      title: '',
34
      description: '',
35
      image: '',
36
      attachment: ''
37
    },
38
    values: formValues
39
  })
40
 
41
  const onSubmit = handleSubmit(async (data) => {
42
    console.log(data)
43
    const { image, title, attachment, description, category_id } = data
44
    try {
45
      const formData = new FormData()
46
      formData.append('image', image[0])
47
      formData.append('title', title)
48
      formData.append('attachment', attachment[0])
49
      formData.append('description', description)
50
      formData.append('category_id', category_id)
51
 
52
      const response = await axios.post(url, formData)
53
 
54
      const { success } = response.data
55
 
56
      if (!success) throw new Error('No se pudo guardar el conocimiento')
57
      onComplete()
58
      reset()
59
      onClose()
60
    } catch (error) {
61
      dispatch(addNotification({ style: 'danger', msg: error.message }))
62
    }
63
  })
64
 
65
  return (
66
    <Modal
67
      title='Editar Conocimiento'
68
      show={show}
69
      onClose={onClose}
70
      onAccept={onSubmit}
71
    >
72
      <Select
73
        name='category_id'
74
        label='Categoría'
75
        placeholder='Seleccione un opción'
76
        options={categories}
77
        control={control}
78
        rules={{ required: 'Este campo es requerido' }}
79
        error={errors.category_id?.message}
80
      />
81
 
82
      <Input
83
        name='title'
84
        label={labels.title}
85
        placeholder='Ingrese el título del conocimiento'
86
        control={control}
87
        rules={{ required: 'Este campo es requerido' }}
88
        error={errors.title?.message}
89
      />
90
 
91
      <Ckeditor
92
        name='description'
93
        control={control}
94
        rules={{ required: 'Este campo es requerido' }}
95
        error={errors.description?.message}
96
      />
97
 
98
      <Input
99
        name='image'
100
        type='file'
101
        label={labels.image}
102
        accept='image/*'
103
        placeholder='Seleccione una imagen'
104
        control={control}
105
        rules={{ required: 'Este campo es requerido' }}
106
        error={errors.image?.message}
107
      />
108
 
109
      <Input
110
        name='attachment'
111
        type='file'
112
        label={labels.attachment}
113
        control={control}
114
        placeholder='Seleccione un archivo'
115
        accept='application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document'
116
        rules={{ required: 'Este campo es requerido' }}
117
        error={errors.attachment?.message}
118
      />
119
    </Modal>
120
  )
121
}
122
 
123
export default KnowledgeEditModal