2018-02-24 03:23:36 +01:00
|
|
|
import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
|
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { toast } from '../_utils/toast'
|
2018-04-17 05:56:21 +02:00
|
|
|
import {
|
|
|
|
setStatusFavorited as setStatusFavoritedInDatabase
|
|
|
|
} from '../_database/timelines/updateStatus'
|
2018-02-24 03:23:36 +01:00
|
|
|
|
2018-02-24 23:49:28 +01:00
|
|
|
export async function setFavorited (statusId, favorited) {
|
2018-04-19 18:37:05 +02:00
|
|
|
let { online } = store.get()
|
|
|
|
if (!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-04-19 18:37:05 +02:00
|
|
|
let { currentInstance, accessToken } = store.get()
|
2018-03-21 01:41:39 +01:00
|
|
|
let networkPromise = favorited
|
2018-04-19 18:37:05 +02:00
|
|
|
? favoriteStatus(currentInstance, accessToken, statusId)
|
|
|
|
: unfavoriteStatus(currentInstance, accessToken, statusId)
|
|
|
|
store.setStatusFavorited(currentInstance, statusId, favorited) // optimistic update
|
2018-02-24 03:23:36 +01:00
|
|
|
try {
|
2018-03-21 01:41:39 +01:00
|
|
|
await networkPromise
|
2018-04-19 18:37:05 +02:00
|
|
|
await setStatusFavoritedInDatabase(currentInstance, statusId, favorited)
|
2018-02-24 03:23:36 +01:00
|
|
|
} 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-04-19 18:37:05 +02:00
|
|
|
store.setStatusFavorited(currentInstance, statusId, !favorited) // undo optimistic update
|
2018-02-24 03:23:36 +01:00
|
|
|
}
|
2018-02-24 23:49:28 +01:00
|
|
|
}
|