pinafore/routes/_actions/favorite.js
Nolan Lawson 8d5690d63d
remove get() with string from Svelte calls (#169)
* remove get() with string pt 1

* remove get() with string pt 2

* fix typo

* fix some null exceptions in get()

* fixup code style
2018-04-19 09:37:05 -07:00

28 lines
1.1 KiB
JavaScript

import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
import { store } from '../_store/store'
import { toast } from '../_utils/toast'
import {
setStatusFavorited as setStatusFavoritedInDatabase
} from '../_database/timelines/updateStatus'
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 setStatusFavoritedInDatabase(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
}
}