pinafore/src/routes/_actions/favorite.js
Nolan Lawson 4bd181d3cc
fix: update Sapper to latest (#775)
* fix: update to latest sapper

fixes #416

* fix error and debug pages

* requestIdleCallback makes column switching feel way nicer than double rAF

* add export feature

* add better csp info

* workaround for sapper sub-page issue

* clarify in readme about exporting

* fix now config

* switch from rIC to triple raf

* style-loader is no longer used

* update theming guide
2018-12-11 07:31:48 -08:00

26 lines
1 KiB
JavaScript

import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
import { store } from '../_store/store'
import { toast } from '../_utils/toast'
import { database } from '../_database/database'
export async function setFavorited (statusId, favorited) {
let { online } = store.get()
if (!online) {
toast.say(`You cannot ${favorited ? 'favorite' : 'unfavorite'} while offline.`)
return
}
let { currentInstance, accessToken } = store.get()
let networkPromise = favorited
? favoriteStatus(currentInstance, accessToken, statusId)
: unfavoriteStatus(currentInstance, accessToken, statusId)
store.setStatusFavorited(currentInstance, statusId, favorited) // optimistic update
try {
await networkPromise
await database.setStatusFavorited(currentInstance, statusId, favorited)
} catch (e) {
console.error(e)
toast.say(`Failed to ${favorited ? 'favorite' : 'unfavorite'}. ` + (e.message || ''))
store.setStatusFavorited(currentInstance, statusId, !favorited) // undo optimistic update
}
}