pinafore/routes/_actions/favorite.js

28 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-02-24 02:23:36 +00:00
import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
import { store } from '../_store/store'
import { toast } from '../_utils/toast'
import {
setStatusFavorited as setStatusFavoritedInDatabase
} from '../_database/timelines/updateStatus'
2018-02-24 02:23:36 +00:00
2018-02-24 22:49:28 +00:00
export async function setFavorited (statusId, favorited) {
if (!store.get('online')) {
2018-02-25 02:20:33 +00:00
toast.say(`You cannot ${favorited ? 'favorite' : 'unfavorite'} while offline.`)
2018-02-24 22:49:28 +00:00
return
}
2018-02-24 02:23:36 +00:00
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
2018-03-21 00:41:39 +00:00
let networkPromise = favorited
? favoriteStatus(instanceName, accessToken, statusId)
: unfavoriteStatus(instanceName, accessToken, statusId)
store.setStatusFavorited(instanceName, statusId, favorited) // optimistic update
2018-02-24 02:23:36 +00:00
try {
2018-03-21 00:41:39 +00:00
await networkPromise
await setStatusFavoritedInDatabase(instanceName, statusId, favorited)
2018-02-24 02:23:36 +00:00
} catch (e) {
2018-02-24 22:49:28 +00:00
console.error(e)
2018-02-25 02:20:33 +00:00
toast.say(`Failed to ${favorited ? 'favorite' : 'unfavorite'}. ` + (e.message || ''))
2018-03-21 00:41:39 +00:00
store.setStatusFavorited(instanceName, statusId, !favorited) // undo optimistic update
2018-02-24 02:23:36 +00:00
}
2018-02-24 22:49:28 +00:00
}