pinafore/src/routes/_actions/accounts.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

73 lines
2.1 KiB
JavaScript

import { getAccount } from '../_api/user'
import { getRelationship } from '../_api/relationships'
import { database } from '../_database/database'
import { store } from '../_store/store'
async function _updateAccount (accountId, instanceName, accessToken) {
let localPromise = database.getAccount(instanceName, accountId)
let remotePromise = getAccount(instanceName, accessToken, accountId).then(account => {
/* no await */ database.setAccount(instanceName, account)
return account
})
try {
store.set({ currentAccountProfile: (await localPromise) })
} catch (e) {
console.error(e)
}
try {
store.set({ currentAccountProfile: (await remotePromise) })
} catch (e) {
console.error(e)
}
}
async function _updateRelationship (accountId, instanceName, accessToken) {
let localPromise = database.getRelationship(instanceName, accountId)
let remotePromise = getRelationship(instanceName, accessToken, accountId).then(relationship => {
/* no await */ database.setRelationship(instanceName, relationship)
return relationship
})
try {
store.set({ currentAccountRelationship: (await localPromise) })
} catch (e) {
console.error(e)
}
try {
store.set({ currentAccountRelationship: (await remotePromise) })
} catch (e) {
console.error(e)
}
}
export async function updateLocalRelationship (instanceName, accountId, relationship) {
await database.setRelationship(instanceName, relationship)
try {
store.set({ currentAccountRelationship: relationship })
} catch (e) {
console.error(e)
}
}
export async function clearProfileAndRelationship () {
store.set({
currentAccountProfile: null,
currentAccountRelationship: null
})
}
export async function updateProfileAndRelationship (accountId) {
let { currentInstance, accessToken } = store.get()
await Promise.all([
_updateAccount(accountId, currentInstance, accessToken),
_updateRelationship(accountId, currentInstance, accessToken)
])
}
export async function updateRelationship (accountId) {
let { currentInstance, accessToken } = store.get()
await _updateRelationship(accountId, currentInstance, accessToken)
}