pinafore/src/routes/_actions/favorite.js
Nolan Lawson 0022286b46
fix: first stab at i18n, extract English strings, add French (#1904)
* first attempt

* progress

* working

* working

* test timeago

* rm

* get timeago working

* reduce size

* fix whitespace

* more intl stuff

* more effort

* more work

* more progress

* more work

* more intl

* set lang=LOCALE

* flatten

* more work

* add ltr/rtl

* more work

* add comments

* yet more work

* still more work

* more work

* fix tests

* more test and string fixes

* fix test

* fix test

* fix test

* fix some more strings, add test

* fix snackbar

* fix }

* fix typo

* fix english

* measure perf

* start on french

* more work on french

* more french

* more french

* finish french

* fix some missing translations

* update readme

* fix test
2020-11-29 14:13:27 -08:00

30 lines
1.2 KiB
JavaScript

import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
import { store } from '../_store/store'
import { toast } from '../_components/toast/toast'
import { database } from '../_database/database'
import { formatIntl } from '../_utils/formatIntl'
export async function setFavorited (statusId, favorited) {
const { online } = store.get()
if (!online) {
/* no await */ toast.say(favorited ? 'intl.cannotFavoriteOffline' : 'intl.cannotUnfavoriteOffline')
return
}
const { currentInstance, accessToken } = store.get()
const 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)
/* no await */ toast.say(favorited
? formatIntl('intl.unableToFavorite', { error: (e.message || '') })
: formatIntl('intl.unableToUnfavorite', { error: (e.message || '') })
)
store.setStatusFavorited(currentInstance, statusId, !favorited) // undo optimistic update
}
}