2017-09-22 18:38:38 -04:00
|
|
|
import GlobalUI from './index'
|
|
|
|
|
2017-09-19 23:48:46 -04:00
|
|
|
const Notifications = {
|
|
|
|
notifications: null,
|
2017-09-22 18:38:38 -04:00
|
|
|
unreadNotificationsCount: 0,
|
|
|
|
init: serverData => {
|
|
|
|
Notifications.unreadNotificationsCount = serverData.unreadNotificationsCount
|
|
|
|
},
|
2017-09-19 23:48:46 -04:00
|
|
|
fetch: render => {
|
|
|
|
$.ajax({
|
|
|
|
url: '/notifications.json',
|
|
|
|
success: function(data) {
|
|
|
|
Notifications.notifications = data
|
|
|
|
render()
|
|
|
|
}
|
|
|
|
})
|
2017-09-22 18:38:38 -04:00
|
|
|
},
|
2017-09-25 15:21:04 -04:00
|
|
|
incrementUnread: (render) => {
|
|
|
|
Notifications.unreadNotificationsCount++
|
|
|
|
render()
|
|
|
|
},
|
|
|
|
decrementUnread: (render) => {
|
|
|
|
Notifications.unreadNotificationsCount--
|
|
|
|
render()
|
|
|
|
},
|
2017-09-22 18:38:38 -04:00
|
|
|
markAsRead: (render, id) => {
|
|
|
|
const n = Notifications.notifications.find(n => n.id === id)
|
|
|
|
$.ajax({
|
2017-09-25 15:21:04 -04:00
|
|
|
url: `/notifications/${id}/mark_read.json`,
|
2017-09-22 18:38:38 -04:00
|
|
|
method: 'PUT',
|
|
|
|
success: function(r) {
|
|
|
|
if (n) {
|
2017-09-29 14:05:39 -04:00
|
|
|
Notifications.unreadNotificationsCount--
|
2017-09-22 18:38:38 -04:00
|
|
|
n.is_read = true
|
|
|
|
render()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
GlobalUI.notifyUser('There was an error marking that notification as read')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
markAsUnread: (render, id) => {
|
|
|
|
const n = Notifications.notifications.find(n => n.id === id)
|
|
|
|
$.ajax({
|
2017-09-25 15:21:04 -04:00
|
|
|
url: `/notifications/${id}/mark_unread.json`,
|
2017-09-22 18:38:38 -04:00
|
|
|
method: 'PUT',
|
|
|
|
success: function() {
|
|
|
|
if (n) {
|
|
|
|
Notifications.unreadNotificationsCount++
|
|
|
|
n.is_read = false
|
|
|
|
render()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
GlobalUI.notifyUser('There was an error marking that notification as read')
|
|
|
|
}
|
|
|
|
})
|
2017-09-19 23:48:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Notifications
|