Rev 5306 | Rev 5311 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { axios } from '../../utils'
import { addNotification } from '../notification/notification.actions'
import { conversationActionTypes } from './conversation.types'
export const sendMessage = async (url, message) => {
return (dispatch) => {
dispatch(startLoading())
const formData = new FormData()
formData.append('message', emojione.toShort(message))
axios.post(url, formData)
.then(({ data: response }) => {
if (!response.success) {
dispatch(addNotification({ style: 'danger', message: 'Ha ocurrido un error' }))
return
}
dispatch(setNewMessage(response.data))
})
.catch((err) => {
const errorMessage = new Error(err).message
dispatch(addNotification({ style: 'danger', message: errorMessage }))
})
.finally(() => dispatch(finishLoading()))
}
}
export const getMessages = async (url) => {
return (dispatch) => {
dispatch(startLoading())
axios.get(url)
.then(({ data: response }) => {
if (!response.success) {
dispatch(addNotification({ style: 'danger', message: 'Ha ocurrido un error' }))
return
}
const updatedMessages = [...response.data.items].reverse()
dispatch(setMessages(updatedMessages))
})
.catch((err) => {
const errorMessage = new Error(err).message
dispatch(addNotification({ style: 'danger', message: errorMessage }))
})
.finally(() => dispatch(finishLoading()))
}
}
export const setNewMessage = (newMessage) => ({
type: conversationActionTypes.SET_NEW_MESSAGE,
payload: newMessage
})
export const setMessages = (unreadMessages) => ({
type: conversationActionTypes.SET_MESSAGES,
payload: unreadMessages
})
export const startLoading = () => ({
type: conversationActionTypes.START_LOADING
})
export const finishLoading = () => ({
type: conversationActionTypes.FINISH_LOADING
})