Skip to content

Commit 6298dad

Browse files
committed
Paginação no SPA
1 parent 23df51e commit 6298dad

File tree

9 files changed

+100
-24
lines changed

9 files changed

+100
-24
lines changed

app/Http/Controllers/Api/BankAccountsController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(BankAccountRepository $repository)
3434
*/
3535
public function index()
3636
{
37-
return $this->repository->all();
37+
return $this->repository->paginate(4);
3838
}
3939

4040
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<template>
2+
<ul class="pagination">
3+
<li :class="{'disabled': currentPage == 0}">
4+
<a @click.prevent="previousPage" href="#">
5+
<i class="material-icons">chevron_left</i>
6+
</a>
7+
</li>
8+
<li v-for="o in pages" class="waves-effect" :class="{'active': currentPage == o}">
9+
<a @click.prevent="setCurrentPage(o)" href="#">{{o + 1}}</a>
10+
</li>
11+
<li :class="{'disabled': currentPage == pages - 1}">
12+
<a @click.prevent="nextPage" href="#">
13+
<i class="material-icons">chevron_right</i>
14+
</a>
15+
</li>
16+
</ul>
17+
</template>
18+
<script>
19+
export default{
20+
props: {
21+
currentPage: {
22+
type: Number,
23+
'default': 0
24+
},
25+
perPage: {
26+
type: Number,
27+
required: true
28+
},
29+
totalRecords: {
30+
type: Number,
31+
required: true
32+
}
33+
},
34+
computed: {
35+
pages(){
36+
let pages = Math.ceil(this.totalRecords / this.perPage);
37+
return Math.max(pages, 1);
38+
}
39+
},
40+
methods: {
41+
setCurrentPage(page){
42+
this.currentPage = page;
43+
},
44+
previousPage(){
45+
if(this.currentPage > 0){
46+
this.currentPage--;
47+
}
48+
},
49+
nextPage(){
50+
if(this.currentPage < this.pages - 1){
51+
this.currentPage++;
52+
}
53+
}
54+
},
55+
watch: {
56+
currentPage(newValue){
57+
this.$dispatch('pagination::changed',newValue);
58+
}
59+
}
60+
}
61+
</script>

resources/assets/spa/js/components/Login.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="container">
33
<div class="row">
4-
<div class="col s8 offset-s2 z-depth-2">
4+
<div class="card-panel col s8 offset-s2 z-depth-2">
55
<h3 class="center">Code Financeiro</h3>
66

77
<div class="row" v-if="error.error">

resources/assets/spa/js/components/Logout.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="container">
33
<div class="row">
4-
<div class="col s8 offset-s2 z-depth-2">
4+
<div class="card-panel col s8 offset-s2 z-depth-2">
55
<h5 class="center">Efetuando logout...</h5>
66
<div class="progress">
77
<div class="indeterminate"></div>
@@ -16,14 +16,14 @@
1616
ready(){
1717
setTimeout(() => {
1818
this.logout();
19-
},1000);
19+
}, 1000);
2020
},
2121
methods: {
2222
logout(){
2323
let goToLogin = () => this.$router.go({name: 'auth.login'});
2424
Auth.logout()
25-
.then(goToLogin())
26-
.catch(goToLogin());
25+
.then(goToLogin)
26+
.catch(goToLogin);
2727
}
2828
}
2929
}

resources/assets/spa/js/components/bank-account/BankAccountList.vue

+25-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</thead>
2020
<tbody>
2121
<tr v-for="(index,o) in bankAccounts">
22-
<td>{{index + 1}}</td>
22+
<td>{{o.id}}</td>
2323
<td>{{o.name}}</td>
2424
<td>{{o.agency}}</td>
2525
<td>{{o.account}}</td>
@@ -30,6 +30,7 @@
3030
</tr>
3131
</tbody>
3232
</table>
33+
<pagination :current-page.sync="pagination.current_page" :per-page="pagination.per_page" :total-records="pagination.total"></pagination>
3334
</div>
3435

3536
<div class="fixed-action-btn">
@@ -59,23 +60,28 @@
5960
<script>
6061
import {BankAccount} from '../../services/resources';
6162
import ModalComponent from '../../../../_default/components/Modal.vue';
63+
import PaginationComponent from '../../../../_default/components/Pagination.vue';
6264
export default{
6365
components: {
6466
'modal': ModalComponent,
67+
'pagination': PaginationComponent,
6568
},
6669
data(){
6770
return{
6871
bankAccounts: [],
6972
bankAccountToDelete: null,
7073
modal: {
7174
id: "modal-delete"
75+
},
76+
pagination: {
77+
current_page: 0,
78+
per_page: 0,
79+
total: 0
7280
}
7381
}
7482
},
7583
created(){
76-
BankAccount.query().then((response) => {
77-
this.bankAccounts = response.data.data;
78-
});
84+
this.getBankAccounts();
7985
},
8086
methods: {
8187
destroy(){
@@ -88,6 +94,21 @@
8894
openModalDelete(bankAccount){
8995
this.bankAccountToDelete = bankAccount;
9096
$('#modal-delete').modal('open');
97+
},
98+
getBankAccounts(){
99+
BankAccount.query({
100+
page: this.pagination.current_page + 1
101+
}).then((response) => {
102+
this.bankAccounts = response.data.data;
103+
let pagination = response.data.meta.pagination;
104+
pagination.current_page--;
105+
this.pagination = pagination;
106+
});
107+
}
108+
},
109+
events: {
110+
'pagination::changed'(page){
111+
this.getBankAccounts();
91112
}
92113
}
93114
}

resources/assets/spa/js/services/auth.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ export default {
3939
});
4040
},
4141
logout(){
42-
let afterLogout = () => {
42+
let afterLogout = (response) => {
4343
this.clearAuth();
44+
return response;
4445
};
4546

4647
return JwtToken.revokeToken().
47-
then(afterLogout())
48-
.catch(afterLogout());
48+
then(afterLogout)
49+
.catch(afterLogout);
4950
},
5051
clearAuth(){
5152
this.user.data = null;

resources/assets/spa/js/services/jwt-token.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ export default {
2323
});
2424
},
2525
revokeToken(){
26-
let afterReveokeToken = () => {
26+
let afterReveokeToken = (response) => {
2727
this.token = null;
28+
return response;
2829
};
2930

3031
return Jwt.logout().
31-
then(afterReveokeToken())
32-
.catch(afterReveokeToken());
32+
then(afterReveokeToken)
33+
.catch(afterReveokeToken);
3334
},
3435
getAuthorizationHeader(){
3536
return `Bearer ${LocalStorage.get(TOKEN)}`;

spa.config-compiled.js

-7
This file was deleted.

spa.config-compiled.js.map

-1
This file was deleted.

0 commit comments

Comments
 (0)