From 2a245d076ab6cc7dc1ed3afc19410b0494c91762 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 31 Mar 2020 17:45:12 -0500 Subject: [PATCH] Support GET /patron/v1/funding --- app/gabsocial/actions/patron.js | 47 +++++++++++++++++++ .../features/ui/components/funding_panel.js | 25 +++++++--- app/gabsocial/pages/home_page.js | 4 +- app/gabsocial/reducers/index.js | 2 + app/gabsocial/reducers/patron.js | 13 +++++ app/soapbox/config.js | 5 ++ webpack/development.js | 2 + 7 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 app/gabsocial/actions/patron.js create mode 100644 app/gabsocial/reducers/patron.js create mode 100644 app/soapbox/config.js diff --git a/app/gabsocial/actions/patron.js b/app/gabsocial/actions/patron.js new file mode 100644 index 000000000..4f4453caf --- /dev/null +++ b/app/gabsocial/actions/patron.js @@ -0,0 +1,47 @@ +import api, { getLinks } from '../api'; +import openDB from '../storage/db'; +import { me } from 'gabsocial/initial_state'; + +export const PATRON_FUNDING_FETCH_REQUEST = 'PATRON_FUNDING_FETCH_REQUEST'; +export const PATRON_FUNDING_FETCH_SUCCESS = 'PATRON_FUNDING_FETCH_SUCCESS'; +export const PATRON_FUNDING_FETCH_FAIL = 'PATRON_FUNDING_FETCH_FAIL'; +export const PATRON_FUNDING_IMPORT = 'PATRON_FUNDING_IMPORT'; + +export function fetchFunding() { + return (dispatch, getState) => { + api(getState).get(`/patron/v1/funding`).then(response => { + dispatch(importFetchedFunding(response.data)); + }).then(() => { + dispatch(fetchFundingSuccess()); + }).catch(error => { + dispatch(fetchFundingFail(error)); + }); + }; +}; + +export function importFetchedFunding(funding) { + return { + type: PATRON_FUNDING_IMPORT, + funding + }; +} + +export function fetchFundingRequest() { + return { + type: PATRON_FUNDING_FETCH_REQUEST, + }; +}; + +export function fetchAccountSuccess() { + return { + type: PATRON_FUNDING_FETCH_SUCCESS, + }; +}; + +export function fetchFundingFail(error) { + return { + type: PATRON_FUNDING_FETCH_FAIL, + error, + skipAlert: true, + }; +}; diff --git a/app/gabsocial/features/ui/components/funding_panel.js b/app/gabsocial/features/ui/components/funding_panel.js index 5e8209668..4d3c74b96 100644 --- a/app/gabsocial/features/ui/components/funding_panel.js +++ b/app/gabsocial/features/ui/components/funding_panel.js @@ -5,19 +5,30 @@ import { injectIntl, FormattedMessage } from 'react-intl'; import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePureComponent from 'react-immutable-pure-component'; import ProgressBar from '../../../components/progress_bar'; -import { funding } from '../../../initial_state'; - +import { fetchFunding } from 'gabsocial/actions/patron'; class FundingPanel extends ImmutablePureComponent { + + componentDidMount () { + this.props.dispatch(fetchFunding()); + } + render() { - const { goal, goal_text } = this.props; - const goal_reached = funding.amount >= goal; + const { funding } = this.props; + + if (!funding) { + return null; + } + + const goal = funding.getIn(['goals', '0', 'amount']); + const goal_text = funding.getIn(['goals', '0', 'text']); + const goal_reached = funding.get('amount') >= goal; let ratio_text; if (goal_reached) { ratio_text = <>${Math.floor(goal/100)} per month— reached!; } else { - ratio_text = <>${Math.floor(funding.amount/100)} out of ${Math.floor(goal/100)} per month; + ratio_text = <>${Math.floor(funding.get('amount')/100)} out of ${Math.floor(goal/100)} per month; } return ( @@ -32,7 +43,7 @@ class FundingPanel extends ImmutablePureComponent {
{ratio_text}
- +
{goal_text}
@@ -43,9 +54,9 @@ class FundingPanel extends ImmutablePureComponent { } }; - const mapStateToProps = state => { return { + funding: state.getIn(['patron', 'funding']) }; }; diff --git a/app/gabsocial/pages/home_page.js b/app/gabsocial/pages/home_page.js index cac62625a..a4056e9c9 100644 --- a/app/gabsocial/pages/home_page.js +++ b/app/gabsocial/pages/home_page.js @@ -1,6 +1,7 @@ import React from 'react'; import { connect } from 'react-redux'; import { me, funding } from 'gabsocial/initial_state'; +import soapbox from 'soapbox/config'; import PropTypes from 'prop-types'; import ImmutablePureComponent from 'react-immutable-pure-component'; import WhoToFollowPanel from '../features/ui/components/who_to_follow_panel'; @@ -21,7 +22,6 @@ export default @connect(mapStateToProps) class HomePage extends ImmutablePureComponent { render () { const {children, account} = this.props; - const hasFunding = funding && funding.goal && funding.goal.amount && funding.goal.text; return (
@@ -31,7 +31,7 @@ class HomePage extends ImmutablePureComponent {
- {hasFunding && } + {soapbox.features.patron && }
diff --git a/app/gabsocial/reducers/index.js b/app/gabsocial/reducers/index.js index 82921f516..2225cabc4 100644 --- a/app/gabsocial/reducers/index.js +++ b/app/gabsocial/reducers/index.js @@ -37,6 +37,7 @@ import group_relationships from './group_relationships'; import group_lists from './group_lists'; import group_editor from './group_editor'; import sidebar from './sidebar'; +import patron from './patron'; const reducers = { dropdown_menu, @@ -77,6 +78,7 @@ const reducers = { group_lists, group_editor, sidebar, + patron, }; export default combineReducers(reducers); diff --git a/app/gabsocial/reducers/patron.js b/app/gabsocial/reducers/patron.js new file mode 100644 index 000000000..f3987c9b2 --- /dev/null +++ b/app/gabsocial/reducers/patron.js @@ -0,0 +1,13 @@ +import { PATRON_FUNDING_IMPORT } 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)); + default: + return state; + } +}; diff --git a/app/soapbox/config.js b/app/soapbox/config.js new file mode 100644 index 000000000..fa0fc5f51 --- /dev/null +++ b/app/soapbox/config.js @@ -0,0 +1,5 @@ +export default { + features: { + patron: true, + }, +} diff --git a/webpack/development.js b/webpack/development.js index b9a1717e2..2a3da8c52 100644 --- a/webpack/development.js +++ b/webpack/development.js @@ -10,6 +10,7 @@ const watchOptions = {}; // TODO: Make this configurable const backendUrl = 'http://localhost:4000'; +const patronUrl = 'http://localhost:5000'; if (process.env.VAGRANT) { // If we are in Vagrant, we can't rely on inotify to update us with changed @@ -68,6 +69,7 @@ module.exports = merge(sharedConfig, { '/socket': backendUrl, '/oauth/revoke': backendUrl, '/.well-known/webfinger': backendUrl, + '/patron': patronUrl, }, }, });