bigbuffet-rw/app/soapbox/features/ui/components/funding_panel.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import React from 'react';
import { connect } from 'react-redux';
2020-04-14 13:45:38 -07:00
import { injectIntl } from 'react-intl';
2020-03-27 13:59:38 -07:00
import ImmutablePureComponent from 'react-immutable-pure-component';
import ProgressBar from '../../../components/progress_bar';
2020-07-04 12:14:36 -07:00
import { fetchPatronInstance } from 'soapbox/actions/patron';
2020-07-04 19:25:43 -07:00
import { Map as ImmutableMap } from 'immutable';
2020-03-27 13:59:38 -07:00
2020-06-30 15:33:21 -07:00
const moneyFormat = amount => (
new Intl
.NumberFormat('en-US', {
style: 'currency',
currency: 'usd',
notation: 'compact',
})
.format(amount/100)
);
2020-03-27 13:59:38 -07:00
class FundingPanel extends ImmutablePureComponent {
2020-03-31 15:45:12 -07:00
componentDidMount() {
2020-07-04 12:14:36 -07:00
this.props.dispatch(fetchPatronInstance());
2020-03-31 15:45:12 -07:00
}
2020-03-27 13:59:38 -07:00
render() {
const { patron } = this.props;
2020-07-04 12:14:36 -07:00
if (patron.isEmpty()) return null;
2020-03-31 15:45:12 -07:00
2020-07-04 12:14:36 -07:00
const amount = patron.getIn(['funding', 'amount']);
const goal = patron.getIn(['goals', '0', 'amount']);
const goal_text = patron.getIn(['goals', '0', 'text']);
2020-06-30 15:33:21 -07:00
const goal_reached = amount >= goal;
2020-03-27 13:59:38 -07:00
let ratio_text;
if (goal_reached) {
2020-06-30 15:33:21 -07:00
ratio_text = <><strong>{moneyFormat(goal)}</strong> per month<span className='funding-panel__reached'>&mdash; reached!</span></>;
2020-03-27 13:59:38 -07:00
} else {
2020-06-30 15:33:21 -07:00
ratio_text = <><strong>{moneyFormat(amount)} out of {moneyFormat(goal)}</strong> per month</>;
2020-03-27 13:59:38 -07:00
}
return (
<div className='wtf-panel funding-panel'>
<div className='wtf-panel-header'>
2020-04-14 11:44:40 -07:00
<i role='img' alt='users' className='fa fa-line-chart wtf-panel-header__icon' />
2020-03-27 13:59:38 -07:00
<span className='wtf-panel-header__label'>
<span>Funding Goal</span>
</span>
</div>
<div className='wtf-panel__content'>
<div className='funding-panel__ratio'>
{ratio_text}
</div>
2020-06-30 15:33:21 -07:00
<ProgressBar progress={amount/goal} />
2020-03-27 13:59:38 -07:00
<div className='funding-panel__description'>
{goal_text}
</div>
<a className='button' href={patron.get('url')}>Donate</a>
2020-03-27 13:59:38 -07:00
</div>
</div>
2020-04-14 11:44:40 -07:00
);
2020-03-27 13:59:38 -07:00
}
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
};
const mapStateToProps = state => {
return {
2020-07-04 19:25:43 -07:00
patron: state.getIn(['patron', 'instance'], ImmutableMap()),
2020-03-27 13:59:38 -07:00
};
};
export default injectIntl(
connect(mapStateToProps, null, null, {
forwardRef: true,
},
2020-04-14 11:44:40 -07:00
)(FundingPanel));