Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
1929 stevensc 1
import React, { useEffect } from 'react'
1460 stevensc 2
import { useDispatch, useSelector } from 'react-redux'
5 stevensc 3
import { useForm } from 'react-hook-form'
4
import { CKEditor } from 'ckeditor4-react'
1460 stevensc 5
 
6
import { CKEDITOR_OPTIONS, axios } from 'utils/index'
5 stevensc 7
import { addNotification } from '../../redux/notification/notification.actions'
8
 
1460 stevensc 9
import Modal from 'components/UI/modal/Modal'
10
import FormErrorFeedback from 'components/UI/form/FormErrorFeedback'
5 stevensc 11
 
12
const AnswerModal = ({
13
  show = false,
14
  currentAnswer = '',
15
  url = '',
16
  onClose = () => null,
748 stevensc 17
  onComplete = () => null
5 stevensc 18
}) => {
19
  const labels = useSelector(({ intl }) => intl.labels)
20
  const dispatch = useDispatch()
21
 
22
  const { register, handleSubmit, setValue, errors } = useForm()
23
 
24
  const onSubmit = handleSubmit(({ description }) => {
1971 stevensc 25
    axios.post(url, { description }).then((response) => {
26
      const { data, success } = response.data
5 stevensc 27
 
1971 stevensc 28
      if (!success) {
29
        const errorMessage =
30
          typeof data === 'string'
31
            ? data
32
            : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
5 stevensc 33
 
1971 stevensc 34
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
35
        return
36
      }
5 stevensc 37
 
1971 stevensc 38
      onComplete(data)
39
      onClose()
40
    })
5 stevensc 41
  })
42
 
43
  useEffect(() => {
44
    register('description', { required: true })
45
  }, [])
46
 
47
  useEffect(() => {
48
    setValue('description', currentAnswer)
49
  }, [currentAnswer])
50
 
51
  return (
1460 stevensc 52
    <Modal
53
      title={
54
        currentAnswer ? labels.my_coach_answer_edit : labels.my_coach_answer_add
55
      }
56
      show={show}
57
      onClose={onClose}
58
      onAccept={onSubmit}
59
    >
60
      <CKEditor
61
        onChange={(e) => setValue('description', e.editor.getData())}
62
        onInstanceReady={(e) => e.editor.setData(currentAnswer)}
63
        config={CKEDITOR_OPTIONS}
64
      />
65
      {errors.description && (
66
        <FormErrorFeedback>{labels.error_field_empty}</FormErrorFeedback>
67
      )}
5 stevensc 68
    </Modal>
69
  )
70
}
71
 
72
export default AnswerModal