move /snippets list auth header code into lib

main
Mike Fix 5 years ago
parent 30b26bfc6a
commit 1c3ef894e3

@ -3,9 +3,9 @@ import debounce from 'lodash.debounce'
import omitBy from 'lodash.omitby'
import ms from 'ms'
import { DEFAULT_CODE, DEFAULT_SETTINGS } from './constants'
import { fileToDataURL } from './util'
import firebase from './client'
import { DEFAULT_CODE, DEFAULT_SETTINGS } from './constants'
export const client = axios.create({
baseURL: `${process.env.API_URL || ''}/api`,
@ -83,19 +83,27 @@ function getSnippet(uid = '', { host } = {}) {
})
}
function listSnippets(page, headers) {
return client
.get(`/snippets`, {
params: {
page
},
headers
})
.then(res => res.data)
.catch(e => {
console.error(e)
throw e
})
function listSnippets(page) {
// IDEA: move into axios interceptor
return firebase
.auth()
.currentUser.getIdToken()
.then(authorization =>
client
.get(`/snippets`, {
params: {
page
},
headers: {
authorization
}
})
.then(res => res.data)
.catch(e => {
console.error(e)
throw e
})
)
}
function isNotDefaultSetting(v, k) {

@ -185,14 +185,7 @@ function SnippetsPage() {
React.useEffect(() => {
if (user) {
user
.getIdToken()
// TODO move into `api.snippet.list` with axios interceptor or within wrappers
.then(authorization =>
loadMore(page, { authorization }).then(newSnippets =>
setSnippets(curr => curr.concat(newSnippets))
)
)
loadMore(page).then(newSnippets => setSnippets(curr => curr.concat(newSnippets)))
}
}, [loadMore, page, user])

Loading…
Cancel
Save