Rev 1662 | Rev 2846 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { feedActionTypes } from './feed.types'
const initialState = {
feeds: {
byId: {},
allIds: []
},
timelineUrl: '',
currentFeed: null,
loading: false,
currentPage: 1,
pages: 1
}
const feedReducer = (state = initialState, { type, payload }) => {
switch (type) {
case feedActionTypes.SET_TIMELINE_URL: {
const newTimelineUrl = payload
return { ...state, timelineUrl: newTimelineUrl }
}
case feedActionTypes.LOAD_FEEDS: {
return { ...state, loading: true }
}
case feedActionTypes.LOAD_FEEDS_SUCCESS: {
const { feeds, currentPage, pages } = payload
const feedsById = {}
feeds.forEach((feed) => {
feedsById[feed.feed_unique] = {
...feed,
comments: feed.comments.map((comment) => ({
...comment,
feedId: feed.feed_unique
}))
}
})
const feedsIds = feeds.map((feed) => feed.feed_unique)
return {
...state,
feeds: { byId: feedsById, allIds: feedsIds },
currentPage,
pages,
loading: false
}
}
case feedActionTypes.REMOVE_COMMENT: {
const { feedId, commentId } = payload
const feed = state.feeds.byId[feedId]
return {
...state,
feeds: {
...state.feeds,
byId: {
...state.feeds.byId,
[feedId]: {
...feed,
comments: feed.comments.filter(
({ unique }) => unique !== commentId
),
owner_comments: feed.owner_comments - 1
}
}
}
}
}
case feedActionTypes.ADD_COMMENT: {
const { feedId, comment } = payload
const feed = state.feeds.byId[feedId]
return {
...state,
feeds: {
...state.feeds,
byId: {
...state.feeds.byId,
[feedId]: {
...feed,
owner_comments: feed.owner_comments + 1,
comments: [...feed.comments, { ...comment, feedId }]
}
}
}
}
}
case feedActionTypes.LOAD_FEEDS_FAILURE: {
return state
}
case feedActionTypes.ADD_FEED: {
const { feedSharedId, feed } = payload
const isShare = state.feeds.allIds.some((id) => id === feedSharedId)
if (isShare) {
const feedShared = state.feeds.byId[feedSharedId]
feedShared.owner_comments = feedShared.owner_comments + 1
return {
...state,
feeds: {
byId: {
...state.feeds.byId,
[feed.feed_unique]: feed,
[feedSharedId]: feedShared
},
allIds: [feed.feed_unique, ...state.feeds.allIds]
}
}
}
return {
...state,
feeds: {
byId: { ...state.feeds.byId, [feed.feed_unique]: feed },
allIds: [feed.feed_unique, ...state.feeds.allIds]
}
}
}
case feedActionTypes.UPDATE_FEED: {
const { feed, uuid } = payload
return {
...state,
feeds: {
...state.feeds,
byId: { ...state.feeds.byId, [uuid]: feed }
}
}
}
case feedActionTypes.DELETE_FEED: {
const feedId = payload
const newIds = state.feeds.allIds.filter((id) => id !== feedId)
const newFeeds = { ...state.feeds.byId, [feedId]: null }
return {
...state,
feeds: {
...state.feeds,
byId: newFeeds,
allIds: newIds
}
}
}
case feedActionTypes.ADD_CURRENT_FEED: {
return { ...state, currentFeed: payload }
}
case feedActionTypes.SET_CURRENT_PAGE: {
const newCurrentPage = payload
return { ...state, currentPage: newCurrentPage }
}
default:
return state
}
}
export default feedReducer