Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { useEffect, useRef, useState } from 'react'
2614 stevensc 2
import { useNavigate, useLocation } from 'react-router-dom'
5 stevensc 3
import { useDispatch, useSelector } from 'react-redux'
4
 
1484 stevensc 5
import { axios } from '../../utils'
6
import { addNotification } from '../../redux/notification/notification.actions'
7
 
5 stevensc 8
import QuestionCard from '../../components/my-coach/QuestionCard'
9
import AnswerCard from '../../components/my-coach/AnswerCard'
10
import AnswerModal from '../../components/my-coach/AnswerModal'
535 stevensc 11
import ConfirmModal from '../../components/modals/ConfirmModal'
409 stevensc 12
import TitleSection from '../../components/UI/TitleSection'
1484 stevensc 13
import { Container, Grid } from '@mui/material'
5 stevensc 14
 
15
const MyCoachViewPage = () => {
16
  const [question, setQuestion] = useState({})
17
  const [answers, setAnswers] = useState([])
18
  const [modalShow, setModalShow] = useState(null)
19
  const [currentAnswer, setCurrentAnswer] = useState('')
20
  const addUrl = useRef('')
21
  const actionUrl = useRef('')
22
  const labels = useSelector(({ intl }) => intl.labels)
23
  const dispatch = useDispatch()
24
  const { pathname } = useLocation()
2614 stevensc 25
  const navigate = useNavigate()
5 stevensc 26
 
27
  const getQuestion = () => {
28
    axios
29
      .get(pathname, {
30
        headers: {
1484 stevensc 31
          'Content-Type': 'application/json'
32
        }
5 stevensc 33
      })
34
      .then((response) => {
35
        const { data, success } = response.data
36
 
37
        if (!success) {
38
          const errorMessage =
39
            typeof data === 'string'
40
              ? data
41
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
42
 
43
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
44
          return
45
        }
46
 
47
        addUrl.current = data.link_answers_add
48
        setQuestion(data)
49
      })
50
      .catch((error) => {
2194 stevensc 51
        dispatch(addNotification({ style: 'danger', msg: error.message }))
5 stevensc 52
      })
53
  }
54
 
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
 
71
        closeModal()
72
        dispatch(addNotification({ style: 'success', msg: data }))
2614 stevensc 73
        navigate('/my-coach')
5 stevensc 74
      })
75
      .catch((error) => {
2194 stevensc 76
        dispatch(addNotification({ style: 'danger', msg: error.message }))
5 stevensc 77
      })
78
  }
79
 
80
  const confirmDeleteAnswer = () => {
81
    axios
82
      .post(actionUrl.current)
83
      .then((response) => {
84
        const { data, success } = response.data
85
 
86
        if (!success) {
87
          const errorMessage =
88
            typeof data === 'string'
89
              ? data
90
              : 'Ha ocurrido un error, por favor intente más tarde.'
91
 
92
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
93
          return
94
        }
95
 
96
        setQuestion((prevQuestion) => ({
97
          ...prevQuestion,
98
          comments: data.total_comments,
99
          answers: data.total_answers,
1484 stevensc 100
          reactions: data.total_reactions
5 stevensc 101
        }))
102
        closeModal()
103
      })
104
      .catch((error) => {
2194 stevensc 105
        dispatch(addNotification({ style: 'danger', msg: error.message }))
5 stevensc 106
      })
107
  }
108
 
109
  const deleteQuestion = (url) => {
110
    actionUrl.current = url
111
    setModalShow('delete')
112
  }
113
 
114
  const editQuestion = (url) => {
115
    actionUrl.current = url
116
    setModalShow('edit')
117
  }
118
 
119
  const closeModal = () => {
120
    actionUrl.current = ''
121
    setModalShow(null)
122
    setCurrentAnswer('')
123
  }
124
 
125
  const addAnswer = () => {
126
    setModalShow('addAnswer')
127
  }
128
 
