Transactions.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <!--
  2. Copyright 2019 Hackware SpA <human@hackware.cl>
  3. This file is part of "Hackware Userland" and licensed under the terms
  4. of the GNU Affero General Public License version 3, or (at your option)
  5. a later version. You should have received a copy of this license along
  6. with the software. If not, see <https://www.gnu.org/licenses/>.
  7. -->
  8. <template>
  9. <div class="transactions">
  10. <h2>Mis transacciones</h2>
  11. <table>
  12. <thead>
  13. <tr>
  14. <th>Estado</th>
  15. <th>Emisión</th>
  16. <th>Descripción</th>
  17. <th>Monto</th>
  18. <th>Balance</th>
  19. </tr>
  20. </thead>
  21. <tbody
  22. v-for="transaction in transactions"
  23. :key="transaction.id"
  24. >
  25. <tr :class="`status-${transaction.status}`">
  26. <td>
  27. <i :class="statusIcon(transaction.status)" />
  28. <template v-if="transaction.status === 'ok'">
  29. {{ okWord() }}
  30. </template>
  31. <template v-else-if="transaction.status === 'due'">
  32. expira el {{ transaction.due_at | date }}
  33. </template>
  34. <template v-else>
  35. expiró el {{ transaction.due_at | date }}
  36. </template>
  37. </td>
  38. <td>{{ transaction.created_at | datetime }}</td>
  39. <td>{{ transaction.description }}</td>
  40. <td>{{ transaction.amount | currency }}</td>
  41. <td>{{ transaction.balance | currency }}</td>
  42. </tr>
  43. </tbody>
  44. </table>
  45. <VPagination
  46. v-if="totalPages"
  47. :total-pages="totalPages"
  48. />
  49. </div>
  50. </template>
  51. <script>
  52. import VPagination from '@/components/VPagination.vue';
  53. export default {
  54. name: 'Transactions',
  55. components: {
  56. VPagination,
  57. },
  58. data() {
  59. return {
  60. transactions: [],
  61. totalPages: null,
  62. };
  63. },
  64. created() {
  65. const page = ('page' in this.$route.query) ? Number(this.$route.query.page) : 1;
  66. this.fetchTransactions(page);
  67. },
  68. methods: {
  69. statusIcon(status) {
  70. let icon;
  71. switch (status) {
  72. case 'ok':
  73. icon = 'fas fa-check-circle';
  74. break;
  75. case 'due':
  76. icon = 'fas fa-exclamation-triangle';
  77. break;
  78. case 'expired':
  79. icon = 'fas fa-skull-crossbones';
  80. break;
  81. default:
  82. icon = 'fas fa-question';
  83. }
  84. return icon;
  85. },
  86. okWord() {
  87. const words = [
  88. 'cachi lupis', 'picho caluga', 'súper', 'OK', 'todo bien', 'no problemo',
  89. ];
  90. return words[Math.floor(Math.random() * words.length)];
  91. },
  92. determineStatus(dueAt) {
  93. if (!dueAt) return 'ok';
  94. const dueDate = new Date(dueAt);
  95. const currentDate = new Date();
  96. if (currentDate.getTime() < dueDate.getTime()) return 'due';
  97. return 'expired';
  98. },
  99. fetchTransactions(page) {
  100. this.$fetcher.haweseGet('/transactions', {
  101. user_uid: this.$auth.uid,
  102. page,
  103. limit: 10,
  104. sort: '-id',
  105. })
  106. .then((body) => {
  107. this.totalPages = body.meta.total_pages;
  108. this.transactions = body.data.map((transaction) => {
  109. const editedTransaction = transaction;
  110. editedTransaction.status = this.determineStatus(transaction.due_at);
  111. return editedTransaction;
  112. });
  113. });
  114. },
  115. },
  116. beforeRouteUpdate(to, from, next) {
  117. if (to.name === 'transactions') {
  118. this.fetchTransactions(to.query.page);
  119. }
  120. next();
  121. },
  122. };
  123. </script>
  124. <style lang="scss">
  125. @import "../assets/bootstrap/custom";
  126. .transactions {
  127. @extend .pt-3;
  128. > table {
  129. @extend .table;
  130. @extend .table-hover;
  131. @extend .table-borderless;
  132. @extend .table-striped;
  133. >tbody > tr {
  134. // &.status-ok { }
  135. &.status-due {
  136. @extend .table-warning;
  137. }
  138. &.status-expired {
  139. @extend .table-danger;
  140. }
  141. }
  142. }
  143. }
  144. </style>