Skip to content

Commit

Permalink
UI: improved display of prices and currencies (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwitkowski authored Aug 4, 2024
1 parent 7447d50 commit 6e2a398
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
16 changes: 7 additions & 9 deletions ui/src/components/OfferThumb.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
<small>{{ offer.date }}, {{ offer.location }}</small>
,
<small>
<strong>
{{ offer.price_in_euro }}
<div v-if="offer.currency_code != 'EUR'" class="tooltip">
<span class="tooltiptext">converted from {{ offer.price }} {{ offer.currency }}</span>
</div>
<div v-else style="display: inline">€</div>
</strong>
<strong>{{ formatPrice(offer.price, offer.currency_code) }}</strong>
</small>
</p>
<small>
Expand All @@ -26,7 +19,7 @@
params: { aircraftType: offer.aircraft_type, manufacturer: offer.manufacturer, model: offer.model }
}"
>
<a>Model: {{ offer.manufacturer }} {{ offer.model }}</a>
<a>all `{{ offer.model }}` offers</a>
</router-link>
</p>
</small>
Expand All @@ -42,13 +35,18 @@
</template>

<script>
import formatPrice from '@/utils.js'
export default {
name: 'OfferThumb',
props: {
offer: {
type: Object,
default: null
}
},
methods: {
formatPrice
}
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/__tests__/OfferThumb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ describe('OfferThumb', () => {

const wrapper = mount(OfferThumb, { props: { offer: offer } })

expect(wrapper.text()).toContain('Rolladen Schneider LS4', '2020-02-26')
expect(wrapper.text()).toContain('LS4', '2020-02-26', '€28,000')
})
})
12 changes: 12 additions & 0 deletions ui/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function formatPrice(input, currency) {
try {
let n = Number(input)
return n.toLocaleString(navigator.languages[0] || 'en', {
style: 'currency',
currency: currency || 'EUR',
maximumFractionDigits: 0
})
} catch {
return input
}
}
10 changes: 6 additions & 4 deletions ui/src/views/OfferDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<chartist type="Line" ratio=".ct-chart" :data="chartData" :options="chartOptions" />
</div>
<h2>Offers</h2>
<p>There were {{ offers.length }} offer(s). The average offer price is {{ avgPrice }} €.</p>
<p>There were {{ offers.length }} offer(s). The average offer price is {{ formatPrice(avgPrice, 'EUR') }}.</p>
<table class="modelinformation-table">
<tr>
<th>Date</th>
Expand All @@ -25,7 +25,7 @@
<td>{{ offer.date }}</td>
<td>{{ offer.title }}</td>
<td>{{ offer.location }}</td>
<td>{{ offer.price_in_euro }} €</td>
<td>{{ formatPrice(offer.price, offer.currency_code) }}</td>
<td>
<div class="icon">
<small>
Expand All @@ -45,6 +45,7 @@
import axios from 'axios'
import moment from 'moment'
import ChartistTooltip from 'chartist-plugin-tooltips-updated'
import formatPrice from '@/utils.js'
export default {
name: 'OfferDetails',
Expand Down Expand Up @@ -87,6 +88,7 @@ export default {
},
methods: {
formatPrice,
dateAlreadyPresent(date) {
for (let j = 0; j < this.chartData.series[0].length; j++) {
if (moment(this.chartData.series[0][j].x).isSame(date, 'day')) {
Expand Down Expand Up @@ -117,7 +119,7 @@ export default {
const datapoint = {
meta: offer.title,
x: new Date(offer.date),
y: offer.price_in_euro
y: Number(offer.price_in_euro)
}
if (this.dateAlreadyPresent(datapoint.x)) {
this.chartData.series.push([datapoint])
Expand All @@ -139,7 +141,7 @@ export default {
plugins: [
this.$chartist.plugins.tooltip({
transformTooltipTextFnc: (datapoint) => {
return datapoint.split(',')[1] + ''
return formatPrice(datapoint.split(',')[1], 'EUR')
}
})
]
Expand Down

0 comments on commit 6e2a398

Please sign in to comment.