good coding pracitce: process function axios response
axios.get(generateUrl('/apps/radio/favorites'), req).then((response) => {
this.processNotifications(response.data)
this.state = 'ok'
}).catch((error) => {
clearInterval(this.loop)
if (error.response && error.response.status === 400) {
this.state = 'no-token'
} else if (error.response && error.response.status === 401) {
showError(t('integration_twitter', 'Failed to get Twitter notifications'))
this.state = 'error'
} else {
// there was an error in notif processing
console.debug(error)
}
})
},
processNotifications(newNotifications) {
if (this.lastDate) {
// just add those which are more recent than our most recent one
let i = 0
while (i < newNotifications.length && this.lastDate < newNotifications[i].timestamp) {
i++
}
if (i > 0) {
const toAdd = this.filter(newNotifications.slice(0, i))
this.notifications = toAdd.concat(this.notifications)
}
} else {
// first time we don't check the date
this.notifications = this.filter(newNotifications)
}
// update lastDate manually (explained in data)
const nbNotif = this.notifications.length
this.lastDate = (nbNotif > 0) ? this.notifications[0].timestamp : null
},
https://github.com/nextcloud/integration_twitter/blob/master/src/views/Dashboard.vue