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
def index
body = Redis.current.get("gabtrends")
type = params[:type]
if type == 'feed'
body = Redis.current.get("gabtrends")
if body.nil?
uri = URI("https://trends.gab.com/trend-feed/json")
uri.query = URI.encode_www_form({})
if body.nil?
uri = URI("https://trends.gab.com/trend-feed/json")
uri.query = URI.encode_www_form({})
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess)
body = res.body
Redis.current.set("gabtrends", res.body)
Redis.current.expire("gabtrends", 1.hour.seconds)
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess)
body = res.body
Redis.current.set("gabtrends", res.body)
Redis.current.expire("gabtrends", 1.hour.seconds)
end
end
end
render json: body
render json: body
else
raise GabSocial::NotPermittedError
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_FAIL = 'GAB_TRENDS_RESULTS_FETCH_FAIL'
export const fetchGabTrends = () => {
export const fetchGabTrends = (feedType) => {
return function (dispatch, getState) {
dispatch(fetchGabTrendsRequest())
dispatch(fetchGabTrendsRequest(feedType))
api(getState).get('/api/v1/gab_trends').then(response => {
dispatch(fetchGabTrendsSuccess(response.data.items))
api(getState).get(`/api/v1/gab_trends?type=${feedType}`).then(response => {
dispatch(fetchGabTrendsSuccess(response.data.items, feedType))
}).catch(function (error) {
dispatch(fetchGabTrendsFail(error))
dispatch(fetchGabTrendsFail(error, feedType))
})
}
}
function fetchGabTrendsRequest() {
function fetchGabTrendsRequest(feedType) {
return {
type: GAB_TRENDS_RESULTS_FETCH_REQUEST,
feedType,
}
}
function fetchGabTrendsSuccess(items) {
function fetchGabTrendsSuccess(items, feedType) {
return {
type: GAB_TRENDS_RESULTS_FETCH_SUCCESS,
items,
feedType,
}
}
function fetchGabTrendsFail(error) {
function fetchGabTrendsFail(error, feedType) {
return {
type: GAB_TRENDS_RESULTS_FETCH_FAIL,
error,
feedType,
}
}

View File

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

View File

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