Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7226 | | 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'
7218 stevensc 7
import styled from 'styled-components'
8
 
7212 stevensc 9
import QuestionCard from '../../components/my-coach/QuestionCard'
10
import ConfirmModal from '../../components/modals/ConfirmModal'
7217 stevensc 11
import AnswerCard from '../../components/my-coach/AnswerCard'
7220 stevensc 12
import AnswerModal from '../../components/my-coach/AnswerModal'
7209 stevensc 13
 
7218 stevensc 14
const StyledSection = styled(Col)`
15
  display: flex;
16
  flex-direction: column;
17
  gap: 0.5rem;
18
  margin: auto;
19
`
20
 
7209 stevensc 21
const MyCoachViewPage = () => {
7212 stevensc 22
  const [question, setQuestion] = useState({})
7216 stevensc 23
  const [answers, setAnswers] = useState([])
7212 stevensc 24
  const [modalShow, setModalShow] = useState(null)
7221 stevensc 25
  const [currentAnswer, setCurrentAnswer] = useState('')
7212 stevensc 26
  const addUrl = useRef('')
27
  const actionUrl = useRef('')
28
  const labels = useSelector(({ intl }) => intl.labels)
7209 stevensc 29
  const dispatch = useDispatch()
30
  const { pathname } = useLocation()
7213 stevensc 31
  const history = useHistory()
7209 stevensc 32
 
33
  const getQuestion = () => {
34
    axios
7211 stevensc 35
      .get(pathname, {
7209 stevensc 36
        headers: {
37
          'Content-Type': 'application/json',
38
        },
39
      })
40
      .then((response) => {
41
        const { data, success } = response.data
42
 
43
        if (!success) {
44
          const errorMessage =
45
            typeof data === 'string'
46
              ? data
47
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
48
 
49
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
50
          return
51
        }
52
 
7212 stevensc 53
        addUrl.current = data.link_answers_add
54
        setQuestion(data)
7209 stevensc 55
      })
56
      .catch((error) => {
57
        dispatch(
58
          addNotification({
59
            style: 'danger',
60
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
61
          })
62
        )
63
        throw new Error(error)
64
      })
65
  }
66
 
7212 stevensc 67
  const confirmDelete = () => {
68
    axios
69
      .post(actionUrl.current)
70
      .then((response) => {
71
        const { data, success } = response.data
72
 
73
        if (!success) {
74
          const errorMessage =
75
            typeof data === 'string'
76
              ? data
77
              : 'Ha ocurrido un error, por favor intente más tarde.'
78
 
79
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
80
          return
81
        }
82
 
7213 stevensc 83
        closeModal()
7212 stevensc 84
        dispatch(addNotification({ style: 'success', msg: data }))
7214 stevensc 85
        history.replace('/my-coach')
7212 stevensc 86
      })
87
      .catch((error) => {
88
        dispatch(
89
          addNotification({
90
            style: 'danger',
91
            msg: 'Ha ocurrido un error, por favor intente más tarde.',
92
          })
93
        )
94
        throw new Error(error)
95
      })
96
  }
97
 
7221 stevensc 98
  const confirmDeleteAnswer = () => {
99
    axios
100
      .post(actionUrl.current)
101
      .then((response) => {
102
        const { data, success } = response.data
103
 
104
        if (!success) {
105
          const errorMessage =
106
            typeof data === 'string'
107
              ? data
108
              : 'Ha ocurrido un error, por favor intente más tarde.'
109
 
110
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
111
          return
112
        }
113
 
114
        setQuestion((prevQuestion) => ({
115
          ...prevQuestion,
116
          comments: data.total_comments,
117
          answers: data.total_answers,
118
          reactions: data.total_reactions,
119
        }))
120
        closeModal()
121
      })
122
      .catch((error) => {
123
        dispatch(
124
          addNotification({
125
            style: 'danger',
126
            msg: 'Ha ocurrido un error, por favor intente más tarde.',
127
          })
128
        )
129
        throw new Error(error)
130
      })
131
  }
132
 
7212 stevensc 133
  const deleteQuestion = (url) => {
134
    actionUrl.current = url
135
    setModalShow('delete')
136
  }
137
 
138
  const editQuestion = (url) => {
139
    actionUrl.current = url
140
    setModalShow('edit')
141
  }
142
 
143
  const closeModal = () => {
144
    actionUrl.current = ''
145
    setModalShow(null)
7221 stevensc 146
    setCurrentAnswer('')
7212 stevensc 147
  }
148
 
7220 stevensc 149
  const addAnswer = () => {
150
    setModalShow('addAnswer')
151
  }
152
 
7221 stevensc 153
  const editAnswer = (url, text) => {
154
    setModalShow('editAnswer')
155
    actionUrl.current = url
156
    setCurrentAnswer(text)
157
  }
158
 
159
  const deleteAnswer = (url) => {
160
    setModalShow('deleteAnswer')
161
    actionUrl.current = url
162
  }
163
 
7220 stevensc 164
  const onAddAnswer = ({ answers, item }) => {
165
    setQuestion((prevQuestion) => ({ ...prevQuestion, answers }))
166
    setAnswers((prevAnswers) => [item, ...prevAnswers])
167
  }
168
 
7221 stevensc 169
  const onEditAnswer = ({ description }) => {
170
    const newAnswers = answers.map((answer) => {
171
      if (answer.link_edit === actionUrl.current) {
172
        return { ...answer, text: description }
173
      }
174
 
175
      return answer
176
    })
177
 
178
    setAnswers(newAnswers)
179
  }
180
 
7226 stevensc 181
  const updateTotalComments = (total) => {
182
    setQuestion({
183
      ...question,
184
      comments: total,
185
    })
186
  }
187
 
7231 stevensc 188
  const updateTotalReactions = (total) => {
189
    setQuestion({
190
      ...question,
191
      reactions: total,
192
    })
193
  }
194
 
7209 stevensc 195
  useEffect(() => {
196
    getQuestion()
197
  }, [])
198
 
7216 stevensc 199
  useEffect(() => {
200
    if (question.link_answers) {
201
      axios
202
        .get(question.link_answers)
203
        .then((response) => {
204
          const { data, success } = response.data
205
 
206
          if (!success) {
207
            const errorMessage =
208
              typeof data === 'string'
209
                ? data
210
                : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
211
 
212
            dispatch(addNotification({ style: 'danger', msg: errorMessage }))
213
            return
214
          }
215
 
7217 stevensc 216
          setAnswers(data.items)
7216 stevensc 217
        })
218
        .catch((error) => {
219
          dispatch(
220
            addNotification({
221
              style: 'danger',
222
              msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
223
            })
224
          )
225
          throw new Error(error)
226
        })
227
    }
228
  }, [question])
229
 
7212 stevensc 230
  return (
231
    <>
232
      <Container as="section" className="companies-info">
233
        <div className="company-title">
234
          <h1 className="title mx-auto">{labels.my_coach}</h1>
235
        </div>
236
        <Row>
7218 stevensc 237
          <StyledSection md="8">
7212 stevensc 238
            <QuestionCard
239
              key={question.uuid}
240
              onEdit={editQuestion}
241
              onDelete={deleteQuestion}
7220 stevensc 242
              onReply={addAnswer}
7212 stevensc 243
              {...question}
244
            />
7217 stevensc 245
            {answers.map((answer) => (
7221 stevensc 246
              <AnswerCard
247
                key={answer.unique}
248
                {...answer}
249
                onEdit={editAnswer}
250
                onDelete={deleteAnswer}
7226 stevensc 251
                updateComments={updateTotalComments}
7231 stevensc 252
                updateReactions={updateTotalReactions}
7221 stevensc 253
              />
7217 stevensc 254
            ))}
7218 stevensc 255
          </StyledSection>
7212 stevensc 256
        </Row>
257
      </Container>
7220 stevensc 258
      <AnswerModal
7224 stevensc 259
        url={currentAnswer ? actionUrl.current : addUrl.current}
7221 stevensc 260
        show={['addAnswer', 'editAnswer'].includes(modalShow)}
261
        currentAnswer={currentAnswer}
7220 stevensc 262
        onClose={closeModal}
7221 stevensc 263
        onComplete={currentAnswer ? onEditAnswer : onAddAnswer}
7220 stevensc 264
      />
7212 stevensc 265
      <ConfirmModal
7221 stevensc 266
        show={modalShow === 'deleteAnswer'}
267
        onClose={closeModal}
268
        onAccept={confirmDeleteAnswer}
269
      />
270
      <ConfirmModal
7212 stevensc 271
        show={modalShow === 'delete'}
272
        onClose={closeModal}
273
        onAccept={confirmDelete}
274
      />
275
    </>
276
  )
7209 stevensc 277
}
278
 
279
export default MyCoachViewPage