Added WIP for news redux actions (prev. gab actions)
• Added: - WIP for news redux actions (prev. gab actions) - renamed gab redux actions to "news" • Todo: - Add RSS expanding in news action - Complete leadline setting in news reducer
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
import {
|
||||
GAB_TRENDS_RESULTS_FETCH_REQUEST,
|
||||
GAB_TRENDS_RESULTS_FETCH_SUCCESS,
|
||||
GAB_TRENDS_RESULTS_FETCH_FAIL,
|
||||
GAB_NEWS_RESULTS_FETCH_REQUEST,
|
||||
GAB_NEWS_RESULTS_FETCH_SUCCESS,
|
||||
GAB_NEWS_RESULTS_FETCH_FAIL,
|
||||
} from '../actions/gab'
|
||||
import {
|
||||
Map as ImmutableMap,
|
||||
List as ImmutableList,
|
||||
fromJS,
|
||||
} from 'immutable'
|
||||
|
||||
const initialState = ImmutableMap({
|
||||
feed: ImmutableMap({
|
||||
items: ImmutableList(),
|
||||
isLoading: false,
|
||||
isError: false,
|
||||
}),
|
||||
partner: ImmutableMap({
|
||||
items: {},
|
||||
isLoading: false,
|
||||
isError: false,
|
||||
}),
|
||||
news: ImmutableMap({
|
||||
items: {},
|
||||
isLoading: false,
|
||||
isError: false,
|
||||
}),
|
||||
})
|
||||
|
||||
const normalizeList = (state, type, items) => {
|
||||
return state.set(type, ImmutableMap({
|
||||
items: fromJS(items),
|
||||
isLoading: false,
|
||||
isError: false,
|
||||
}))
|
||||
}
|
||||
|
||||
const setListFailed = (state, type) => {
|
||||
return state.set(type, ImmutableMap({
|
||||
items: ImmutableList(),
|
||||
isLoading: false,
|
||||
isError: true,
|
||||
}))
|
||||
}
|
||||
|
||||
export default function (state = initialState, action) {
|
||||
switch (action.type) {
|
||||
case GAB_TRENDS_RESULTS_FETCH_REQUEST:
|
||||
return state.setIn([action.feedType, 'isLoading'], true);
|
||||
case GAB_TRENDS_RESULTS_FETCH_SUCCESS:
|
||||
if (action.feedType === 'feed') {
|
||||
return normalizeList(state, action.feedType, action.items.items)
|
||||
} else if (action.feedType === 'partner') {
|
||||
return state.set('partner', ImmutableMap({
|
||||
items: action.items['trends'] || {},
|
||||
isLoading: false,
|
||||
isError: false,
|
||||
}))
|
||||
}
|
||||
|
||||
return state
|
||||
case GAB_TRENDS_RESULTS_FETCH_FAIL:
|
||||
return setListFailed(state, action.feedType)
|
||||
case GAB_NEWS_RESULTS_FETCH_REQUEST:
|
||||
return state.setIn(['news', 'isLoading'], true);
|
||||
case GAB_NEWS_RESULTS_FETCH_SUCCESS:
|
||||
return normalizeList(state, 'news', action.items)
|
||||
case GAB_NEWS_RESULTS_FETCH_FAIL:
|
||||
return setListFailed(state, 'news')
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import contexts from './contexts'
|
||||
import conversations from './conversations'
|
||||
import custom_emojis from './custom_emojis'
|
||||
import filters from './filters'
|
||||
import gab from './gab'
|
||||
import groups from './groups'
|
||||
import group_categories from './group_categories'
|
||||
import group_editor from './group_editor'
|
||||
@@ -23,6 +22,7 @@ import media_attachments from './media_attachments'
|
||||
import meta from './meta'
|
||||
import modal from './modal'
|
||||
import mutes from './mutes'
|
||||
import news from './news'
|
||||
import notifications from './notifications'
|
||||
import polls from './polls'
|
||||
import popover from './popover'
|
||||
@@ -52,7 +52,6 @@ const reducers = {
|
||||
conversations,
|
||||
custom_emojis,
|
||||
filters,
|
||||
gab,
|
||||
groups,
|
||||
group_categories,
|
||||
group_editor,
|
||||
@@ -69,6 +68,7 @@ const reducers = {
|
||||
meta,
|
||||
modal,
|
||||
mutes,
|
||||
news,
|
||||
notifications,
|
||||
polls,
|
||||
popover,
|
||||
|
||||
161
app/javascript/gabsocial/reducers/news.js
Normal file
161
app/javascript/gabsocial/reducers/news.js
Normal file
@@ -0,0 +1,161 @@
|
||||
import {
|
||||
GAB_TRENDS_FETCH_REQUEST,
|
||||
GAB_TRENDS_FETCH_SUCCESS,
|
||||
GAB_TRENDS_FETCH_FAIL,
|
||||
GAB_TREND_FEED_EXPAND_REQUEST,
|
||||
GAB_TREND_FEED_EXPAND_SUCCESS,
|
||||
GAB_TREND_FEED_EXPAND_FAIL,
|
||||
GAB_NEWS_FETCH_REQUEST,
|
||||
GAB_NEWS_FETCH_SUCCESS,
|
||||
GAB_NEWS_FETCH_FAIL,
|
||||
LATEST_GAB_STATUSES_FETCH_REQUEST,
|
||||
LATEST_GAB_STATUSES_FETCH_SUCCESS,
|
||||
LATEST_GAB_STATUSES_FETCH_FAIL,
|
||||
} from '../actions/news'
|
||||
import {
|
||||
Map as ImmutableMap,
|
||||
List as ImmutableList,
|
||||
fromJS,
|
||||
} from 'immutable'
|
||||
|
||||
const defaultMap = ImmutableMap({
|
||||
isLoading: false,
|
||||
isFetched: false,
|
||||
items: ImmutableList(),
|
||||
})
|
||||
|
||||
const initialState = ImmutableMap({
|
||||
trends_leadline: ImmutableMap({
|
||||
title: null,
|
||||
image: null,
|
||||
trends_url: null,
|
||||
}),
|
||||
trends_headlines: defaultMap,
|
||||
trends_breaking: defaultMap,
|
||||
gab_news: defaultMap,
|
||||
latest_from_gab: defaultMap,
|
||||
trends_feeds: ImmutableMap(), // ex: { 'feed_id_1': defaultMap, 'feed_id_2': defaultMap, ... }
|
||||
})
|
||||
|
||||
const normalizeHeadlineItem = (item) => {
|
||||
return ImmutableMap({
|
||||
trend_id: item._id,
|
||||
title: item.title,
|
||||
trends_url: `https://trends.gab.com/trend?url=${item.href}`,
|
||||
})
|
||||
}
|
||||
|
||||
const normalizeTrendsItem = (item) => {
|
||||
return ImmutableMap({
|
||||
title: `${item.pagePreview.title}`.trim(),
|
||||
description: `${item.pagePreview.description}`.trim(),
|
||||
publish_date: item.pubDate,
|
||||
image: Array.isArray(item.pagePreview.images) ? item.pagePreview.images[0] : null, // : todo : proxy
|
||||
feed_title: item.feed.title,
|
||||
feed_slug: item.feed.slug,
|
||||
feed_base_url: `${item.feed.url}`.replace('www.', '').replace('https://', '').replace('http://', '').replace('/', ''),
|
||||
trend_feed_id: item.feed._id,
|
||||
trend_id: item._id,
|
||||
trends_url: `https://trends.gab.com/trend?url=${item.link}`,
|
||||
})
|
||||
}
|
||||
|
||||
const normalizeNewsItem = (item) => {
|
||||
return ImmutableMap({
|
||||
id: item.id,
|
||||
url: item.url,
|
||||
title: item.title,
|
||||
image: item.image,
|
||||
publish_date: item.date_published,
|
||||
})
|
||||
}
|
||||
|
||||
const setStateKeysOnRequest = (state, keys) => {
|
||||
return state.withMutations((map) => {
|
||||
keys.map((key) => map.setIn([key, 'isLoading'], true))
|
||||
})
|
||||
}
|
||||
|
||||
const setStateKeysOnFail = (state, keys) => {
|
||||
return state.withMutations((map) => {
|
||||
keys.map((key) => {
|
||||
map.setIn([key, 'isLoading'], false)
|
||||
map.setIn([key, 'isFetched'], true)
|
||||
map.setIn([key, 'items'], ImmutableList())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const setStateKeysOnSuccess = (state, keysAndData) => {
|
||||
return state.withMutations((map) => {
|
||||
Object.keys(keysAndData).map((key) => {
|
||||
map.setIn([key, 'isLoading'], false)
|
||||
map.setIn([key, 'isFetched'], true)
|
||||
map.setIn([key, 'items'], keysAndData[key])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default function (state = initialState, action) {
|
||||
switch (action.type) {
|
||||
case GAB_TRENDS_FETCH_REQUEST:
|
||||
return setStateKeysOnRequest(state, ['trends_headlines', 'trends_breaking'])
|
||||
case GAB_TRENDS_FETCH_FAIL:
|
||||
return setStateKeysOnFail(state, ['trends_headlines', 'trends_breaking'])
|
||||
case GAB_TRENDS_FETCH_SUCCESS:
|
||||
let trendsFetchData = {}
|
||||
try {
|
||||
trendsFetchData.trends_headlines = ImmutableList(action.items.trends.leadHeadlines.map((item) => normalizeHeadlineItem(item)))
|
||||
trendsFetchData.trends_breaking = ImmutableList(action.items.trends.rssFeedItems.map((item) => normalizeTrendsItem(item)))
|
||||
} catch (error) {
|
||||
trendsFetchData = {
|
||||
breakingItems: ImmutableList(),
|
||||
headlineItems: ImmutableList(),
|
||||
}
|
||||
}
|
||||
// : todo :
|
||||
// trends_leadline: ImmutableMap({
|
||||
// title: null,
|
||||
// image: null,
|
||||
// trends_url: null,
|
||||
// }),
|
||||
|
||||
return setStateKeysOnSuccess(state, trendsFetchData)
|
||||
|
||||
//
|
||||
case LATEST_GAB_STATUSES_FETCH_REQUEST:
|
||||
return setStateKeysOnRequest(state, ['latest_from_gab'])
|
||||
case LATEST_GAB_STATUSES_FETCH_FAIL:
|
||||
return setStateKeysOnFail(state, ['latest_from_gab'])
|
||||
case LATEST_GAB_STATUSES_FETCH_SUCCESS:
|
||||
let latestGabStatusData = {}
|
||||
try {
|
||||
latestGabStatusData.latest_from_gab = ImmutableList(action.statuses.map((status) => status.id))
|
||||
} catch (error) {
|
||||
latestGabStatusData = {
|
||||
latest_from_gab: ImmutableList(),
|
||||
}
|
||||
}
|
||||
return setStateKeysOnSuccess(state, latestGabStatusData)
|
||||
|
||||
//
|
||||
case GAB_NEWS_FETCH_REQUEST:
|
||||
return setStateKeysOnRequest(state, ['gab_news'])
|
||||
case GAB_NEWS_FETCH_FAIL:
|
||||
return setStateKeysOnFail(state, ['gab_news'])
|
||||
case GAB_NEWS_FETCH_SUCCESS:
|
||||
let latestGabNewsData = {}
|
||||
try {
|
||||
latestGabNewsData.gab_news = ImmutableList(action.items.map((item) => normalizeNewsItem(item)))
|
||||
} catch (error) {
|
||||
console.log("news reducer error: ", error)
|
||||
latestGabNewsData = {
|
||||
gab_news: ImmutableList(),
|
||||
}
|
||||
}
|
||||
return setStateKeysOnSuccess(state, latestGabNewsData)
|
||||
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user