From 0dc085411c5cc7abeb6340825f3eb9596af59645 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 4 Jul 2020 14:14:36 -0500 Subject: [PATCH] Refactor Patron code --- app/soapbox/actions/patron.js | 16 +++++++++------- .../features/ui/components/funding_panel.js | 19 ++++++++----------- app/soapbox/reducers/patron.js | 6 +++--- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/app/soapbox/actions/patron.js b/app/soapbox/actions/patron.js index ec9a35241..b9e55331d 100644 --- a/app/soapbox/actions/patron.js +++ b/app/soapbox/actions/patron.js @@ -1,10 +1,12 @@ import api from '../api'; -export const PATRON_FUNDING_IMPORT = 'PATRON_FUNDING_IMPORT'; -export const PATRON_FUNDING_FETCH_FAIL = 'PATRON_FUNDING_FETCH_FAIL'; +export const PATRON_INSTANCE_FETCH_REQUEST = 'PATRON_INSTANCE_FETCH_REQUEST'; +export const PATRON_INSTANCE_FETCH_SUCCESS = 'PATRON_INSTANCE_FETCH_SUCCESS'; +export const PATRON_INSTANCE_FETCH_FAIL = 'PATRON_INSTANCE_FETCH_FAIL'; -export function fetchFunding() { +export function fetchPatronInstance() { return (dispatch, getState) => { + dispatch({ type: PATRON_INSTANCE_FETCH_REQUEST }); api(getState).get('/api/patron/v1/instance').then(response => { dispatch(importFetchedFunding(response.data)); }).catch(error => { @@ -13,16 +15,16 @@ export function fetchFunding() { }; }; -export function importFetchedFunding(funding) { +export function importFetchedFunding(instance) { return { - type: PATRON_FUNDING_IMPORT, - funding, + type: PATRON_INSTANCE_FETCH_SUCCESS, + instance, }; } export function fetchFundingFail(error) { return { - type: PATRON_FUNDING_FETCH_FAIL, + type: PATRON_INSTANCE_FETCH_FAIL, error, skipAlert: true, }; diff --git a/app/soapbox/features/ui/components/funding_panel.js b/app/soapbox/features/ui/components/funding_panel.js index df96512e2..41889bd45 100644 --- a/app/soapbox/features/ui/components/funding_panel.js +++ b/app/soapbox/features/ui/components/funding_panel.js @@ -3,7 +3,7 @@ import { connect } from 'react-redux'; import { injectIntl } from 'react-intl'; import ImmutablePureComponent from 'react-immutable-pure-component'; import ProgressBar from '../../../components/progress_bar'; -import { fetchFunding } from 'soapbox/actions/patron'; +import { fetchPatronInstance } from 'soapbox/actions/patron'; const moneyFormat = amount => ( new Intl @@ -18,19 +18,16 @@ const moneyFormat = amount => ( class FundingPanel extends ImmutablePureComponent { componentDidMount() { - this.props.dispatch(fetchFunding()); + this.props.dispatch(fetchPatronInstance()); } render() { - const { funding, patronUrl } = this.props; + const { patron, patronUrl } = this.props; + if (patron.isEmpty()) return null; - if (!funding) { - return null; - } - - const amount = funding.getIn(['funding', 'amount']); - const goal = funding.getIn(['goals', '0', 'amount']); - const goal_text = funding.getIn(['goals', '0', 'text']); + const amount = patron.getIn(['funding', 'amount']); + const goal = patron.getIn(['goals', '0', 'amount']); + const goal_text = patron.getIn(['goals', '0', 'text']); const goal_reached = amount >= goal; let ratio_text; @@ -66,7 +63,7 @@ class FundingPanel extends ImmutablePureComponent { const mapStateToProps = state => { return { - funding: state.getIn(['patron', 'funding']), + patron: state.get('patron'), patronUrl: state.getIn(['soapbox', 'extensions', 'patron', 'baseUrl']), }; }; diff --git a/app/soapbox/reducers/patron.js b/app/soapbox/reducers/patron.js index f3987c9b2..459927bf9 100644 --- a/app/soapbox/reducers/patron.js +++ b/app/soapbox/reducers/patron.js @@ -1,12 +1,12 @@ -import { PATRON_FUNDING_IMPORT } from '../actions/patron'; +import { PATRON_INSTANCE_FETCH_SUCCESS } from '../actions/patron'; import { Map as ImmutableMap, fromJS } from 'immutable'; const initialState = ImmutableMap(); export default function patron(state = initialState, action) { switch(action.type) { - case PATRON_FUNDING_IMPORT: - return state.set('funding', fromJS(action.funding)); + case PATRON_INSTANCE_FETCH_SUCCESS: + return fromJS(action.instance); default: return state; }