bigbuffet-rw/app/soapbox/features/ui/containers/notifications_container.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

import React from 'react';
2020-03-27 13:59:38 -07:00
import { injectIntl } from 'react-intl';
import { NotificationStack } from 'react-notification';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
2020-03-27 13:59:38 -07:00
import { dismissAlert } from '../../../actions/alerts';
import { getAlerts } from '../../../selectors';
2022-03-21 11:09:01 -07:00
const CustomNotificationStack = (props) => (
2022-05-12 13:56:09 -07:00
<div role='assertive' data-testid='toast' className='z-1000 fixed inset-0 flex items-end px-4 py-6 pointer-events-none pt-16 lg:pt-20 sm:items-start'>
2022-03-21 11:09:01 -07:00
<NotificationStack {...props} />
</div>
);
const defaultBarStyleFactory = (index, style, notification) => {
return Object.assign(
{},
style,
{ bottom: `${14 + index * 12 + index * 42}px` },
);
};
2020-03-27 13:59:38 -07:00
const mapStateToProps = (state, { intl }) => {
const notifications = getAlerts(state);
notifications.forEach(notification => {
['title', 'message', 'actionLabel'].forEach(key => {
const value = notification[key];
if (typeof value === 'object') {
notification[key] = intl.formatMessage(value);
}
});
if (notification.actionLabel) {
notification.action = (
<Link to={notification.actionLink}>
{notification.actionLabel}
</Link>
);
2020-03-27 13:59:38 -07:00
}
});
2020-03-27 13:59:38 -07:00
return { notifications, linkComponent: Link };
2020-03-27 13:59:38 -07:00
};
const mapDispatchToProps = (dispatch) => {
const onDismiss = alert => {
dispatch(dismissAlert(alert));
};
2020-03-27 13:59:38 -07:00
return {
onDismiss,
onClick: onDismiss,
barStyleFactory: defaultBarStyleFactory,
activeBarStyleFactory: defaultBarStyleFactory,
2020-03-27 13:59:38 -07:00
};
};
2022-03-21 11:09:01 -07:00
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(CustomNotificationStack));