Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5320 | 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'
5317 stevensc 2
import { addNotification } from '../notification/notification.actions'
5307 stevensc 3
import { conversationActionTypes } from './conversation.types'
5306 stevensc 4
 
5317 stevensc 5
export const sendMessage = (url, message) => {
6
  return (dispatch) => {
7
    const formData = new FormData()
8
    formData.append('message', emojione.toShort(message))
9
 
10
    axios.post(url, formData)
11
      .then(({ data: response }) => {
12
        if (!response.success) {
5331 stevensc 13
          dispatch(addNotification({ style: 'danger', msg: 'Ha ocurrido un error' }))
5317 stevensc 14
          return
15
        }
16
 
17
        dispatch(setNewMessage(response.data))
18
      })
19
      .catch((error) => {
20
        throw Error(error.message)
21
      })
5331 stevensc 22
      .catch((err) => dispatch(addNotification({ style: 'danger', msg: new Error(err).message })))
5317 stevensc 23
      .finally(() => dispatch(finishLoading()))
24
  }
25
}
26
 
27
export const getMessages = (url) => {
28
  return (dispatch) => {
29
    dispatch(startLoading())
30
    axios.get(url)
31
      .then(({ data: response }) => {
32
        if (!response.success) {
5331 stevensc 33
          dispatch(addNotification({ style: 'danger', msg: 'Ha ocurrido un error' }))
5317 stevensc 34
          return
35
        }
36
        const updatedMessages = [...response.data.items].reverse()
37
 
38
        dispatch(setMessages(updatedMessages))
39
      })
40
      .catch((err) => {
41
        const errorMessage = new Error(err).message
5331 stevensc 42
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
5317 stevensc 43
      })
44
      .finally(() => dispatch(finishLoading()))
45
  }
46
}
47
 
5320 stevensc 48
export const getMessagesByPage = (url, page = 1) => {
49
  return (dispatch) => {
50
    dispatch(startLoading())
51
    axios.get(`${url}?page=${page}`)
52
      .then(({ data: response }) => {
53
        if (!response.success) {
5331 stevensc 54
          dispatch(addNotification({ style: 'danger', msg: 'Ha ocurrido un error' }))
5320 stevensc 55
          return
56
        }
57
        const updatedMessages = [...response.data.items].reverse()
58
 
59
        dispatch(setMessages(updatedMessages))
60
      })
61
      .catch((err) => {
62
        const errorMessage = new Error(err).message
5331 stevensc 63
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
5320 stevensc 64
      })
65
      .finally(() => dispatch(finishLoading()))
66
  }
67
}
68
 
5311 stevensc 69
export const setNewMessage = (newMessage) => ({
70
  type: conversationActionTypes.SET_NEW_MESSAGE,
71
  payload: newMessage
72
})
73
 
5320 stevensc 74
export const updateMessages = (newMessages) => ({
75
  type: conversationActionTypes.SET_UPDATE_MESSAGES,
76
  payload: newMessages
77
})
78
 
5331 stevensc 79
export const setPage = (messages) => ({
80
  type: conversationActionTypes.SET_MESSAGES,
81
  payload: messages
82
})
83
 
5320 stevensc 84
export const setMessages = (messages) => ({
5311 stevensc 85
  type: conversationActionTypes.SET_MESSAGES,
5320 stevensc 86
  payload: messages
5311 stevensc 87
})
88
 
89
export const startLoading = () => ({
90
  type: conversationActionTypes.START_LOADING
91
})
92
 
93
export const finishLoading = () => ({
94
  type: conversationActionTypes.FINISH_LOADING
95
})