flaschengeist-frontend/src/views/BarView.vue

108 lines
4.0 KiB
Vue
Raw Normal View History

2019-12-21 07:20:25 +00:00
<template>
<div>
<TitleBar/>
<v-content>
<SearchBar @add:creditList="addCreditList"/>
<CreditLists v-bind:users="users" @add:amount="addAmount"></CreditLists>
</v-content>
2019-12-21 07:20:25 +00:00
</div>
</template>
<script>
import TitleBar from "@/components/TitleBar";
import CreditLists from "@/components/baruser/CreditLists";
2019-12-21 11:22:21 +00:00
import httpClient from "../plugins/restService";
2019-12-21 12:16:37 +00:00
// eslint-disable-next-line no-unused-vars
2019-12-21 11:22:21 +00:00
import axios from "axios";
import SearchBar from "../components/baruser/SearchBar";
2019-12-21 07:20:25 +00:00
export default {
name: "BarView",
components: {SearchBar, CreditLists, TitleBar},
2019-12-21 07:20:25 +00:00
created() {
this.getUser()
},
2019-12-21 11:22:21 +00:00
data () {
return {
users: []
}
},
2019-12-21 07:20:25 +00:00
methods: {
getUser() {
2019-12-21 12:16:37 +00:00
this. users = []
2019-12-21 11:22:21 +00:00
httpClient.getUserBar(this.$store.getters.getToken)
2019-12-21 07:20:25 +00:00
.then(response => {
2019-12-21 11:22:21 +00:00
for (let user in response.data) {
const lastId = this.users.length > 0 ? this.users[this.users.length - 1].id : 0
this.users.push({
id: lastId + 1,
username: response.data[user].username,
firstname: response.data[user].firstname,
lastname: response.data[user].lastname,
locked: response.data[user].locked,
2019-12-21 12:16:37 +00:00
amount: response.data[user].amount,
type: response.data[user].type
2019-12-21 11:22:21 +00:00
})
}
2019-12-21 07:20:25 +00:00
})
.catch(error => {
if (error.response) {
if (error.response.status === 401) {
this.$store.dispatch('logout')
}
}
2019-12-21 11:22:21 +00:00
this.users = []
})
this.users = this.users.sort((a,b) => {
if (a.username > b.username) return 1
if (a.username < b.username) return -1
return 0
})
2019-12-21 11:22:21 +00:00
},
2019-12-21 12:16:37 +00:00
// eslint-disable-next-line no-unused-vars
2019-12-21 11:22:21 +00:00
addAmount(username, amount) {
2019-12-21 12:16:37 +00:00
httpClient.addAmountBar(this.$store.getters.getToken, {userId: username, amount: amount})
.then((response) => {
let user = this.users.find(user => {
return user.username === username ? user : false
})
user.amount = response.data.amount
user.locked = response.data.locked
user.type = response.data.type
2019-12-21 07:20:25 +00:00
})
.catch(error => {
if (error.response) {
if (error.response.status === 401) {
this.$store.dispatch('logout')
}
}
this.users = []
})
},
addCreditList(user) {
if (!this.users.find(user1 => {
return user1.username === user.username
})) {
httpClient.getOneUserBar(this.$store.getters.getToken, {userId: user.username})
.then(response => {
const lastId = this.users.length > 0 ? this.users[this.users.length - 1].id : 0
this.users.push({
id: lastId + 1,
username: response.data.username,
firstname: response.data.firstname,
lastname: response.data.lastname,
locked: response.data.locked,
amount: response.data.amount,
type: response.data.type
})
})
}
2019-12-21 07:20:25 +00:00
}
}
}
</script>
<style scoped>
</style>