Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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