Rev 3285 | Rev 3432 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { axios } from '../../utils'
import { feedActionTypes } from './feed.types'
export const setTimelineUrl = (url) => ({
type: feedActionTypes.SET_TIMELINE_URL,
payload: url
})
export const loadFeeds = () => ({
type: feedActionTypes.LOAD_FEEDS
})
export const loadFeedsSuccess = (feeds, currentPage, pages) => ({
type: feedActionTypes.LOAD_FEEDS_SUCCESS,
payload: { feeds, currentPage, pages }
})
export const addFeed = (url, feed, shareId) => {
return async (dispatch) => {
const form = new FormData()
const feedFields = Object.keys(feed)
feedFields.forEach((field) => form.append(field, feed[field]))
const response = await axios.post(url, form)
const { data, success } = response.data
if (!success) {
const error = typeof data === 'string' ? data : 'Error al publicar'
throw new Error(error)
}
dispatch(addFeedSuccess(data, shareId))
}
}
export const addFeedSuccess = (feed, feedSharedId = '') => ({
type: feedActionTypes.ADD_FEED,
payload: { feed, feedSharedId }
})
export const deleteFeed = (url, id) => {
return async (dispatch) => {
const response = await axios.post(url)
const { data, success } = response.data
if (!success) {
const err =
typeof data === 'string' ? data : 'Error al borrar la publicación'
throw new Error(err)
}
dispatch(deleteFeedSuccess(id))
return data
}
}
export const deleteFeedSuccess = (feedId) => ({
type: feedActionTypes.DELETE_FEED,
payload: feedId
})
export const updateFeed = ({ feed, uuid }) => ({
type: feedActionTypes.UPDATE_FEED,
payload: { feed, uuid }
})
export const setCurrentPage = (page) => ({
type: feedActionTypes.SET_CURRENT_PAGE,
payload: page
})
export const fetchFeeds = (url, page) => {
return (dispatch) => {
if (!url || url.includes('undefined')) return
dispatch(loadFeeds())
axios.get(url + '?page=' + page).then(({ data: response }) => {
const { data, success } = response
if (!success) {
throw new Error(
'Error al obtener las publicaciones, por favor intente más tarde'
)
}
const feeds = data.current.items
const currentPage = data.current.page
const pages = data.total.pages
dispatch(loadFeedsSuccess(feeds, currentPage, pages))
})
}
}
export const removeComment = ({ feedId, commentId }) => ({
type: feedActionTypes.REMOVE_COMMENT,
payload: { feedId, commentId }
})
export const addComment = ({ feedId, comment }) => ({
type: feedActionTypes.ADD_COMMENT,
payload: { feedId, comment }
})