2018-04-10 03:44:37 +10:00
|
|
|
import Notification from '../notification/notification.vue'
|
2018-08-12 21:14:34 +10:00
|
|
|
import notificationsFetcher from '../../services/notifications_fetcher/notifications_fetcher.service.js'
|
2017-05-31 18:47:18 +10:00
|
|
|
|
2018-08-16 20:57:16 +10:00
|
|
|
import { sortBy, filter } from 'lodash'
|
2016-11-28 05:44:56 +11:00
|
|
|
|
|
|
|
const Notifications = {
|
2018-08-12 21:14:34 +10:00
|
|
|
created () {
|
|
|
|
const store = this.$store
|
|
|
|
const credentials = store.state.users.currentUser.credentials
|
|
|
|
|
|
|
|
notificationsFetcher.startFetching({ store, credentials })
|
2016-11-28 05:44:56 +11:00
|
|
|
},
|
|
|
|
computed: {
|
2017-02-19 06:42:00 +11:00
|
|
|
notifications () {
|
2018-08-12 21:14:34 +10:00
|
|
|
return this.$store.state.statuses.notifications.data
|
2017-02-19 06:42:00 +11:00
|
|
|
},
|
|
|
|
unseenNotifications () {
|
|
|
|
return filter(this.notifications, ({seen}) => !seen)
|
|
|
|
},
|
2016-11-28 05:44:56 +11:00
|
|
|
visibleNotifications () {
|
2017-02-19 06:42:00 +11:00
|
|
|
// Don't know why, but sortBy([seen, -action.id]) doesn't work.
|
|
|
|
let sortedNotifications = sortBy(this.notifications, ({action}) => -action.id)
|
|
|
|
sortedNotifications = sortBy(sortedNotifications, 'seen')
|
2018-08-12 21:14:34 +10:00
|
|
|
return sortedNotifications
|
2017-02-19 06:42:00 +11:00
|
|
|
},
|
|
|
|
unseenCount () {
|
|
|
|
return this.unseenNotifications.length
|
|
|
|
}
|
|
|
|
},
|
2017-05-31 18:47:18 +10:00
|
|
|
components: {
|
2018-04-10 03:44:37 +10:00
|
|
|
Notification
|
2017-05-31 18:47:18 +10:00
|
|
|
},
|
2017-02-19 06:42:00 +11:00
|
|
|
watch: {
|
|
|
|
unseenCount (count) {
|
2017-02-19 22:19:47 +11:00
|
|
|
if (count > 0) {
|
|
|
|
this.$store.dispatch('setPageTitle', `(${count})`)
|
|
|
|
} else {
|
|
|
|
this.$store.dispatch('setPageTitle', '')
|
|
|
|
}
|
2017-02-19 06:42:00 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
markAsSeen () {
|
|
|
|
this.$store.commit('markNotificationsAsSeen', this.visibleNotifications)
|
2018-08-12 21:14:34 +10:00
|
|
|
},
|
|
|
|
fetchOlderNotifications () {
|
|
|
|
const store = this.$store
|
|
|
|
const credentials = store.state.users.currentUser.credentials
|
|
|
|
notificationsFetcher.fetchAndUpdate({
|
|
|
|
store,
|
|
|
|
credentials,
|
|
|
|
older: true
|
|
|
|
})
|
2016-11-28 05:44:56 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Notifications
|