Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fetch price immediately after currency changes #1276

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions api/resolvers/price.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,36 @@ async function fetchPrice (fiat) {
.then((res) => res.json())
.then((body) => parseFloat(body.data.amount))
.catch((err) => {
console.error(err)
console.error('price', err)
return -1
})
cache.set(fiat, { price, createdAt: Date.now() })
return price
}

async function getPrice (fiat) {
async function getPrice (fiat, fromCache = true) {
fiat ??= 'USD'

if (!fromCache) {
const newPrice = await fetchPrice(fiat)
return newPrice
}

if (cache.has(fiat)) {
const { price, createdAt } = cache.get(fiat)
const expired = createdAt + expiresIn < Date.now()
if (expired) fetchPrice(fiat).catch(console.error) // update cache
if (expired) fetchPrice(fiat).catch(console.error)
return price // serve stale price (this on the SSR critical path)
} else {
fetchPrice(fiat).catch(console.error)
return null
}
return null
}

export default {
Query: {
price: async (parent, { fiatCurrency }, ctx) => {
return await getPrice(fiatCurrency)
price: async (parent, { fiatCurrency, fromCache }, ctx) => {
return await getPrice(fiatCurrency, fromCache)
}
}
}
2 changes: 1 addition & 1 deletion api/typeDefs/price.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { gql } from 'graphql-tag'

export default gql`
extend type Query {
price(fiatCurrency: String): Float
price(fiatCurrency: String, fromCache: Boolean = true): Float
}
`
7 changes: 6 additions & 1 deletion components/price.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function usePrice () {
export function PriceProvider ({ price, children }) {
const me = useMe()
const fiatCurrency = me?.privates?.fiatCurrency
const { data } = useQuery(PRICE, {
const { data, refetch } = useQuery(PRICE, {
variables: { fiatCurrency },
...(SSR
? {}
Expand All @@ -31,6 +31,11 @@ export function PriceProvider ({ price, children }) {
})
})

useEffect(() => {
if (fiatCurrency) {
refetch({ fiatCurrency, fromCache: false })
}
}, [fiatCurrency, refetch])
const contextValue = useMemo(() => ({
price: data?.price || price,
fiatSymbol: CURRENCY_SYMBOLS[fiatCurrency] || '$'
Expand Down
4 changes: 2 additions & 2 deletions fragments/price.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from '@apollo/client'

export const PRICE = gql`
query price($fiatCurrency: String) {
price(fiatCurrency: $fiatCurrency)
query price($fiatCurrency: String, $fromCache: Boolean) {
price(fiatCurrency: $fiatCurrency, fromCache: $fromCache)
}`
Loading