Source: Pages/DashBoard/DashBoard.js

  1. import './DashBoard.css';
  2. import dayjs from 'dayjs';
  3. import { BarChart } from "../../Components/BarChart";
  4. import { DatePicker } from '@mui/x-date-pickers/DatePicker';
  5. import { LocalizationProvider } from '@mui/x-date-pickers';
  6. import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
  7. import { useState, useEffect } from "react";
  8. import {
  9. SESSION,
  10. BUDGET,
  11. getExpensesAsDateObject,
  12. getIncomesAsDateObject,
  13. getExpensesAsDateList,
  14. getIncomesAsDateList,
  15. getTransactionDatesList
  16. } from '../../Session/Session';
  17. /**
  18. * DashBoard component which represents the landing-page after
  19. * a successful user authentication. It displays a short summary
  20. * from a given date regarding the status of the budget as well
  21. * as what transactions which was made at the given date.
  22. *
  23. * The data is displayed through regular text and through the
  24. * BarChart component which shows the sum of incomes vs. the
  25. * sum of expenses. The date which are to be summarised can be
  26. * picked using the DatePicker component on the top of the page.
  27. */
  28. export function DashBoard() {
  29. /** String representation of the given date. */
  30. const [strDate, setStrDate] = useState('2023-02-10');
  31. /* Dayjs Date representation of the given date. */
  32. const [date, setDate] = useState(dayjs(strDate));
  33. /* Sum of expenses at the given date. */
  34. const [expenses, setExpenses] = useState(0);
  35. /* Sum of incomes at the given date. */
  36. const [incomes, setIncomes] = useState(0);
  37. /* Budget boundary which represents the value which the user wants hold in the budget. */
  38. const [balance, setBalance] = useState(0);
  39. /* Sum of incomes up to the given date to be displayed in the BarChart component. */
  40. const [sumIncome, setSumIncome] = useState(0);
  41. /* Sum of expenses up to the given date to be displayed in the BarChart component. */
  42. const [sumExpense, setSumExpense] = useState(0);
  43. /**
  44. * Gets all transactions labeled as an expense at the given date
  45. * and summarises them. The expenses variable will be set to
  46. * the sum of theese transaction values.
  47. */
  48. const getExpenses = () => {
  49. /** Object containing the expenses at the given date */
  50. const obj = getExpensesAsDateObject();
  51. let sumExpenses = 0;
  52. /* Summing all expenses. */
  53. for (let i = 0; i < obj.dates.length; i++) {
  54. if (obj.dates[i] == strDate) {
  55. sumExpenses += obj.expenses[i];
  56. }
  57. }
  58. /* Updates the expenses variable. */
  59. setExpenses(sumExpenses);
  60. };
  61. /**
  62. * Gets all transactions labeled as an income at the given date
  63. * and summarises them. The incomes variable will be set to
  64. * the sum of theese transaction values.
  65. */
  66. const getIncomes = () => {
  67. /** Object containing the incomes at the given date */
  68. const obj = getIncomesAsDateObject();
  69. let sumIncomes = 0;
  70. /* Summing all incomes. */
  71. for (let i = 0; i < obj.dates.length; i++) {
  72. if (obj.dates[i] == strDate) {
  73. sumIncomes += obj.incomes[i];
  74. }
  75. }
  76. /* Updates the incomes variable. */
  77. setIncomes(sumIncomes);
  78. };
  79. /**
  80. * Fetches all incomes, expenses and a list of transaction dates
  81. * as lists and summarises the income and expenses of the whole
  82. * span of the budget. The sums are used to update the sumIncome
  83. * and sumExpense variables, which again is used to display the
  84. * total income and spendage of the budget up to the given date.
  85. */
  86. const getSumOfTransactionsAndBalance = async () => {
  87. /** List of all income transactions. */
  88. const incomeList = await getIncomesAsDateList();
  89. /** List of all expense transactions. */
  90. const expenseList = await getExpensesAsDateList();
  91. /** List of all dates which transactions has been made. */
  92. const dateList = await getTransactionDatesList();
  93. /** Index of the given date in the datelist. */
  94. const index = dateList.indexOf(strDate);
  95. let sumIncome = 0;
  96. let sumExpense = 0;
  97. /* Summing all incomes and expenses up to the given date. */
  98. for (let i = 0; i <= index; i++) {
  99. sumIncome += incomeList[i];
  100. sumExpense += expenseList[i];
  101. }
  102. /* Updates the sumIncome and sumExpense variables. */
  103. setSumIncome(sumIncome);
  104. setSumExpense(sumExpense);
  105. };
  106. /**
  107. * UseEffect hook which runs on every rendering. It checks
  108. * if the user is authenticated before updating the data to
  109. * be displayed on the budget page.
  110. */
  111. useEffect( () => {
  112. if (SESSION.getAuth()) {
  113. getExpenses();
  114. getIncomes();
  115. getSumOfTransactionsAndBalance();
  116. setBalance(BUDGET.boundary);
  117. }
  118. });
  119. return(
  120. <article>
  121. <header>
  122. <div id="title">
  123. <h1>Dashboard</h1>
  124. </div>
  125. <div id="date">
  126. <LocalizationProvider dateAdapter={AdapterDayjs}>
  127. <DatePicker
  128. label=""
  129. value={date}
  130. onChange={(date) =>
  131. {
  132. setStrDate(date.format('YYYY-MM-DD'));
  133. setDate(date);
  134. }
  135. }
  136. />
  137. </LocalizationProvider>
  138. </div>
  139. </header>
  140. <hr/>
  141. <div id="budget-balance">
  142. <h2>Total available balance</h2>
  143. <p id="budget-balance-placeholder">Kr {balance}</p>
  144. </div>
  145. <hr/>
  146. <div id="budget-summary">
  147. <div id="budget-summary-income">
  148. <h3>Income</h3>
  149. <p id="budget-income-placeholder">Kr {incomes}</p>
  150. </div>
  151. <div id="budget-summary-expense">
  152. <h3>Expense</h3>
  153. <p id="budget-expense-placeholder">Kr {expenses}</p>
  154. </div>
  155. <div id="budget-summary-remaining">
  156. <h3>Budget Remaining</h3>
  157. <p id="budget-remeinder-placeholder">Kr {balance + sumIncome - sumExpense}</p>
  158. </div>
  159. </div>
  160. <hr/>
  161. <h2 id="bar-title">Income & Expense</h2>
  162. <div id="barchart">
  163. <BarChart
  164. income={incomes}
  165. expense = {expenses}
  166. />
  167. </div>
  168. </article>
  169. );
  170. }