-
-
Notifications
You must be signed in to change notification settings - Fork 361
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
Extend Money Component to show fiat value #175
Changes from 5 commits
2e8d6d3
425657d
6bf5aca
23cf92f
40fad82
22dab32
133c973
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
<template> | ||
<div v-if="!inline">{{value | formatBalance(decimals, unit)}}</div> | ||
<span v-else>{{value | formatBalance(decimals, unit)}}</span> | ||
<div :class="['money', { 'money--inline': inline }]"> | ||
<span> | ||
{{ value | formatBalance(decimals, unit) }} | ||
</span> | ||
<span v-if="fiatValue"> | ||
/ {{ fiatValue | formatBalance(decimals, showFiatValue.toUpperCase()) }} | ||
</span> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Component, Prop, Vue } from 'vue-property-decorator'; | ||
import { mapGetters } from 'vuex' | ||
|
||
import axios from 'axios' | ||
|
||
@Component | ||
export default class Money extends Vue { | ||
@Prop({default: 0}) readonly value: number | string | undefined; | ||
@Prop({default: 0}) readonly value: number | string | undefined | ||
@Prop(Boolean) readonly inline!: boolean | ||
@Prop({default: ''}) readonly showFiatValue!: string | ||
|
||
private readonly apiUrl: string = 'https://api.coingecko.com/api/v3' | ||
private readonly coinId: string = 'kusama' | ||
private fiatValue: number = 0 | ||
|
||
get chainProperties() { | ||
return this.$store.getters.getChainProperties; | ||
return this.$store.getters.getChainProperties | ||
} | ||
|
||
get decimals(): number { | ||
|
@@ -25,6 +35,33 @@ export default class Money extends Vue { | |
return this.chainProperties.tokenSymbol | ||
} | ||
|
||
} | ||
public created() { | ||
if (this.showFiatValue) { | ||
this.getFiatValue() | ||
} | ||
} | ||
|
||
private async getFiatValue() { | ||
try { | ||
const { data } = await axios.get(`${this.apiUrl}/simple/price`, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move this to separate file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I misunderstood it and you wanted to move whole request to separate file just let me know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I just wanted to move it so separate file |
||
params: { | ||
ids: this.coinId, | ||
vs_currencies: this.showFiatValue | ||
} | ||
}) | ||
|
||
this.fiatValue = data[this.coinId][this.showFiatValue] * Number(this.value) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="scss"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow nice ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simple solution 😄 |
||
.money { | ||
&--inline { | ||
display: inline-block; | ||
} | ||
} | ||
</style> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
mounted
instead ofcreated
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed but I don't know why do you want to use
mounted
herewith
created
hook we can get data fasterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always used mounted for fetching. However, it looks like it doesn't matter.
https://stackoverflow.com/a/57089636
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always use
created
for fetching because it's executed before mounted and I don't need DOM elements here