Skip to content
Snippets Groups Projects
Commit 9ef74c8a authored by Tini Tran's avatar Tini Tran
Browse files

Using the hook and fetching data from api call to the view

parent 60d815fd
No related branches found
No related tags found
1 merge request!21Resolve "Consumption page functionality"
Pipeline #277656 passed
<script setup lang="ts"> <script setup lang="ts">
import { computed, ref } from 'vue' import { computed, onMounted, ref } from 'vue'
import {Pie} from 'vue-chartjs' import { Pie } from 'vue-chartjs'
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js' import { ArcElement, Chart as ChartJS, Colors, Legend, Tooltip } from 'chart.js'
ChartJS.register(ArcElement,Tooltip,Legend)
import TransactionComponent from '@/components/economy/TransactionComponent.vue' import TransactionComponent from '@/components/economy/TransactionComponent.vue'
import ToggleButton from '@/components/economy/ToggleButton.vue' import ToggleButton from '@/components/economy/ToggleButton.vue'
import { getTransactions } from '@/utils/TransactionUtils'
ChartJS.register(ArcElement,Tooltip,Legend, Colors)
const selectedOption = ref<string | null>("") const selectedOption = ref<string | null>("")
...@@ -13,38 +15,24 @@ const selectedOption = ref<string | null>("") ...@@ -13,38 +15,24 @@ const selectedOption = ref<string | null>("")
let pages = 0; let pages = 0;
let currentPage = 0; let currentPage = 0;
const transactions = ref([ const transactions = ref<any[]>([])
{ id: 1, const fetchTransactions = async() => {
title: 'Rema 1000', try{
date: '2022-05-10', transactions.value = await getTransactions(1, 2)
amount: 100, //const response = await getTransactions(1,2)
category: 'Dagligvare' //transactions.value = response.data
}, //fordi akk nå benytter vi av test data
{ id: 2, } catch (e) {
title: 'Trondheim Kino', console.log(e)
date: '2022-05-15', }
amount: 500,
category: 'Underholdning' }
}, onMounted(() => {
{ id: 3, fetchTransactions()
title: 'SIT', })
date: '2022-05-15',
amount: 4450,
category: 'regninger'
},
{ id: 4,
title: 'Superhero Burger',
date: '2022-05-15',
amount: 1500,
category: 'Mat & Restaurant'
},
{ id: 6,
title: 'Kiwi',
date: '2022-05-20',
amount: 100,
category: 'Dagligvare'
},
])
const chartVisible = ref(false) const chartVisible = ref(false)
const toggleChart = (value: boolean) => { const toggleChart = (value: boolean) => {
...@@ -58,7 +46,7 @@ const handleSelectionChange = (value: string | null) => { ...@@ -58,7 +46,7 @@ const handleSelectionChange = (value: string | null) => {
const distinctCategories = computed(() => { const distinctCategories = computed(() => {
const categories = new Set<string>() const categories = new Set<string>()
transactions.value.forEach(transaction => { transactions.value.forEach(transaction => {
categories.add(transaction.category) categories.add(transaction.TransactionCategory.category)
}) })
return Array.from(categories) return Array.from(categories)
}) })
...@@ -71,7 +59,7 @@ const filteredTransactions = computed(() => { ...@@ -71,7 +59,7 @@ const filteredTransactions = computed(() => {
if (selectedOption.value === 'Alle' || !selectedOption.value) { if (selectedOption.value === 'Alle' || !selectedOption.value) {
return transactions.value return transactions.value
} else { } else {
return transactions.value.filter(transaction => transaction.category === selectedOption.value) return transactions.value.filter(transaction => transaction.TransactionCategory.category === selectedOption.value)
} }
}) })
const chartData = computed(() => { const chartData = computed(() => {
...@@ -87,7 +75,9 @@ const chartData = computed(() => { ...@@ -87,7 +75,9 @@ const chartData = computed(() => {
const categoryAmounts: { [key: string]: number } = {}; const categoryAmounts: { [key: string]: number } = {};
transactions.value.forEach(transaction => { transactions.value.forEach(transaction => {
const { category, amount } = transaction; const { TransactionCategory, amount } = transaction;
console.log(TransactionCategory)
const category = TransactionCategory.category
if (category in categoryAmounts) { if (category in categoryAmounts) {
categoryAmounts[category] += amount; categoryAmounts[category] += amount;
} else { } else {
...@@ -101,7 +91,6 @@ const chartData = computed(() => { ...@@ -101,7 +91,6 @@ const chartData = computed(() => {
data.labels.forEach(label => { data.labels.forEach(label => {
data.datasets[0].data.push(categoryAmounts[label]); data.datasets[0].data.push(categoryAmounts[label]);
}); });
console.log(data)
return data; return data;
}) })
...@@ -114,6 +103,40 @@ const getRandomColor = () => { ...@@ -114,6 +103,40 @@ const getRandomColor = () => {
} }
return color return color
} }
//
// const transactions1 = ref([
// { id: 1,
// title: 'Rema 1000',
// date: '2022-05-10',
// amount: 100,
// category: 'Dagligvare'
// },
// { id: 2,
// title: 'Trondheim Kino',
// date: '2022-05-15',
// amount: 500,
// category: 'Underholdning'
// },
// { id: 3,
// title: 'SIT',
// date: '2022-05-15',
// amount: 4450,
// category: 'regninger'
// },
// { id: 4,
// title: 'Superhero Burger',
// date: '2022-05-15',
// amount: 1500,
// category: 'Mat & Restaurant'
// },
// { id: 6,
// title: 'Kiwi',
// date: '2022-05-20',
// amount: 100,
// category: 'Dagligvare'
// },
// ])
</script> </script>
<template> <template>
...@@ -133,12 +156,12 @@ const getRandomColor = () => { ...@@ -133,12 +156,12 @@ const getRandomColor = () => {
<option v-for="option in dropdownOptions" :key="option" :value="option">{{ option }}</option> <option v-for="option in dropdownOptions" :key="option" :value="option">{{ option }}</option>
</select> </select>
</div> </div>
<div class="component-container"> <div class="component-container" v-if="filteredTransactions">
<transaction-component <transaction-component
v-for="transaction in filteredTransactions" v-for="transaction in filteredTransactions"
:key="transaction.id" :key="transaction.transactionId"
:title="transaction.title" :title="transaction.transactionTitle"
:category="transaction.category" :category="transaction.TransactionCategory.category"
:amount="transaction.amount" :amount="transaction.amount"
:date="transaction.date" :date="transaction.date"
></transaction-component> ></transaction-component>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment