| 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) {
|
|
|
13 |
dispatch(addNotification({ style: 'danger', message: 'Ha ocurrido un error' }))
|
|
|
14 |
return
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
dispatch(setNewMessage(response.data))
|
|
|
18 |
})
|
|
|
19 |
.catch((error) => {
|
|
|
20 |
throw Error(error.message)
|
|
|
21 |
})
|
|
|
22 |
.catch((err) => dispatch(addNotification({ style: 'danger', message: new Error(err).message })))
|
|
|
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) {
|
|
|
33 |
dispatch(addNotification({ style: 'danger', message: 'Ha ocurrido un error' }))
|
|
|
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
|
|
|
42 |
dispatch(addNotification({ style: 'danger', message: errorMessage }))
|
|
|
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) {
|
|
|
54 |
dispatch(addNotification({ style: 'danger', message: 'Ha ocurrido un error' }))
|
|
|
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
|
|
|
63 |
dispatch(addNotification({ style: 'danger', message: errorMessage }))
|
|
|
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 |
|
|
|
79 |
export const setMessages = (messages) => ({
|
| 5311 |
stevensc |
80 |
type: conversationActionTypes.SET_MESSAGES,
|
| 5320 |
stevensc |
81 |
payload: messages
|
| 5311 |
stevensc |
82 |
})
|
|
|
83 |
|
|
|
84 |
export const startLoading = () => ({
|
|
|
85 |
type: conversationActionTypes.START_LOADING
|
|
|
86 |
})
|
|
|
87 |
|
|
|
88 |
export const finishLoading = () => ({
|
|
|
89 |
type: conversationActionTypes.FINISH_LOADING
|
|
|
90 |
})
|