Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
5306 stevensc 1
import { axios } from '../../utils'
5307 stevensc 2
import { conversationActionTypes } from './conversation.types'
5306 stevensc 3
 
5311 stevensc 4
export const setNewMessage = (newMessage) => ({
5
  type: conversationActionTypes.SET_NEW_MESSAGE,
6
  payload: newMessage
7
})
8
 
9
export const setMessages = (unreadMessages) => ({
10
  type: conversationActionTypes.SET_MESSAGES,
11
  payload: unreadMessages
12
})
13
 
14
export const startLoading = () => ({
15
  type: conversationActionTypes.START_LOADING
16
})
17
 
18
export const finishLoading = () => ({
19
  type: conversationActionTypes.FINISH_LOADING
20
})
21
 
22
export const getMessages = async (url) => {
5306 stevensc 23
  return (dispatch) => {
5307 stevensc 24
    dispatch(startLoading())
5311 stevensc 25
    axios.get(url)
5306 stevensc 26
      .then(({ data: response }) => {
27
        if (!response.success) {
5314 stevensc 28
          console.log({ style: 'danger', message: 'Ha ocurrido un error' })
5306 stevensc 29
          return
30
        }
5311 stevensc 31
        const updatedMessages = [...response.data.items].reverse()
5306 stevensc 32
 
5311 stevensc 33
        dispatch(setMessages(updatedMessages))
5306 stevensc 34
      })
5307 stevensc 35
      .catch((err) => {
36
        const errorMessage = new Error(err).message
5314 stevensc 37
        console.log({ style: 'danger', message: errorMessage })
38
        console.log({ style: 'danger', message: errorMessage })
5306 stevensc 39
      })
5307 stevensc 40
      .finally(() => dispatch(finishLoading()))
5306 stevensc 41
  }
42
}