Updated Gab Trends api, redux, panel

• Updated:
- Gab Trends api, redux, panel
- GabTrendsController takes in query string for feed type
This commit is contained in:
mgabdev 2020-06-08 22:10:51 -04:00
parent 24ad9b6ff4
commit 4a23c62ec8
4 changed files with 77 additions and 44 deletions

View File

@ -6,20 +6,26 @@ class Api::V1::GabTrendsController < Api::BaseController
skip_before_action :set_cache_headers skip_before_action :set_cache_headers
def index def index
body = Redis.current.get("gabtrends") type = params[:type]
if type == 'feed'
body = Redis.current.get("gabtrends")
if body.nil? if body.nil?
uri = URI("https://trends.gab.com/trend-feed/json") uri = URI("https://trends.gab.com/trend-feed/json")
uri.query = URI.encode_www_form({}) uri.query = URI.encode_www_form({})
res = Net::HTTP.get_response(uri) res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess) if res.is_a?(Net::HTTPSuccess)
body = res.body body = res.body
Redis.current.set("gabtrends", res.body) Redis.current.set("gabtrends", res.body)
Redis.current.expire("gabtrends", 1.hour.seconds) Redis.current.expire("gabtrends", 1.hour.seconds)
end
end end
end
render json: body render json: body
else
raise GabSocial::NotPermittedError
end
end end
end end

View File

@ -5,34 +5,37 @@ export const GAB_TRENDS_RESULTS_FETCH_REQUEST = 'GAB_TRENDS_RESULTS_FETCH_REQUES
export const GAB_TRENDS_RESULTS_FETCH_SUCCESS = 'GAB_TRENDS_RESULTS_FETCH_SUCCESS' export const GAB_TRENDS_RESULTS_FETCH_SUCCESS = 'GAB_TRENDS_RESULTS_FETCH_SUCCESS'
export const GAB_TRENDS_RESULTS_FETCH_FAIL = 'GAB_TRENDS_RESULTS_FETCH_FAIL' export const GAB_TRENDS_RESULTS_FETCH_FAIL = 'GAB_TRENDS_RESULTS_FETCH_FAIL'
export const fetchGabTrends = () => { export const fetchGabTrends = (feedType) => {
return function (dispatch, getState) { return function (dispatch, getState) {
dispatch(fetchGabTrendsRequest()) dispatch(fetchGabTrendsRequest(feedType))
api(getState).get('/api/v1/gab_trends').then(response => { api(getState).get(`/api/v1/gab_trends?type=${feedType}`).then(response => {
dispatch(fetchGabTrendsSuccess(response.data.items)) dispatch(fetchGabTrendsSuccess(response.data.items, feedType))
}).catch(function (error) { }).catch(function (error) {
dispatch(fetchGabTrendsFail(error)) dispatch(fetchGabTrendsFail(error, feedType))
}) })
} }
} }
function fetchGabTrendsRequest() { function fetchGabTrendsRequest(feedType) {
return { return {
type: GAB_TRENDS_RESULTS_FETCH_REQUEST, type: GAB_TRENDS_RESULTS_FETCH_REQUEST,
feedType,
} }
} }
function fetchGabTrendsSuccess(items) { function fetchGabTrendsSuccess(items, feedType) {
return { return {
type: GAB_TRENDS_RESULTS_FETCH_SUCCESS, type: GAB_TRENDS_RESULTS_FETCH_SUCCESS,
items, items,
feedType,
} }
} }
function fetchGabTrendsFail(error) { function fetchGabTrendsFail(error, feedType) {
return { return {
type: GAB_TRENDS_RESULTS_FETCH_FAIL, type: GAB_TRENDS_RESULTS_FETCH_FAIL,
error, error,
feedType,
} }
} }

View File

@ -11,34 +11,47 @@ const messages = defineMessages({
}) })
const mapStateToProps = (state) => ({ const mapStateToProps = (state) => ({
gabtrends: state.getIn(['gab_trends', 'items']), isError: state.getIn(['gab_trends', 'feed', 'isError']),
isLoading: state.getIn(['gab_trends', 'feed', 'isLoading']),
items: state.getIn(['gab_trends', 'feed', 'items']),
}) })
const mapDispatchToProps = (dispatch) => ({ const mapDispatchToProps = (dispatch) => ({
onFetchGabTrends: () => dispatch(fetchGabTrends()), onfetchGabTrends: () => dispatch(fetchGabTrends('feed')),
}) })
export default export default
@connect(mapStateToProps, mapDispatchToProps)
@injectIntl @injectIntl
@connect(mapStateToProps, mapDispatchToProps)
class TrendsPanel extends ImmutablePureComponent { class TrendsPanel extends ImmutablePureComponent {
static propTypes = { static propTypes = {
intl: PropTypes.object.isRequired, intl: PropTypes.object.isRequired,
gabtrends: ImmutablePropTypes.list.isRequired, isError: PropTypes.bool,
onFetchGabTrends: PropTypes.func.isRequired, isLoading: PropTypes.bool,
items: ImmutablePropTypes.list.isRequired,
onfetchGabTrends: PropTypes.func.isRequired,
} }
updateOnProps = [ updateOnProps = [
'gabtrends', 'items',
'isLoading',
'isError',
] ]
componentDidMount() { componentDidMount() {
this.props.onFetchGabTrends() this.props.onfetchGabTrends()
} }
render() { render() {
const { intl, gabtrends } = this.props const {
intl,
isError,
isLoading,
items,
} = this.props
if (isError) return null
return ( return (
<PanelLayout <PanelLayout
@ -46,11 +59,11 @@ class TrendsPanel extends ImmutablePureComponent {
title={intl.formatMessage(messages.title)} title={intl.formatMessage(messages.title)}
> >
<ScrollableList <ScrollableList
showLoading={gabtrends.size == 0} isLoading={isLoading}
scrollKey='trending-items' scrollKey='trending-items'
> >
{ {
gabtrends.slice(0, 8).map((trend, i) => ( items.slice(0, 8).map((trend, i) => (
<TrendsItem <TrendsItem
key={`gab-trend-${i}`} key={`gab-trend-${i}`}
index={i + 1} index={i + 1}

View File

@ -6,30 +6,41 @@ import {
import { import {
Map as ImmutableMap, Map as ImmutableMap,
List as ImmutableList, List as ImmutableList,
fromJS fromJS,
} from 'immutable' } from 'immutable'
const initialState = ImmutableMap({ const initialState = ImmutableMap({
items: ImmutableList(), feed: ImmutableMap({
loading: false, items: ImmutableList(),
error: false, 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) { export default function (state = initialState, action) {
switch (action.type) { switch (action.type) {
case GAB_TRENDS_RESULTS_FETCH_REQUEST: case GAB_TRENDS_RESULTS_FETCH_REQUEST:
return state.set('loading', true) return state.setIn([action.feedType, 'isLoading'], true);
case GAB_TRENDS_RESULTS_FETCH_SUCCESS: case GAB_TRENDS_RESULTS_FETCH_SUCCESS:
return state.withMutations(map => { return normalizeList(state, action.feedType, action.items)
map.set('items', fromJS(action.items));
map.set('error', false);
map.set('loading', false);
});
case GAB_TRENDS_RESULTS_FETCH_FAIL: case GAB_TRENDS_RESULTS_FETCH_FAIL:
return state.withMutations(map => { return setListFailed(state, action.feedType)
map.set('error', !!action.error);
map.set('loading', false);
});
default: default:
return state return state
} }