-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] Display balance in
THB
and USD
- Loading branch information
Showing
5 changed files
with
98 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use client' | ||
|
||
import { FC, PropsWithChildren, createContext, useContext, useEffect, useMemo, useState } from 'react' | ||
|
||
import { PriceService } from '@/services/price' | ||
import useIsMounted from '@/hooks/useIsMounted' | ||
|
||
interface IAppContext { | ||
priceTHB: number | ||
priceUSD: number | ||
} | ||
|
||
export const AppContext = createContext<IAppContext>({ | ||
priceTHB: 0, | ||
priceUSD: 0, | ||
}) | ||
|
||
interface IAppContextProviderProps extends PropsWithChildren {} | ||
|
||
export const AppContextProvider: FC<IAppContextProviderProps> = ({ children }) => { | ||
const isMounted = useIsMounted() | ||
const [priceTHB, setPriceTHB] = useState(0) | ||
const [priceUSD, setPriceUSD] = useState(0) | ||
|
||
const fetchPrice = async () => { | ||
try { | ||
const res = await PriceService.getPrice() | ||
setPriceTHB(res?.bitcoin['thb'] ?? 0) | ||
setPriceUSD(res?.bitcoin['usd'] ?? 0) | ||
} catch (e) { | ||
console.error('error:', e) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (!isMounted) return | ||
|
||
fetchPrice() | ||
}, [isMounted]) | ||
|
||
const value: IAppContext = useMemo(() => ({ priceTHB, priceUSD }), [priceTHB, priceUSD]) | ||
|
||
return <AppContext.Provider value={value}>{children}</AppContext.Provider> | ||
} | ||
|
||
export const useAppContext = () => useContext(AppContext) |
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,9 @@ | ||
import { AxiosRequestConfig } from 'axios' | ||
|
||
import BaseService from './base' | ||
|
||
export class PriceService extends BaseService { | ||
static getPrice(config: AxiosRequestConfig = {}): Promise<GetPriceResult> { | ||
return this._post('/price.get', {}, config) | ||
} | ||
} |
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,3 @@ | ||
interface GetPriceResult { | ||
bitcoin: Record<string, number> | ||
} |