2018-02-23 18:23:36 -08:00
|
|
|
import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
|
|
|
|
import { store } from '../_store/store'
|
2018-12-22 15:37:51 -08:00
|
|
|
import { toast } from '../_components/toast/toast'
|
2018-08-29 19:03:12 -07:00
|
|
|
import { database } from '../_database/database'
|
2020-11-29 14:13:27 -08:00
|
|
|
import { formatIntl } from '../_utils/formatIntl'
|
2018-02-23 18:23:36 -08:00
|
|
|
|
2018-02-24 14:49:28 -08:00
|
|
|
export async function setFavorited (statusId, favorited) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const { online } = store.get()
|
2018-04-19 09:37:05 -07:00
|
|
|
if (!online) {
|
2020-11-29 14:13:27 -08:00
|
|
|
/* no await */ toast.say(favorited ? 'intl.cannotFavoriteOffline' : 'intl.cannotUnfavoriteOffline')
|
2018-02-24 14:49:28 -08:00
|
|
|
return
|
|
|
|
}
|
2019-08-03 13:49:37 -07:00
|
|
|
const { currentInstance, accessToken } = store.get()
|
|
|
|
const networkPromise = favorited
|
2018-04-19 09:37:05 -07:00
|
|
|
? favoriteStatus(currentInstance, accessToken, statusId)
|
|
|
|
: unfavoriteStatus(currentInstance, accessToken, statusId)
|
|
|
|
store.setStatusFavorited(currentInstance, statusId, favorited) // optimistic update
|
2018-02-23 18:23:36 -08:00
|
|
|
try {
|
2018-03-20 17:41:39 -07:00
|
|
|
await networkPromise
|
2018-08-29 19:03:12 -07:00
|
|
|
await database.setStatusFavorited(currentInstance, statusId, favorited)
|
2018-02-23 18:23:36 -08:00
|
|
|
} catch (e) {
|
2018-02-24 14:49:28 -08:00
|
|
|
console.error(e)
|
2020-11-29 14:13:27 -08:00
|
|
|
/* no await */ toast.say(favorited
|
|
|
|
? formatIntl('intl.unableToFavorite', { error: (e.message || '') })
|
|
|
|
: formatIntl('intl.unableToUnfavorite', { error: (e.message || '') })
|
|
|
|
)
|
2018-04-19 09:37:05 -07:00
|
|
|
store.setStatusFavorited(currentInstance, statusId, !favorited) // undo optimistic update
|
2018-02-23 18:23:36 -08:00
|
|
|
}
|
2018-02-24 14:49:28 -08:00
|
|
|
}
|