EmailList: let csv links be clickable, add combined.csv support, conditionally display elements

This commit is contained in:
Alex Gleason
2021-06-15 15:02:36 -05:00
parent 6255ba4976
commit eb006202e7
6 changed files with 81 additions and 7 deletions

View File

@@ -8,6 +8,19 @@ import Column from '../ui/components/column';
import RegistrationModePicker from './components/registration_mode_picker';
import { parseVersion } from 'soapbox/utils/features';
import sourceCode from 'soapbox/utils/code';
import { getSubscribersCsv, getUnsubscribersCsv, getCombinedCsv } from 'soapbox/actions/email_list';
import { getFeatures } from 'soapbox/utils/features';
// https://stackoverflow.com/a/53230807
const download = (response, filename) => {
const url = URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
link.remove();
};
const messages = defineMessages({
heading: { id: 'column.admin.dashboard', defaultMessage: 'Dashboard' },
@@ -15,6 +28,7 @@ const messages = defineMessages({
const mapStateToProps = (state, props) => ({
instance: state.get('instance'),
supportsEmailList: getFeatures(state.get('instance')).emailList,
});
export default @connect(mapStateToProps)
@@ -24,10 +38,32 @@ class Dashboard extends ImmutablePureComponent {
static propTypes = {
intl: PropTypes.object.isRequired,
instance: ImmutablePropTypes.map.isRequired,
supportsEmailList: PropTypes.bool,
};
handleSubscribersClick = e => {
this.props.dispatch(getSubscribersCsv()).then((response) => {
download(response, 'subscribers.csv');
}).catch(() => {});
e.preventDefault();
}
handleUnsubscribersClick = e => {
this.props.dispatch(getUnsubscribersCsv()).then((response) => {
download(response, 'unsubscribers.csv');
}).catch(() => {});
e.preventDefault();
}
handleCombinedClick = e => {
this.props.dispatch(getCombinedCsv()).then((response) => {
download(response, 'combined.csv');
}).catch(() => {});
e.preventDefault();
}
render() {
const { intl, instance } = this.props;
const { intl, instance, supportsEmailList } = this.props;
const v = parseVersion(instance.get('version'));
const userCount = instance.getIn(['stats', 'user_count']);
const mau = instance.getIn(['pleroma', 'stats', 'mau']);
@@ -96,6 +132,14 @@ class Dashboard extends ImmutablePureComponent {
<li>{v.software} <span className='pull-right'>{v.version}</span></li>
</ul>
</div>
{supportsEmailList && <div className='dashwidget'>
<h4><FormattedMessage id='admin.dashwidgets.email_list_header' defaultMessage='Email list' /></h4>
<ul>
<li><a href='#' onClick={this.handleSubscribersClick} target='_blank'>subscribers.csv</a></li>
<li><a href='#' onClick={this.handleUnsubscribersClick} target='_blank'>unsubscribers.csv</a></li>
<li><a href='#' onClick={this.handleCombinedClick} target='_blank'>combined.csv</a></li>
</ul>
</div>}
</div>
</Column>
);