Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7056 | Rev 7058 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7046 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { useForm } from 'react-hook-form'
7033 stevensc 3
import { CKEditor } from 'ckeditor4-react'
4
import { Button, Form, Modal } from 'react-bootstrap'
5
import { useDispatch, useSelector } from 'react-redux'
6
import { CKEDITOR_OPTIONS, axios } from '../../utils'
7
import { addNotification } from '../../redux/notification/notification.actions'
8
 
7046 stevensc 9
import FormErrorFeedback from '../UI/FormErrorFeedback'
10
import Spinner from '../UI/Spinner'
11
 
7033 stevensc 12
const KnowledgeEditModal = ({
13
  show,
14
  categories = [],
15
  url,
16
  onComplete,
17
  onClose,
7045 stevensc 18
  isEdit,
7033 stevensc 19
}) => {
20
  const [loading, setLoading] = useState(false)
21
  const labels = useSelector(({ intl }) => intl.labels)
7055 stevensc 22
  const [initialData, setInitialData] = useState('')
7033 stevensc 23
  const dispatch = useDispatch()
24
 
7055 stevensc 25
  const { handleSubmit, register, setValue, errors, getValues } = useForm()
7033 stevensc 26
 
27
  const onSubmit = handleSubmit((data) => {
28
    setLoading(true)
29
    const formData = new FormData()
7046 stevensc 30
    Object.entries(data).map(([key, value]) => {
7053 stevensc 31
      if (['attachment', 'image'].includes(key) && value) {
7046 stevensc 32
        formData.append(key, value[0])
33
        return
34
      }
35
      formData.append(key, value)
36
    })
7033 stevensc 37
 
38
    axios
39
      .post(url, formData)
40
      .then((response) => {
41
        const { data, success } = response.data
42
        if (!success) {
43
          if (typeof data === 'string') {
44
            dispatch(addNotification({ style: 'danger', msg: data }))
45
            return
46
          }
47
 
48
          Object.entries(data).map(([key, value]) => {
49
            key in getValues() &&
50
              dispatch(
51
                addNotification({
52
                  style: 'danger',
53
                  msg: `${key}: ${Array.isArray(value) ? value[0] : value}`,
54
                })
55
              )
56
          })
57
          return
58
        }
59
 
60
        onComplete()
61
        onClose()
62
      })
63
      .finally(() => setLoading(false))
64
  })
65
 
66
  useEffect(() => {
67
    register('description', { required: true })
68
  }, [])
69
 
70
  useEffect(() => {
7045 stevensc 71
    if (!url || !show || !isEdit) return
7038 stevensc 72
 
7033 stevensc 73
    axios.get(url).then((response) => {
7038 stevensc 74
      const { data, success } = response.data
7033 stevensc 75
 
76
      if (!success) {
77
        const errorMessage =
78
          typeof data === 'string' ? data : 'Ha ocurrido un error'
79
 
7037 stevensc 80
        dispatch(
81
          addNotification({
82
            style: 'danger',
83
            msg: errorMessage,
84
          })
85
        )
86
        return
7033 stevensc 87
      }
88
 
7055 stevensc 89
      setValue('title', data.title)
7033 stevensc 90
      setValue('category_id', data.category_id)
91
      setValue('description', data.description)
7055 stevensc 92
      setInitialData(data.description)
7033 stevensc 93
    })
7057 stevensc 94
  }, [url, show, isEdit])
7044 stevensc 95
 
96
  useEffect(() => {
97
    if (!show) {
98
      setValue('category_id', '')
99
      setValue('description', '')
100
      setValue('title', '')
101
    }
7033 stevensc 102
  }, [show])
103
 
104
  return (
105
    <Modal show={show}>
7037 stevensc 106
      <Modal.Header className="pb-0">
7054 stevensc 107
        <Modal.Title>
108
          {isEdit ? labels.edit : labels.add} conocimiento
109
        </Modal.Title>
7033 stevensc 110
      </Modal.Header>
111
      <Modal.Body>
7056 stevensc 112
        {loading ? (
113
          <Spinner />
114
        ) : (
115
          <Form onSubmit={onSubmit}>
116
            <Form.Group>
117
              <Form.Label>{labels.category}</Form.Label>
118
              <Form.Control
119
                as="select"
120
                ref={register({ required: true })}
121
                name="category_id"
122
              >
123
                {categories.map(({ name, uuid }) => (
124
                  <option value={uuid} key={uuid}>
125
                    {name}
126
                  </option>
127
                ))}
128
              </Form.Control>
129
              {errors.category_id && (
130
                <FormErrorFeedback>
131
                  {labels.error_field_empty}
132
                </FormErrorFeedback>
133
              )}
134
            </Form.Group>
7033 stevensc 135
 
7056 stevensc 136
            <Form.Group>
137
              <Form.Label>{labels.title}</Form.Label>
138
              <Form.Control
139
                type="text"
140
                name="title"
141
                ref={register({ required: true })}
142
              />
143
              {errors.title && (
144
                <FormErrorFeedback>
145
                  {labels.error_field_empty}
146
                </FormErrorFeedback>
147
              )}
148
            </Form.Group>
149
 
150
            <CKEditor
151
              onChange={(e) => setValue('description', e.editor.getData())}
152
              initData={initialData}
153
              config={CKEDITOR_OPTIONS}
7038 stevensc 154
            />
7056 stevensc 155
 
156
            {errors.description && (
7033 stevensc 157
              <FormErrorFeedback>{labels.error_field_empty}</FormErrorFeedback>
158
            )}
159
 
7056 stevensc 160
            <Form.Group>
161
              <Form.File
162
                label={labels.image}
163
                name="image"
164
                ref={register({ required: true })}
165
              />
166
              {errors.image && (
167
                <FormErrorFeedback>
168
                  {labels.error_field_empty}
169
                </FormErrorFeedback>
170
              )}
171
            </Form.Group>
7055 stevensc 172
 
7056 stevensc 173
            <Form.Group>
174
              <Form.File
175
                label={labels.attachment}
176
                name="attachment"
177
                ref={register}
178
              />
179
            </Form.Group>
7033 stevensc 180
 
7056 stevensc 181
            <Button className="mt-3 mr-2" variant="primary" type="submit">
182
              {labels.accept}
183
            </Button>
184
            <Button className="btn-secondary mt-3" onClick={onClose}>
185
              {labels.cancel}
186
            </Button>
187
          </Form>
188
        )}
7033 stevensc 189
      </Modal.Body>
190
    </Modal>
191
  )
192
}
193
 
194
export default KnowledgeEditModal