129
  const editAnswer = (url, text) => {
130
    setModalShow('editAnswer')
131
    actionUrl.current = url
132
    setCurrentAnswer(text)
133
  }
134
 
135
  const deleteAnswer = (url) => {
136
    setModalShow('deleteAnswer')
137
    actionUrl.current = url
138
  }
139
 
140
  const onAddAnswer = ({ answers, item }) => {
141
    setQuestion((prevQuestion) => ({ ...prevQuestion, answers }))
142
    setAnswers((prevAnswers) => [item, ...prevAnswers])
143
  }
144
 
145
  const onEditAnswer = ({ description }) => {
146
    const newAnswers = answers.map((answer) => {
147
      if (answer.link_edit === actionUrl.current) {
148
        return { ...answer, text: description }
149
      }
150
 
151
      return answer
152
    })
153
 
154
    setAnswers(newAnswers)
155
  }
156
 
157
  const updateTotalComments = (total) => {
158
    setQuestion({
159
      ...question,
1484 stevensc 160
      comments: total
5 stevensc 161
    })
162
  }
163
 
164
  const updateTotalReactions = (total) => {
165
    setQuestion({
166
      ...question,
1484 stevensc 167
      reactions: total
5 stevensc 168
    })
169
  }
170
 
171
  useEffect(() => {
172
    getQuestion()
173
  }, [])
174
 
175
  useEffect(() => {
176
    if (question.link_answers) {
177
      axios
178
        .get(question.link_answers)
179
        .then((response) => {
180
          const { data, success } = response.data
181
 
182
          if (!success) {
183
            const errorMessage =
184
              typeof data === 'string'
185
                ? data
186
                : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
187
 
188
            dispatch(addNotification({ style: 'danger', msg: errorMessage }))
189
            return
190
          }
191
 
192
          setAnswers(data.items)
193
        })
194
        .catch((error) => {
2194 stevensc 195
          dispatch(addNotification({ style: 'danger', msg: error.message }))
5 stevensc 196
        })
197
    }
198
  }, [question])
199
 
200
  return (
201
    <>
1484 stevensc 202
      <Container sx={{ px: 0 }}>
409 stevensc 203
        <TitleSection title={labels.my_coach} />
1484 stevensc 204
 
1485 stevensc 205
        <Grid container mt={1}>
1484 stevensc 206
          <Grid
207
            item
208
            xs={12}
209
            md={8}
210
            mx='auto'
211
            sx={{
212
              display: 'flex',
213
              flexDirection: 'column',
214
              gap: 1
215
            }}
216
          >
5 stevensc 217
            <QuestionCard
218
              key={question.uuid}
219
              onEdit={editQuestion}
220
              onDelete={deleteQuestion}
221
              onReply={addAnswer}
222
              {...question}
223
            />
1484 stevensc 224
 
5 stevensc 225
            {answers.map((answer) => (
226
              <AnswerCard
227
                key={answer.unique}
228
                {...answer}
229
                onEdit={editAnswer}
230
                onDelete={deleteAnswer}
231
                updateComments={updateTotalComments}
232
                updateReactions={updateTotalReactions}
233
              />
234
            ))}
1484 stevensc 235
          </Grid>
236
        </Grid>
5 stevensc 237
      </Container>
238
      <AnswerModal
239
        url={currentAnswer ? actionUrl.current : addUrl.current}
240
        show={['addAnswer', 'editAnswer'].includes(modalShow)}
241
        currentAnswer={currentAnswer}
242
        onClose={closeModal}
243
        onComplete={currentAnswer ? onEditAnswer : onAddAnswer}
244
      />
245
      <ConfirmModal
246
        show={modalShow === 'deleteAnswer'}
247
        onClose={closeModal}
248
        onAccept={confirmDeleteAnswer}
249
      />
250
      <ConfirmModal
251
        show={modalShow === 'delete'}
252
        onClose={closeModal}
253
        onAccept={confirmDelete}
254
      />
255
    </>
256
  )
257
}
258
 
259
export default MyCoachViewPage