Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3000 | 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'
2806 stevensc 4
import { Grid } from '@mui/material'
5 stevensc 5
 
1484 stevensc 6
import { axios } from '../../utils'
7
import { addNotification } from '../../redux/notification/notification.actions'
8
 
5 stevensc 9
import QuestionCard from '../../components/my-coach/QuestionCard'
10
import AnswerCard from '../../components/my-coach/AnswerCard'
11
import AnswerModal from '../../components/my-coach/AnswerModal'
535 stevensc 12
import ConfirmModal from '../../components/modals/ConfirmModal'
3000 stevensc 13
import GoBackLayout from '@layouts/go-back'
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 (
3000 stevensc 201
    <GoBackLayout title={labels.my_coach}>
3001 stevensc 202
      <Grid container>
2806 stevensc 203
        <Grid
204
          item
205
          xs={12}
206
          md={8}
207
          mx='auto'
208
          sx={{
209
            display: 'flex',
210
            flexDirection: 'column',
211
            gap: 1
212
          }}
213
        >
214
          <QuestionCard
215
            key={question.uuid}
216
            onEdit={editQuestion}
217
            onDelete={deleteQuestion}
218
            onReply={addAnswer}
219
            {...question}
220
          />
221
 
222
          {answers.map((answer) => (
223
            <AnswerCard
224
              key={answer.unique}
225
              {...answer}
226
              onEdit={editAnswer}
227
              onDelete={deleteAnswer}
228
              updateComments={updateTotalComments}
229
              updateReactions={updateTotalReactions}
5 stevensc 230
            />
2806 stevensc 231
          ))}
232
        </Grid>
233
      </Grid>
1484 stevensc 234
 
5 stevensc 235
      <AnswerModal
236
        url={currentAnswer ? actionUrl.current : addUrl.current}
237
        show={['addAnswer', 'editAnswer'].includes(modalShow)}
238
        currentAnswer={currentAnswer}
239
        onClose={closeModal}
240
        onComplete={currentAnswer ? onEditAnswer : onAddAnswer}
241
      />
242
      <ConfirmModal
243
        show={modalShow === 'deleteAnswer'}
244
        onClose={closeModal}
245
        onAccept={confirmDeleteAnswer}
246
      />
247
      <ConfirmModal
248
        show={modalShow === 'delete'}
249
        onClose={closeModal}
250
        onAccept={confirmDelete}
251
      />
3000 stevensc 252
    </GoBackLayout>
5 stevensc 253
  )
254
}
255
 
256
export default MyCoachViewPage