diff options
author | Mateja Marić <mail@matejamaric.com> | 2021-03-22 15:21:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-22 15:21:40 +0100 |
commit | 6ab102bc4be617255d5eab77faebb1cada65b370 (patch) | |
tree | 09fcff5953cc6ca3702335e717ca72f865124d1c /resources/js | |
parent | 21e9e94e76a21516edc3d1e4d0462a0ba75aafa4 (diff) | |
parent | db976a9fb0434df0095ea9f1b8858213d57e7535 (diff) | |
download | yota-laravel-2.0.0.tar.gz yota-laravel-2.0.0.zip |
Use Vue.js and Vuex.
Diffstat (limited to 'resources/js')
-rw-r--r-- | resources/js/app.js | 19 | ||||
-rw-r--r-- | resources/js/bootstrap.js | 8 | ||||
-rw-r--r-- | resources/js/components/activities.vue | 53 | ||||
-rw-r--r-- | resources/js/components/call-sign-description.vue | 56 | ||||
-rw-r--r-- | resources/js/components/call-sign-filter.vue | 44 | ||||
-rw-r--r-- | resources/js/components/reservation.vue | 59 | ||||
-rw-r--r-- | resources/js/components/reservations.vue | 62 | ||||
-rw-r--r-- | resources/js/store.js | 96 |
8 files changed, 396 insertions, 1 deletions
diff --git a/resources/js/app.js b/resources/js/app.js index 40c55f6..b5fbbbf 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -1 +1,20 @@ require('./bootstrap'); + +import Vue from 'vue'; + +import store from './store.js'; + +import callSignDescription from './components/call-sign-description.vue'; + +import activitiesView from './components/activities.vue'; +import reservationsView from './components/reservations.vue'; + +new Vue({ + el: '#vue', + store, + components: { + callSignDescription, + activitiesView, + reservationsView + } +}); diff --git a/resources/js/bootstrap.js b/resources/js/bootstrap.js index ffc0a2b..e5e0ab6 100644 --- a/resources/js/bootstrap.js +++ b/resources/js/bootstrap.js @@ -3,7 +3,13 @@ window._ = require('lodash'); window.axios = require('axios'); window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; -window.Vue = require('vue'); +let token = document.querySelector('meta[name="csrf-token"]'); + +if (token) { + window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; +} else { + console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token'); +} window.$ = window.jQuery = require('jquery'); window.Popper = require('popper.js'); diff --git a/resources/js/components/activities.vue b/resources/js/components/activities.vue new file mode 100644 index 0000000..1b1057c --- /dev/null +++ b/resources/js/components/activities.vue @@ -0,0 +1,53 @@ +<template> + <div> + <call-sign-filter :showDescriptions="true" @sign-changed="filterChanged()"></call-sign-filter> + + <div class="table-responsive mt-2"> + <table class="table table-striped table-bordered" style="white-space:nowrap;"> + <thead class="thead-dark"> + <tr> + <th>Operator</th> + <th>From</th> + <th>To</th> + <th>Special Callsign</th> + <th>Frequencies</th> + <th>Modes</th> + <th>QSO</th> + </tr> + </thead> + <tbody> + <tr v-for="(activity, index) in activities" :key="index"> + <td>{{ activity.operatorCall }}</td> + <td>{{ activity.fromTime }}</td> + <td>{{ activity.toTime }}</td> + <td>{{ activity.specialCall }}</td> + <td>{{ activity.frequencies }}</td> + <td>{{ activity.modes }}</td> + <td>{{ activity.qso }}</td> + </tr> + </tbody> + </table> + </div> + </div> +</template> + +<script> +import callSignFilter from './call-sign-filter.vue'; + +export default { + components: { callSignFilter }, + mounted() { + this.$store.dispatch('pullActivities'); + }, + computed: { + activities() { + return this.$store.getters.getData; + } + }, + methods: { + filterChanged() { + this.$store.dispatch('pullActivities'); + } + } +} +</script> diff --git a/resources/js/components/call-sign-description.vue b/resources/js/components/call-sign-description.vue new file mode 100644 index 0000000..7ab8b64 --- /dev/null +++ b/resources/js/components/call-sign-description.vue @@ -0,0 +1,56 @@ +<template> + <div> + <div class="form-group"> + <label for="special-call">Special Callsign:</label> + <select class="form-control" :class="{ 'is-invalid': isInvalid }" id="special-call" v-model="selected" :name="name" required> + <option v-for="option in options" :key="option.id" :value="option.sign" v-text="option.sign"></option> + </select> + </div> + + <div class="card mb-3"> + <div class="card-body pb-1"> + <div class="card-text" v-html="description"></div> + </div> + </div> + </div> +</template> + +<script> +export default { + props: [ 'name', 'old', 'isInvalid' ], + mounted() { + this.$store.dispatch('pullSigns').then(() => { + try { + if (this.old) { + this.$store.dispatch('setSelectedSign', this.old); + } + else { + this.$store.dispatch('setSelectedSign', this.$store.getters.getSigns[0].sign); + } + } + catch { + console.log('No call signs!'); + } + }); + }, + computed: { + options() { + return this.$store.getters.getSigns; + }, + selected: { + get() { + return this.$store.getters.getSelectedSign; + }, + set(value) { + this.$store.dispatch('setSelectedSign', value); + } + }, + description() { + for (let i = 0; i < this.options.length; i++) + if (this.options[i].sign === this.selected) + return this.options[i].description; + return ''; + } + } +} +</script> diff --git a/resources/js/components/call-sign-filter.vue b/resources/js/components/call-sign-filter.vue new file mode 100644 index 0000000..0ba8b4d --- /dev/null +++ b/resources/js/components/call-sign-filter.vue @@ -0,0 +1,44 @@ +<template> + <div> + <label for="call-sign">Filter by special callsign: </label> + <select id="call-sign" v-model="selected"> + <option value="all">All</option> + <option v-for="option in options" :key="option.id" :value="option.sign" v-text="option.sign"></option> + </select> + + <div class="card mb-3" v-if="showDescriptions && (selected !== 'all')"> + <div class="card-body pb-1"> + <div class="card-text" v-html="description"></div> + </div> + </div> + </div> +</template> + +<script> +export default { + props: ['showDescriptions'], + mounted() { + this.$store.dispatch('pullSigns'); + }, + computed: { + selected: { + get() { + return this.$store.getters.getSelectedSign; + }, + set(value) { + this.$store.dispatch('setSelectedSign', value); + this.$emit('sign-changed'); + } + }, + options() { + return this.$store.getters.getSigns; + }, + description() { + for (let i = 0; i < this.options.length; i++) + if (this.options[i].sign === this.selected) + return this.options[i].description; + return ''; + } + } +} +</script> diff --git a/resources/js/components/reservation.vue b/resources/js/components/reservation.vue new file mode 100644 index 0000000..ec430ab --- /dev/null +++ b/resources/js/components/reservation.vue @@ -0,0 +1,59 @@ +<template> + <tr> + <td v-text="reservation.id"></td> + <td><input type="checkbox" v-model="reservation.approved"/></td> + <td><input type="text" v-model="reservation.operatorCall"></td> + <td><input type="text" v-model="reservation.qso"></td> + <td><input type="text" v-model="reservation.fromTime"></td> + <td><input type="text" v-model="reservation.toTime"></td> + <td><input type="text" v-model="reservation.specialCall"></td> + <td><input type="text" v-model="reservation.frequencies"></td> + <td><input type="text" v-model="reservation.modes"></td> + <td><input type="text" v-model="reservation.operatorName"></td> + <td><input type="text" v-model="reservation.operatorEmail"></td> + <td><input type="text" v-model="reservation.operatorPhone"></td> + <td> + <button class="btn btn-primary mr-2" @click="updateRow">Update</button> + <button class="btn btn-warning mr-2" @click="restoreRow">Restore</button> + <button class="btn btn-danger" @click="deleteRow">Delete</button> + </td> + </tr> +</template> + +<script> +export default { + props: [ 'reservationIndex' ], + data() { + return { + reservation: this.$store.getters.getDataRow(this.reservationIndex) + } + }, + methods: { + updateRow() { + this.$store.dispatch('pushReservation', { + index: this.reservationIndex, + reservation: this.reservation + }); + }, + restoreRow() { + this.reservation = this.$store.getters.getDataRow(this.reservationIndex); + }, + deleteRow() { + this.$store.dispatch('removeReservation', this.reservationIndex); + } + } +} +</script> + +<style scoped> +td { + text-align: center; + vertical-align: middle; +} +input { + background-color: white; + border: 1px solid lightgray; + border-radius: 3px; + padding: 0.2em; +} +</style> diff --git a/resources/js/components/reservations.vue b/resources/js/components/reservations.vue new file mode 100644 index 0000000..0c420b8 --- /dev/null +++ b/resources/js/components/reservations.vue @@ -0,0 +1,62 @@ +<template> + <div> + <call-sign-filter @sign-changed="filterChanged()"></call-sign-filter> + + <div class="table-responsive mt-2"> + <table id="ajax-table" class="table table-striped table-bordered" style="white-space:nowrap;"><!-- table-hover --> + <thead class="thead-dark"> + <tr> + <th>ID</th> + <th>Approved</th> + <th>Operator Callsign</th> + <th>QSO</th> + <th>From</th> + <th>To</th> + <th>Special Callsign</th> + <th>Frequencies</th> + <th>Modes</th> + <th>Operator Name</th> + <th>Operator Email</th> + <th>Operator Phone</th> + <th>Actions</th> + </tr> + </thead> + <tbody> + <reservation-view v-for="(reservation, index) in reservations" + :key="reservation.id" :reservation-index="index"> + </reservation-view> + </tbody> + </table> + </div> + </div> +</template> + +<script> +import callSignFilter from './call-sign-filter.vue'; +import reservationView from './reservation.vue'; + +export default { + components: { callSignFilter, reservationView }, + mounted() { + this.$store.dispatch('pullReservations'); + }, + computed: { + reservations() { + return this.$store.getters.getData; + } + }, + methods: { + filterChanged() { + this.$store.dispatch('pullReservations'); + } + } +} +</script> + +<style scoped> +@media only screen and (min-width:961px) { + .table-responsive { + max-height: 80vh; + } +} +</style> diff --git a/resources/js/store.js b/resources/js/store.js new file mode 100644 index 0000000..117db93 --- /dev/null +++ b/resources/js/store.js @@ -0,0 +1,96 @@ +import Vue from 'vue'; +import Vuex from 'vuex'; +Vue.use(Vuex); + +export default new Vuex.Store({ + state: { + selectedSign: "all", + callSigns: [], + data: [] + }, + getters: { + getSelectedSign(state) { + return state.selectedSign; + }, + getSigns(state) { + return state.callSigns; + }, + getData(state) { + return state.data; + }, + getDataRow(state) { + return (index) => _.cloneDeep(state.data[index]); + } + }, + mutations: { + setSelectedSign(state, sign) { + state.selectedSign = sign; + }, + setSigns(state, signs) { + state.callSigns = signs; + }, + setData(state, data) { + state.data = data; + }, + setDataRow(state, row) { + state.data[row.index] = _.cloneDeep(row.data); + }, + removeDataRow(state, index) { + state.data.splice(index, 1); + } + }, + actions: { + setSelectedSign(context, sign) { + context.commit('setSelectedSign', sign); + }, + async pullSigns(context) { + await axios.get('/special-calls/show').then(response => { + context.commit('setSigns', response.data); + }).catch(error => { + console.log(error); + }); + }, + async pullActivities(context) { + await axios.post('/api/activities', {'call-sign': this.state.selectedSign}).then(response => { + context.commit('setData', response.data.data); + }).catch(error => { + console.log(error); + }); + }, + async pullReservations(context) { + await axios.post('/special-calls/reservations', {'call-sign': this.state.selectedSign}).then(response => { + context.commit('setData', response.data.data); + }).catch(error => { + console.log(error); + }); + }, + async pushReservation(context, data) { + await axios.post('/api/reservations', { + action: 'update', + ...data.reservation + }).then(() => { + context.commit('setDataRow', { + index: data.index, + data: data.reservation + }); + }).catch(error => { + console.log(error); + alert("Couldn't update reservation! Bad data!"); + }); + }, + async removeReservation(context, index) { + let data = { + action: 'delete', + ...this.state.data[index] + }; + if (confirm(`Are you sure you want to delete reservation #${data.id} made by ${data.operatorCall}?`) === true) { + await axios.post('/api/reservations', data).then(() => { + context.commit('removeDataRow', index); + }).catch(error => { + console.log(error); + alert('Unable to remove reservation!'); + }); + } + } + } +}); |