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 {