-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #209
- Loading branch information
Showing
13 changed files
with
203 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { exchangeRatesQueries } from './queries' | ||
import { useExchangeRatesStore } from './store' | ||
|
||
export function useExchangeRate({ | ||
fromCurrency, | ||
toCurrency, | ||
date, | ||
}: { | ||
fromCurrency: string | ||
toCurrency: string | ||
date?: string | ||
}) { | ||
const { exchangeRates, updateExchangeRate } = useExchangeRatesStore() | ||
|
||
const { data, isLoading } = useQuery({ | ||
...exchangeRatesQueries.detail({ | ||
fromCurrency, | ||
toCurrency, | ||
date, | ||
updateExchangeRate, | ||
}), | ||
initialData: exchangeRates.find( | ||
(e) => | ||
e.fromCurrency === fromCurrency && | ||
e.toCurrency === toCurrency && | ||
e.date === date, | ||
), | ||
enabled: fromCurrency !== toCurrency, | ||
}) | ||
|
||
return { | ||
exchangeRate: data, | ||
isLoading, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { getHonoClient } from '@/lib/client' | ||
import { createQueryKeys } from '@lukemorales/query-key-factory' | ||
import type { ExchangeRate } from './store' | ||
|
||
export const exchangeRatesQueries = createQueryKeys('exchange-rates', { | ||
detail: ({ | ||
fromCurrency, | ||
toCurrency, | ||
date, | ||
updateExchangeRate, | ||
}: { | ||
fromCurrency: string | ||
toCurrency: string | ||
date?: string | ||
updateExchangeRate: (exchangeRate: ExchangeRate) => void | ||
}) => ({ | ||
queryKey: [fromCurrency, toCurrency, date], | ||
queryFn: async () => { | ||
const hc = await getHonoClient() | ||
const res = await hc.v1['exchange-rates'][':fromCurrency'][ | ||
':toCurrency' | ||
].$get({ | ||
param: { fromCurrency, toCurrency }, | ||
query: { date }, | ||
}) | ||
if (!res.ok) { | ||
throw new Error(await res.text()) | ||
} | ||
|
||
const result = await res.json() | ||
if (result.date) { | ||
updateExchangeRate({ | ||
date: result.date, | ||
fromCurrency: result.fromCurrency, | ||
toCurrency: result.toCurrency, | ||
rate: result.rate, | ||
rateDecimal: result.rate.toString(), | ||
[result.fromCurrency]: 1, | ||
[result.toCurrency]: result.rate, | ||
}) | ||
} | ||
|
||
return result | ||
}, | ||
}), | ||
}) |
Oops, something went wrong.