Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7213 | Rev 7216 | 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({})
12
  const [modalShow, setModalShow] = useState(null)
13
  const addUrl = useRef('')
14
  const actionUrl = useRef('')
15
  const labels = useSelector(({ intl }) => intl.labels)
7209 stevensc 16
  const dispatch = useDispatch()
17
  const { pathname } = useLocation()
7213 stevensc 18
  const history = useHistory()
7209 stevensc 19
 
20
  const getQuestion = () => {
21
    axios
7211 stevensc 22
      .get(pathname, {
7209 stevensc 23
        headers: {
24
          'Content-Type': 'application/json',
25
        },
26
      })
27
      .then((response) => {
28
        const { data, success } = response.data
29
 
30
        if (!success) {
31
          const errorMessage =
32
            typeof data === 'string'
33
              ? data
34
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
35
 
36
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
37
          return
38
        }
39
 
7212 stevensc 40
        addUrl.current = data.link_answers_add
41
        setQuestion(data)
7209 stevensc 42
      })
43
      .catch((error) => {
44
        dispatch(
45
          addNotification({
46
            style: 'danger',
47
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
48
          })
49
        )
50
        throw new Error(error)
51
      })
52
  }
53
 
7212 stevensc 54
  const confirmDelete = () => {
55
    axios
56
      .post(actionUrl.current)
57
      .then((response) => {
58
        const { data, success } = response.data
59
 
60
        if (!success) {
61
          const errorMessage =
62
            typeof data === 'string'
63
              ? data
64
              : 'Ha ocurrido un error, por favor intente más tarde.'
65
 
66
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
67
          return
68
        }
69
 
7213 stevensc 70
        closeModal()
7212 stevensc 71
        dispatch(addNotification({ style: 'success', msg: data }))
7214 stevensc 72
        history.replace('/my-coach')
7212 stevensc 73
      })
74
      .catch((error) => {
75
        dispatch(
76
          addNotification({
77
            style: 'danger',
78
            msg: 'Ha ocurrido un error, por favor intente más tarde.',
79
          })
80
        )
81
        throw new Error(error)
82
      })
83
  }
84
 
85
  const deleteQuestion = (url) => {
86
    actionUrl.current = url
87
    setModalShow('delete')
88
  }
89
 
90
  const editQuestion = (url) => {
91
    actionUrl.current = url
92
    setModalShow('edit')
93
  }
94
 
95
  const closeModal = () => {
96
    actionUrl.current = ''
97
    setModalShow(null)
98
  }
99
 
7209 stevensc 100
  useEffect(() => {
101
    getQuestion()
102
  }, [])
103
 
7212 stevensc 104
  return (
105
    <>
106
      <Container as="section" className="companies-info">
107
        <div className="company-title">
108
          <h1 className="title mx-auto">{labels.my_coach}</h1>
109
        </div>
110
        <Row>
111
          <Col md="8" className="mx-auto">
112
            <QuestionCard
113
              key={question.uuid}
114
              onEdit={editQuestion}
115
              onDelete={deleteQuestion}
116
              {...question}
117
            />
118
          </Col>
119
        </Row>
120
      </Container>
121
      <ConfirmModal
122
        show={modalShow === 'delete'}
123
        onClose={closeModal}
124
        onAccept={confirmDelete}
125
      />
126
    </>
127
  )
7209 stevensc 128
}
129
 
130
export default MyCoachViewPage