Api call from component itself and linting.

This commit is contained in:
2021-02-18 21:40:40 +01:00
parent eb9e7d4b43
commit cded690fba
2 changed files with 59 additions and 62 deletions

View File

@@ -14,20 +14,25 @@
<h4 class="margin-bottom-0">Vinnende farger:</h4> <h4 class="margin-bottom-0">Vinnende farger:</h4>
<div class="raffle-container el-spacing"> <div class="raffle-container el-spacing">
<div class="raffle-element" :class="color + `-raffle`" v-for="[color, occurences] in Object.entries(winningColors)" :key="color"> <div
class="raffle-element"
:class="color + `-raffle`"
v-for="[color, occurences] in Object.entries(winningColors)"
:key="color"
>
{{ occurences }} {{ occurences }}
</div> </div>
</div> </div>
<h4 class="el-spacing">Flasker vunnet:</h4> <h4 class="el-spacing">Flasker vunnet:</h4>
<div v-for="win in winner.highscore" :key="win._id"> <div v-for="win in winner.wins" :key="win._id">
<router-link :to="winDateUrl(win.date)" class="days-ago"> <router-link :to="winDateUrl(win.date)" class="days-ago">
{{ humanReadableDate(win.date) }} - {{ daysAgo(win.date) }} dager siden {{ humanReadableDate(win.date) }} - {{ daysAgo(win.date) }} dager siden
</router-link> </router-link>
<div class="won-wine"> <div class="won-wine" v-if="win.wine">
<img :src="smallerWineImage(win.wine.image)"> <img :src="smallerWineImage(win.wine.image)" />
<div class="won-wine-details"> <div class="won-wine-details">
<h3>{{ win.wine.name }}</h3> <h3>{{ win.wine.name }}</h3>
@@ -38,6 +43,11 @@
<div class="raffle-element small" :class="win.color + `-raffle`"></div> <div class="raffle-element small" :class="win.color + `-raffle`"></div>
</div> </div>
<div class="won-wine" v-else>
<div class="won-wine-details">
<h3>Oisann! Klarte ikke finne vin.</h3>
</div>
</div>
</div> </div>
</section> </section>
@@ -49,67 +59,71 @@
</template> </template>
<script> <script>
import { getWinnerByName } from "@/api"; import { dateString, humanReadableDate, daysAgo } from "@/utils";
import { humanReadableDate, daysAgo } from "@/utils";
export default { export default {
data() { data() {
return { return {
winner: undefined, winner: undefined,
name: undefined,
error: undefined, error: undefined,
previousRoute: { previousRoute: {
default: true, default: true,
name: "topplisten", name: "topplisten",
path: "/highscore" path: "/highscore"
} }
} };
}, },
beforeRouteEnter(to, from, next) { beforeRouteEnter(to, from, next) {
next(vm => { next(vm => {
if (from.name != null) if (from.name != null) vm.previousRoute = from;
vm.previousRoute = from });
})
}, },
computed: { computed: {
numberOfWins() { numberOfWins() {
return this.winner.highscore.length return this.winner.wins.length;
} }
}, },
created() { created() {
const nameFromURL = this.$route.params.name; this.name = this.$route.params.name;
getWinnerByName(nameFromURL) this.getWinnerByName(this.name)
.then(winner => this.setWinner(winner)) .then(winner => this.setWinner(winner))
.catch(err => this.error = `Ingen med navn: "${nameFromURL}" funnet.`) .catch(err => (this.error = `Ingen med navn: "${nameFromURL}" funnet.`));
}, },
methods: { methods: {
getWinnerByName(name) {
return fetch(`/api/history/by-name/${name}`)
.then(resp => resp.json())
.then(response => response.winner);
},
setWinner(winner) { setWinner(winner) {
this.winner = { this.winner = {
name: winner.name, name: winner.name,
highscore: [], highscore: [],
...winner ...winner
} };
this.winningColors = this.findWinningColors() this.winningColors = this.findWinningColors();
}, },
smallerWineImage(image) { smallerWineImage(image) {
if (image && image.includes(`515x515`)) if (image && image.includes(`515x515`)) return image.replace(`515x515`, `175x175`);
return image.replace(`515x515`, `175x175`) if (image && image.includes(`500x500`)) return image.replace(`500x500`, `175x175`);
return image return image;
}, },
findWinningColors() { findWinningColors() {
const colors = this.winner.highscore.map(win => win.color) const colors = this.winner.wins.map(win => win.color);
const colorOccurences = {} const colorOccurences = {};
colors.forEach(color => { colors.forEach(color => {
if (colorOccurences[color] == undefined) { if (colorOccurences[color] == undefined) {
colorOccurences[color] = 1 colorOccurences[color] = 1;
} else { } else {
colorOccurences[color] += 1 colorOccurences[color] += 1;
} }
}) });
return colorOccurences return colorOccurences;
}, },
winDateUrl(date) { winDateUrl(date) {
const timestamp = new Date(date).getTime(); const dateParameter = dateString(new Date(date));
return `/history/${timestamp}` return `/history/${dateParameter}`;
}, },
navigateBack() { navigateBack() {
if (this.previousRoute.default) { if (this.previousRoute.default) {
@@ -121,7 +135,7 @@ export default {
humanReadableDate: humanReadableDate, humanReadableDate: humanReadableDate,
daysAgo: daysAgo daysAgo: daysAgo
} }
} };
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@@ -142,7 +156,7 @@ $elementSpacing: 3rem;
} }
.container { .container {
width: 90vw; width: 90vw;
margin: 3rem auto; margin: 3rem auto;
margin-bottom: 0; margin-bottom: 0;
padding-bottom: 3rem; padding-bottom: 3rem;
@@ -233,7 +247,7 @@ h1 {
@include tablet { @include tablet {
width: calc(100% - 160px - 80px); width: calc(100% - 160px - 80px);
} }
& > * { & > * {
width: 100%; width: 100%;
} }
@@ -259,10 +273,9 @@ h1 {
} }
} }
.backdrop { .backdrop {
$background: rgb(244,244,244); $background: rgb(244, 244, 244);
--padding: 2rem; --padding: 2rem;
@include desktop { @include desktop {
--padding: 5rem; --padding: 5rem;
@@ -270,4 +283,4 @@ h1 {
background-color: $background; background-color: $background;
padding: var(--padding); padding: var(--padding);
} }
</style> </style>

View File

@@ -23,7 +23,9 @@ export default {
}; };
}, },
async mounted() { async mounted() {
prelottery().then(wines => this.wines = wines); fetch("/api/lottery/wines")
.then(resp => resp.json())
.then(response => (this.wines = response.wines));
} }
}; };
</script> </script>
@@ -42,19 +44,18 @@ h1 {
} }
.wines-container { .wines-container {
display: flex; width: 90vw;
flex-wrap: wrap; padding: 5vw;
justify-content: space-evenly;
margin: 0 2rem; @include desktop {
width: 80vw;
padding: 0 10vw;
}
@media (min-width: 1500px) { @media (min-width: 1500px) {
max-width: 1500px; max-width: 1500px;
margin: 0 auto; margin: 0 auto;
} }
@include mobile {
flex-direction: column;
}
} }
h3 { h3 {
@@ -65,23 +66,6 @@ h3 {
} }
} }
.inner-wine-container {
display: flex;
flex-direction: row;
margin: auto;
width: 500px;
font-family: Arial;
margin-bottom: 30px;
@include desktop {
justify-content: center;
}
@include mobile {
width: auto;
}
}
.right { .right {
display: flex; display: flex;
flex-direction: column; flex-direction: column;