| 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 |
|
| 5311 |
stevensc |
48 |
export const setNewMessage = (newMessage) => ({
|
|
|
49 |
type: conversationActionTypes.SET_NEW_MESSAGE,
|
|
|
50 |
payload: newMessage
|
|
|
51 |
})
|
|
|
52 |
|
|
|
53 |
export const setMessages = (unreadMessages) => ({
|
|
|
54 |
type: conversationActionTypes.SET_MESSAGES,
|
|
|
55 |
payload: unreadMessages
|
|
|
56 |
})
|
|
|
57 |
|
|
|
58 |
export const startLoading = () => ({
|
|
|
59 |
type: conversationActionTypes.START_LOADING
|
|
|
60 |
})
|
|
|
61 |
|
|
|
62 |
export const finishLoading = () => ({
|
|
|
63 |
type: conversationActionTypes.FINISH_LOADING
|
|
|
64 |
})
|