Browse Source

Use single component architecture

Mensa and day are passed as two params to a single component
vuex
Schneider 8 years ago
parent
commit
7f86464980
  1. 1
      .gitignore
  2. 35
      src/App.vue
  3. 39
      src/components/Mensa.vue
  4. 8
      src/lib/Mensen.js
  5. 16
      src/router/index.js

1
.gitignore

@ -5,3 +5,4 @@ npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
static/*mensa*.json static/*mensa*.json
static/*bistro*.json

35
src/App.vue

@ -2,15 +2,22 @@
<div id="app"> <div id="app">
<h1>Essen in Göttingen</h1> <h1>Essen in Göttingen</h1>
<nav>
<router-link v-for="mensa in mensen" :to="'/' + mensa.url + '/'">{{mensa.name | noBreak}}</router-link>
</nav>
Wo? <select v-model="where" @click="updateRoute">
<option v-for="mensa in mensen" :value="mensa.url">{{mensa.name}}</option>
</select>
Wann? <select v-model="when" @click="updateRoute">
<option v-for="day in days" :value="day" :key="day">{{dayOfWeek[day]}}</option>
</select>
<router-view></router-view> <router-view></router-view>
<footer> <footer>
<hr> <hr>
Developed by <a href="https://webschneider.org">webschneider.org</a>
&copy; {{ new Date().getFullYear() }}
<span>
Developed by <a href="https://webschneider.org">webschneider.org</a>
&copy; {{ new Date().getFullYear() }}
</span>
<span><a href="https://webschneider.org/impress">Impressum</a></span>
</footer> </footer>
</div> </div>
</template> </template>
@ -22,7 +29,23 @@ export default {
data () { data () {
return { return {
menu: {}, menu: {},
mensen: Mensen
mensen: Mensen,
days: [1, 2, 3, 4, 5, 6],
dayOfWeek: {
1: 'Montag',
2: 'Dienstag',
3: 'Mittwoch',
4: 'Donnerstag',
5: 'Freitag',
6: 'Samstag'
},
where: 'zentralmensa',
when: '' + new Date().getDay()
}
},
methods: {
updateRoute () {
this.$router.push(`/${this.where}/${this.when}`)
} }
}, },
filters: { filters: {

39
src/components/Mensa.vue

@ -1,9 +1,9 @@
<template> <template>
<div class="hello"> <div class="hello">
<nav>
<router-link v-for="day in days" :to="'/' + $route.params.mensa + '/' + day" :key="day">{{dayOfWeek[day]}}</router-link>
</nav>
<router-view></router-view>
<div class="meal" v-for="meal in meals">
<h3>{{meal.category}}</h3>
<p>{{meal.title}}</p>
</div>
</div> </div>
</template> </template>
@ -12,6 +12,7 @@ export default {
name: 'mensa', name: 'mensa',
data () { data () {
return { return {
meals: [],
days: [1, 2, 3, 4, 5, 6], days: [1, 2, 3, 4, 5, 6],
dayOfWeek: { dayOfWeek: {
1: 'Montag', 1: 'Montag',
@ -31,17 +32,33 @@ export default {
if (!this.$route.params.tag) { if (!this.$route.params.tag) {
this.$router.push('' + new Date().getDay()) this.$router.push('' + new Date().getDay())
} }
this.loadMeals()
} }
}, },
mounted () {
if (!this.$route.fullPath.endsWith('/')) {
this.$router.replace(this.$route.fullPath + '/')
}
if (!this.$route.params.tag) {
this.$router.push('' + new Date().getDay())
methods: {
loadMeals () {
fetch(`/static/${this.$route.params.mensa}.${this.$route.params.tag}.json`)
.then(res => res.json())
.then(menu => { this.meals = menu.meals })
} }
},
created () {
this.$router.replace(`/zentralmensa/${new Date().getDay()}`)
this.loadMeals()
} }
} }
</script> </script>
<style>
.meal{
margin: 2em 0;
}
.meal>h3{
margin-bottom:0em;
}
.meal>p{
margin-top: 0.4em;
}
<style></style>
</style>

8
src/lib/Mensen.js

@ -1,8 +1,4 @@
export default { export default {
nordmensa: {
name: 'Nordmensa',
url: 'nordmensa'
},
zentralmensa: { zentralmensa: {
name: 'Zentralmensa', name: 'Zentralmensa',
url: 'zentralmensa' url: 'zentralmensa'
@ -11,6 +7,10 @@ export default {
name: 'Mensa am Turm', name: 'Mensa am Turm',
url: 'turmmensa' url: 'turmmensa'
}, },
nordmensa: {
name: 'Nordmensa',
url: 'nordmensa'
},
mensaItalia: { mensaItalia: {
name: 'Mensa Italia', name: 'Mensa Italia',
url: 'mensaitalia' url: 'mensaitalia'

16
src/router/index.js

@ -1,8 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import Router from 'vue-router' import Router from 'vue-router'
import Hello from '@/components/Hello'
import Mensa from '@/components/Mensa' import Mensa from '@/components/Mensa'
import Tag from '@/components/Tag'
Vue.use(Router) Vue.use(Router)
@ -11,18 +9,12 @@ export default new Router({
routes: [ routes: [
{ {
path: '/', path: '/',
name: 'Hello',
component: Hello
name: 'Mensa',
component: Mensa
}, },
{ {
path: '/:mensa',
name: 'Mensa',
component: Mensa,
props: {name: 'info'},
children: [{
path: ':tag',
component: Tag
}]
path: '/:mensa/:tag',
component: Mensa
} }
] ]
}) })
Loading…
Cancel
Save