-
-
Save mlvrkhn/739276c3732b28a7f2f0e00fb2df589d to your computer and use it in GitHub Desktop.
ChartBestSellingProducts
This file contains hidden or 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
<template> | |
<apexchart | |
width="100%" | |
title="Sales last week" | |
type="bar" | |
:series="series" | |
:options="chartOptions" | |
></apexchart> | |
</template> | |
<script lang="ts"> | |
import { Vue, Component, Watch } from 'vue-property-decorator' | |
import { providerChartProductsOptions } from '~/components/options/chartOptions' | |
import StatsServiceAPI from '~/services/StatsServiceAPI' | |
import { ProviderState } from '~/types/store/providers.types' | |
import { TopProduct } from '~/types/store/orders.types' | |
@Component | |
export default class ProvidersChartSales extends Vue { | |
/* Props */ | |
/* State */ | |
/* Data */ | |
chartOptions = providerChartProductsOptions | |
series: ApexAxisChartSeries = [ | |
{ | |
name: 'Series 2', | |
data: [], | |
}, | |
] | |
/* Hooks */ | |
created(): void { | |
this.fetchProvidersBestSellingProducts() | |
} | |
/* Methods */ | |
async fetchProvidersBestSellingProducts() { | |
if (this.currentProviderID !== null) { | |
try { | |
const { data } = await StatsServiceAPI.getProviderBestSellingProducts( | |
this.$axios, | |
this.currentProviderID | |
) | |
if (!data) return | |
this.updateChart(data) | |
} catch (error) { | |
console.error('Error fetching Products', error) | |
} | |
} | |
} | |
updateChart(data: TopProduct[]) { | |
const newSeries: number[] = [] | |
const newLabels: string[] = [] | |
data.forEach((order: TopProduct): void => { | |
newSeries.push(order.quantity) | |
newLabels.push(order.name) | |
}) | |
this.series = [ | |
{ | |
name: 'providers-chart-sales', | |
data: newSeries, | |
}, | |
] | |
this.chartOptions = { | |
xaxis: { | |
categories: newLabels, | |
}, | |
} | |
} | |
/* Computed */ | |
get providerState(): ProviderState { | |
return this.$store.state.providers | |
} | |
get currentProviderID(): number { | |
return this.providerState.currentProvider.id | |
} | |
/* Watchers */ | |
@Watch('currentProviderID') | |
onProviderChange() { | |
console.log( | |
'curr prov changed in PROVIDERS CHART PRODUCTS BEST', | |
this.currentProviderID | |
) | |
this.fetchProvidersBestSellingProducts() | |
} | |
} | |
</script> | |
<style scoped></style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment