Pinafore-Web-Client-Frontend/routes/_actions/favorite.js

28 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-02-24 03:23:36 +01:00
import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
import { store } from '../_store/store'
import { database } from '../_database/database'
import { toast } from '../_utils/toast'
2018-02-24 23:49:28 +01:00
export async function setFavorited (statusId, favorited) {
if (!store.get('online')) {
2018-02-25 03:20:33 +01:00
toast.say(`You cannot ${favorited ? 'favorite' : 'unfavorite'} while offline.`)
2018-02-24 23:49:28 +01:00
return
}
2018-02-24 03:23:36 +01:00
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
try {
2018-02-25 04:25:33 +01:00
await (favorited
2018-02-24 03:23:36 +01:00
? favoriteStatus(instanceName, accessToken, statusId)
: unfavoriteStatus(instanceName, accessToken, statusId))
2018-02-24 23:49:28 +01:00
await database.setStatusFavorited(instanceName, statusId, favorited)
2018-02-24 03:23:36 +01:00
let statusModifications = store.get('statusModifications')
let currentStatusModifications = statusModifications[instanceName] =
(statusModifications[instanceName] || {favorites: {}, reblogs: {}})
currentStatusModifications.favorites[statusId] = favorited
store.set({statusModifications: statusModifications})
} catch (e) {
2018-02-24 23:49:28 +01:00
console.error(e)
2018-02-25 03:20:33 +01:00
toast.say(`Failed to ${favorited ? 'favorite' : 'unfavorite'}. ` + (e.message || ''))
2018-02-24 03:23:36 +01:00
}
2018-02-24 23:49:28 +01:00
}