From 4a23c62ec83e51d3ace13e494d2b0f86a2708881 Mon Sep 17 00:00:00 2001 From: mgabdev <> Date: Mon, 8 Jun 2020 22:10:51 -0400 Subject: [PATCH] Updated Gab Trends api, redux, panel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Updated: - Gab Trends api, redux, panel - GabTrendsController takes in query string for feed type --- .../api/v1/gab_trends_controller.rb | 30 ++++++++------ .../gabsocial/actions/gab_trends.js | 19 +++++---- .../components/panel/trends_panel.js | 33 +++++++++++----- .../gabsocial/reducers/gab_trends.js | 39 ++++++++++++------- 4 files changed, 77 insertions(+), 44 deletions(-) diff --git a/app/controllers/api/v1/gab_trends_controller.rb b/app/controllers/api/v1/gab_trends_controller.rb index 5a2cb7c1..98ad103f 100644 --- a/app/controllers/api/v1/gab_trends_controller.rb +++ b/app/controllers/api/v1/gab_trends_controller.rb @@ -6,20 +6,26 @@ class Api::V1::GabTrendsController < Api::BaseController skip_before_action :set_cache_headers def index - body = Redis.current.get("gabtrends") - - if body.nil? - uri = URI("https://trends.gab.com/trend-feed/json") - uri.query = URI.encode_www_form({}) + 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({}) - 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 diff --git a/app/javascript/gabsocial/actions/gab_trends.js b/app/javascript/gabsocial/actions/gab_trends.js index 10ba9a37..8cacf481 100644 --- a/app/javascript/gabsocial/actions/gab_trends.js +++ b/app/javascript/gabsocial/actions/gab_trends.js @@ -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, } } \ No newline at end of file diff --git a/app/javascript/gabsocial/components/panel/trends_panel.js b/app/javascript/gabsocial/components/panel/trends_panel.js index 1079210e..8fcd3fa7 100644 --- a/app/javascript/gabsocial/components/panel/trends_panel.js +++ b/app/javascript/gabsocial/components/panel/trends_panel.js @@ -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 ( { - gabtrends.slice(0, 8).map((trend, i) => ( + items.slice(0, 8).map((trend, i) => ( { + 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 }