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

@ -185,14 +185,7 @@ function SnippetsPage() {
React.useEffect(() => { React.useEffect(() => {
if (user) { if (user) {
user loadMore(page).then(newSnippets => setSnippets(curr => curr.concat(newSnippets)))
.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, user]) }, [loadMore, page, user])

Loading…
Cancel
Save