Views: Updated for (async) computed properties

This commit is contained in:
Jörn-Michael Miehe 2019-05-14 16:20:38 +02:00
parent 48b1a9bbc3
commit 2b304ca407
2 changed files with 117 additions and 109 deletions

View file

@ -24,18 +24,22 @@ export default {
RegisterForm RegisterForm
}, },
computed: {
session: () => Cookies.get('session')
},
mounted() { mounted() {
if (Cookies.get('session')) { this.$nextTick(() => {
axios axios
.post('/user/login', { .post('/user/login', {
session: Cookies.get('session') session: this.session
}) })
.then(response => { .then(response => {
if (response.data.success) { if (response.data.success) {
this.$router.push({ name: 'usercp' }) this.$router.push({ name: 'usercp' })
} }
}) })
} })
} }
} }
</script> </script>

View file

@ -2,27 +2,31 @@
<v-container> <v-container>
<Header /> <Header />
<p>user logged in: {{ user.login }}</p> <template v-if="user">
<p>user logged in: {{ user.login }}</p>
<v-expansion-panel> <template v-if="decks">
<v-expansion-panel-content v-for="deck in decks" :key="deck.id"> <v-expansion-panel>
<template v-slot:header> <v-expansion-panel-content v-for="deck in decks" :key="deck.id">
<div>{{ deck.content.name }}</div> <template v-slot:header>
</template> <div>{{ deck.content.name }}</div>
<v-card> </template>
<v-card-text> <v-card>
{{ deck.content.note }} <v-card-text>
<ul> {{ deck.content.note }}
<li v-for="card in deck.content.cards" :key="card.serial"> <ul>
{{ card.serial }}: {{ cardsdb[card.serial].name }} <li v-for="card in deck.content.cards" :key="card.serial">
</li> {{ card.serial }}: {{ cardsdb[card.serial].name }}
</ul> </li>
</v-card-text> </ul>
</v-card> </v-card-text>
</v-expansion-panel-content> </v-card>
</v-expansion-panel> </v-expansion-panel-content>
</v-expansion-panel>
</template>
<v-btn @click.native="logout">Logout</v-btn> <v-btn @click.native="logout">Logout</v-btn>
</template>
</v-container> </v-container>
</template> </template>
@ -40,11 +44,6 @@ export default {
Header Header
}, },
data: () => ({
user: '',
decks: ''
}),
methods: { methods: {
goHome() { goHome() {
Cookies.remove('session') Cookies.remove('session')
@ -61,10 +60,45 @@ export default {
this.goHome() this.goHome()
} }
}) })
}
},
computed: {
session: () => Cookies.get('session'),
cardsdb: () => CardsDB
},
asyncComputed: {
user() {
return axios
.post('/user/info', {
session: this.session
})
.then(response => {
if (response.data.success) {
return response.data.user
} else {
this.goHome()
return null
}
})
}, },
parseDecklist() { decks() {
let fileContent = `--Generated By FF Decks (www.ffdecks.com)-- return axios
.post('/decks/list', {
session: this.session
})
.then(response => {
if (response.data.success) {
return response.data.decks
}
})
}
},
mounted() {
let fileContent = `--Generated By FF Decks (www.ffdecks.com)--
Deck Name: Ashe turbo Deck Name: Ashe turbo
Created by: hawks1997 Created by: hawks1997
@ -97,91 +131,61 @@ Backup(16):
Monster(0):` Monster(0):`
// select the line containing 'deck name:' // select the line containing 'deck name:'
// and its successor // and its successor
let metaRE = /^deck name: (.+)$[\s]*?^(.+)$/im let metaRE = /^deck name: (.+)$[\s]*?^(.+)$/im
let metaData = metaRE.exec(fileContent) let metaData = metaRE.exec(fileContent)
// extract matches // extract matches
let deckData = { let deckData = {
name: metaData[1], name: metaData[1],
note: metaData[2], note: metaData[2],
cards: [] cards: []
}
// select all lines containing card serial numbers
let cardLinesRE = /^.*\b\d-\d{3}[A-Z]?\b.*$/gm
let cardLines = fileContent.match(cardLinesRE)
cardLines.forEach(cardLine => {
// extract serial (guaranteed to be in here!)
let serialRE = /\b(\d-\d{3})[A-Z]?\b/i
let serial = serialRE.exec(cardLine)[1]
// strip out serial number
cardLine = cardLine.replace(serialRE, '')
let countREs = [
// prioritize a count with "times" symbol *, x, ×
/\b([1-3])(?:[*×]|[x]\b)/,
/(?:[*×]|\b[x])([1-3])\b/,
// next priority: with whitespace
/\s+([1-3])\s+/,
/\s+([1-3])\b/,
/\b([1-3])\s+/,
// least priority: any single digit
/\b([1-3])\b/
]
// fallback value
let count = '0'
for (let i = 0; i < countREs.length; i++) {
let data = countREs[i].exec(cardLine)
if (data) {
count = data[1]
break
}
} }
// select all lines containing card serial numbers // push card data into deck
let cardLinesRE = /^.*\b\d-\d{3}[A-Z]?\b.*$/gm deckData.cards.push({
let cardLines = fileContent.match(cardLinesRE) serial: serial,
count: count
cardLines.forEach(cardLine => {
// extract serial (guaranteed to be in here!)
let serialRE = /\b(\d-\d{3})[A-Z]?\b/i
let serial = serialRE.exec(cardLine)[1]
// strip out serial number
cardLine = cardLine.replace(serialRE, '')
let countREs = [
// prioritize a count with "times" symbol *, x, ×
/\b([1-3])(?:[*×]|[x]\b)/,
/(?:[*×]|\b[x])([1-3])\b/,
// next priority: with whitespace
/\s+([1-3])\s+/,
/\s+([1-3])\b/,
/\b([1-3])\s+/,
// least priority: any single digit
/\b([1-3])\b/
]
// fallback value
let count = '0'
for (let i = 0; i < countREs.length; i++) {
let data = countREs[i].exec(cardLine)
if (data) {
count = data[1]
break
}
}
// push card data into deck
deckData.cards.push({
serial: serial,
count: count
})
}) })
})
console.log(deckData) console.log(deckData)
}
},
computed: {
cardsdb: () => CardsDB
},
mounted() {
this.parseDecklist()
axios
.post('/user/info', {
session: Cookies.get('session')
})
.then(response => {
if (response.data.success) {
this.user = response.data.user
} else {
this.goHome()
}
})
axios
.post('/decks/list', {
session: Cookies.get('session')
})
.then(response => {
if (response.data.success) {
this.decks = response.data.decks
}
})
} }
} }
</script> </script>