Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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