Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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