Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
7212 stevensc 1
import React, { useEffect, useRef, useState } from 'react'
7209 stevensc 2
import { axios } from '../../utils'
7213 stevensc 3
import { useHistory, useLocation } from 'react-router-dom'
7209 stevensc 4
import { addNotification } from '../../redux/notification/notification.actions'
7212 stevensc 5
import { useDispatch, useSelector } from 'react-redux'
6
import { Col, Container, Row } from 'react-bootstrap'
7
import QuestionCard from '../../components/my-coach/QuestionCard'
8
import ConfirmModal from '../../components/modals/ConfirmModal'
7209 stevensc 9
 
10
const MyCoachViewPage = () => {
7212 stevensc 11
  const [question, setQuestion] = useState({})
7216 stevensc 12
  const [answers, setAnswers] = useState([])
7212 stevensc 13
  const [modalShow, setModalShow] = useState(null)
14
  const addUrl = useRef('')
15
  const actionUrl = useRef('')
16
  const labels = useSelector(({ intl }) => intl.labels)
7209 stevensc 17
  const dispatch = useDispatch()
18
  const { pathname } = useLocation()
7213 stevensc 19
  const history = useHistory()
7209 stevensc 20
 
21
  const getQuestion = () => {
22
    axios
7211 stevensc 23
      .get(pathname, {
7209 stevensc 24
        headers: {
25
          'Content-Type': 'application/json',
26
        },
27
      })
28
      .then((response) => {
29
        const { data, success } = response.data
30
 
31
        if (!success) {
32
          const errorMessage =
33
            typeof data === 'string'
34
              ? data
35
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
36
 
37
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
38
          return
39
        }
40
 
7212 stevensc 41
        addUrl.current = data.link_answers_add
42
        setQuestion(data)
7209 stevensc 43
      })
44
      .catch((error) => {
45
        dispatch(
46
          addNotification({
47
            style: 'danger',
48
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
49
          })
50
        )
51
        throw new Error(error)
52
      })
53
  }
54
 
7212 stevensc 55
  const confirmDelete = () => {
56
    axios
57
      .post(actionUrl.current)
58
      .then((response) => {
59
        const { data, success } = response.data
60
 
61
        if (!success) {
62
          const errorMessage =
63
            typeof data === 'string'
64
              ? data
65
              : 'Ha ocurrido un error, por favor intente más tarde.'
66
 
67
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
68
          return
69
        }
70
 
7213 stevensc 71
        closeModal()
7212 stevensc 72
        dispatch(addNotification({ style: 'success', msg: data }))
7214 stevensc 73
        history.replace('/my-coach')
7212 stevensc 74
      })
75
      .catch((error) => {
76
        dispatch(
77
          addNotification({
78
            style: 'danger',
79
            msg: 'Ha ocurrido un error, por favor intente más tarde.',
80
          })
81
        )
82
        throw new Error(error)
83
      })
84
  }
85
 
86
  const deleteQuestion = (url) => {
87
    actionUrl.current = url
88
    setModalShow('delete')
89
  }
90
 
91
  const editQuestion = (url) => {
92
    actionUrl.current = url
93
    setModalShow('edit')
94
  }
95
 
96
  const closeModal = () => {
97
    actionUrl.current = ''
98
    setModalShow(null)
99
  }
100
 
7209 stevensc 101
  useEffect(() => {
102
    getQuestion()
103
  }, [])
104
 
7216 stevensc 105
  useEffect(() => {
106
    if (question.link_answers) {
107
      axios
108
        .get(question.link_answers)
109
        .then((response) => {
110
          const { data, success } = response.data
111
 
112
          if (!success) {
113
            const errorMessage =
114
              typeof data === 'string'
115
                ? data
116
                : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
117
 
118
            dispatch(addNotification({ style: 'danger', msg: errorMessage }))
119
            return
120
          }
121
 
122
          setAnswers(data)
123
        })
124
        .catch((error) => {
125
          dispatch(
126
            addNotification({
127
              style: 'danger',
128
              msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
129
            })
130
          )
131
          throw new Error(error)
132
        })
133
    }
134
  }, [question])
135
 
7212 stevensc 136
  return (
137
    <>
138
      <Container as="section" className="companies-info">
139
        <div className="company-title">
140
          <h1 className="title mx-auto">{labels.my_coach}</h1>
141
        </div>
142
        <Row>
143
          <Col md="8" className="mx-auto">
144
            <QuestionCard
145
              key={question.uuid}
146
              onEdit={editQuestion}
147
              onDelete={deleteQuestion}
148
              {...question}
149
            />
150
          </Col>
151
        </Row>
152
      </Container>
153
      <ConfirmModal
154
        show={modalShow === 'delete'}
155
        onClose={closeModal}
156
        onAccept={confirmDelete}
157
      />
158
    </>
159
  )
7209 stevensc 160
}
161
 
162
export default MyCoachViewPage