Skip to content

Commit

Permalink
feat(xx): notification support
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Oct 29, 2018
1 parent 83ad521 commit 3cbfaac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/core/botpress/src/server/static.js
Expand Up @@ -93,7 +93,7 @@ module.exports = bp => {
app.use(
tamper(function(req, res) {
const contentType = res.getHeaders()['content-type']
if (!contentType.includes('text/html')) {
if (!contentType || !contentType.includes('text/html')) {
return
}

Expand Down
Expand Up @@ -3,6 +3,7 @@ import { connect } from 'react-redux'
import { NavDropdown, MenuItem } from 'react-bootstrap'
import _ from 'lodash'
import classnames from 'classnames'
import { fetchNotifications } from '~/actions'

import NotificationComponent from './index.jsx'
import styles from './hubStyle.scss'
Expand Down Expand Up @@ -83,5 +84,6 @@ class NotificationHub extends NotificationComponent {
}

const mapStateToProps = state => ({ notifications: state.notifications })
const mapDispatchToProps = { fetchNotifications }

export default connect(mapStateToProps)(NotificationHub)
export default connect(mapStateToProps, { fetchNotifications, mapDispatchToProps })(NotificationHub)
31 changes: 23 additions & 8 deletions packages/core/botpress/src/web/components/Notifications/index.jsx
Expand Up @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'
import { Row, Col, Tooltip, OverlayTrigger } from 'react-bootstrap'
import moment from 'moment'
import classnames from 'classnames'

import axios from 'axios'
import EventBus from '~/util/EventBus'

const getNotificationStyle = (styles, notification) =>
Expand Down Expand Up @@ -40,16 +40,31 @@ export default class NotificationComponent extends Component {
this.context.router.history.push(notif.url)
}

markAsRead(notif) {
EventBus.default.emit('notifications.read', notif.id)
markAsRead = async notif => {
if (!window.BOTPRESS_XX) {
EventBus.default.emit('notifications.read', notif.id)
} else {
await axios.post(`/api/notifications/${notif.id}/read`)
this.props.fetchNotifications()
}
}

markAllAsRead = () => {
EventBus.default.emit('notifications.allRead')
markAllAsRead = async () => {
if (!window.BOTPRESS_XX) {
EventBus.default.emit('notifications.allRead')
} else {
await axios.post(`/api/notifications/read`)
this.props.fetchNotifications()
}
}

trashAll = () => {
EventBus.default.emit('notifications.trashAll')
trashAll = async () => {
if (!window.BOTPRESS_XX) {
EventBus.default.emit('notifications.trashAll')
} else {
await axios.post(`/api/notifications/archive`)
this.props.fetchNotifications()
}
}

renderMarkAsReadButton(notification, index) {
Expand Down Expand Up @@ -77,7 +92,7 @@ export default class NotificationComponent extends Component {
renderMenuItem(notification, index) {
const ItemComponent = this.itemComponent
const styles = this.styles
const date = moment(new Date(notification.date)).fromNow()
const date = moment(new Date(notification.date || notification.created_on)).fromNow()
const className = getNotificationStyle(styles, notification)
const checkButton = this.renderMarkAsReadButton(notification, index)
const iconClass = classnames('icon', 'material-icons', this.styles.icon)
Expand Down
5 changes: 3 additions & 2 deletions packages/core/botpress/src/web/views/Notifications/index.jsx
Expand Up @@ -6,7 +6,7 @@ import _ from 'lodash'
import NotificationComponent from '~/components/Notifications'
import ContentWrapper from '~/components/Layout/ContentWrapper'
import PageHeader from '~/components/Layout/PageHeader'

import { fetchNotifications } from '~/actions'
import styles from './style.scss'

// TODO: extending components is discouraged,
Expand Down Expand Up @@ -71,5 +71,6 @@ class NotificationHub extends NotificationComponent {
}

const mapStateToProps = state => ({ notifications: state.notifications })
const mapDispatchToProps = { fetchNotifications }

export default connect(mapStateToProps)(NotificationHub)
export default connect(mapStateToProps, mapDispatchToProps)(NotificationHub)

0 comments on commit 3cbfaac

Please sign in to comment.