Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
9bb47004
Commit
9bb47004
authored
Dec 05, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor currency value
parent
8e703560
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
21 deletions
+33
-21
getCurrencyValue.ts
lib/getCurrencyValue.ts
+29
-0
CurrencyValue.tsx
ui/shared/CurrencyValue.tsx
+4
-21
No files found.
lib/getCurrencyValue.ts
0 → 100644
View file @
9bb47004
import
BigNumber
from
'
bignumber.js
'
;
interface
Params
{
value
:
string
;
exchangeRate
?:
string
|
null
;
accuracy
?:
number
;
accuracyUsd
?:
number
;
decimals
?:
string
|
null
;
}
export
default
function
getCurrencyValue
({
value
,
accuracy
,
accuracyUsd
,
decimals
,
exchangeRate
}:
Params
)
{
const
valueCurr
=
BigNumber
(
value
).
div
(
BigNumber
(
10
**
Number
(
decimals
||
'
18
'
)));
const
valueResult
=
accuracy
?
valueCurr
.
dp
(
accuracy
).
toFormat
()
:
valueCurr
.
toFormat
();
let
usdResult
:
string
|
undefined
;
if
(
exchangeRate
)
{
const
exchangeRateBn
=
new
BigNumber
(
exchangeRate
);
const
usdBn
=
valueCurr
.
times
(
exchangeRateBn
);
if
(
accuracyUsd
&&
!
usdBn
.
isEqualTo
(
0
))
{
const
usdBnDp
=
usdBn
.
dp
(
accuracyUsd
);
usdResult
=
usdBnDp
.
isEqualTo
(
0
)
?
usdBn
.
precision
(
accuracyUsd
).
toFormat
()
:
usdBnDp
.
toFormat
();
}
else
{
usdResult
=
usdBn
.
toFormat
();
}
}
return
{
valueStr
:
valueResult
,
usd
:
usdResult
};
}
ui/shared/CurrencyValue.tsx
View file @
9bb47004
import
{
Box
,
Text
,
chakra
}
from
'
@chakra-ui/react
'
;
import
BigNumber
from
'
bignumber.js
'
;
import
React
from
'
react
'
;
import
getCurrencyValue
from
'
lib/getCurrencyValue
'
;
interface
Props
{
value
:
string
;
currency
?:
string
;
...
...
@@ -20,32 +21,14 @@ const CurrencyValue = ({ value, currency = '', decimals, exchangeRate, className
</
Box
>
);
}
const
valueCurr
=
BigNumber
(
value
).
div
(
BigNumber
(
10
**
Number
(
decimals
||
'
18
'
)));
const
valueResult
=
accuracy
?
valueCurr
.
dp
(
accuracy
).
toFormat
()
:
valueCurr
.
toFormat
();
let
usdContent
;
if
(
exchangeRate
!==
undefined
&&
exchangeRate
!==
null
)
{
const
exchangeRateBn
=
new
BigNumber
(
exchangeRate
);
const
usdBn
=
valueCurr
.
times
(
exchangeRateBn
);
let
usdResult
:
string
;
if
(
accuracyUsd
&&
!
usdBn
.
isEqualTo
(
0
))
{
const
usdBnDp
=
usdBn
.
dp
(
accuracyUsd
);
usdResult
=
usdBnDp
.
isEqualTo
(
0
)
?
usdBn
.
precision
(
accuracyUsd
).
toFormat
()
:
usdBnDp
.
toFormat
();
}
else
{
usdResult
=
usdBn
.
toFormat
();
}
usdContent
=
(
<
Text
as=
"span"
variant=
"secondary"
fontWeight=
{
400
}
>
($
{
usdResult
}
)
</
Text
>
);
}
const
{
valueStr
:
valueResult
,
usd
:
usdResult
}
=
getCurrencyValue
({
value
,
accuracy
,
accuracyUsd
,
exchangeRate
,
decimals
});
return
(
<
Box
as=
"span"
className=
{
className
}
display=
"inline-flex"
rowGap=
{
3
}
columnGap=
{
1
}
>
<
Text
display=
"inline-block"
>
{
valueResult
}{
currency
?
` ${ currency }`
:
''
}
</
Text
>
{
usd
Content
}
{
usd
Result
&&
<
Text
as=
"span"
variant=
"secondary"
fontWeight=
{
400
}
>
($
{
usdResult
}
)
</
Text
>
}
</
Box
>
);
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment