gancio/components/admin/Cohorts.vue

207 lines
7.5 KiB
Vue
Raw Normal View History

2022-05-20 12:33:09 +02:00
<template lang='pug'>
v-container
v-card-title {{$t('common.cohort')}}
v-spacer
v-text-field(v-model='search'
:append-icon='mdiMagnify' outlined rounded
label='Search'
single-line hide-details)
v-card-subtitle(v-html="$t('admin.cohort_description')")
2022-05-25 10:51:58 +02:00
v-btn(color='primary' text @click='newCohort') <v-icon v-text='mdiPlus'></v-icon> {{$t('common.new')}}
2022-05-20 12:33:09 +02:00
v-dialog(v-model='dialog' width='800' destroy-on-close :fullscreen='$vuetify.breakpoint.xsOnly')
v-card(color='secondary')
v-card-title {{$t('admin.edit_cohort')}}
v-card-text
2022-05-31 15:32:18 +02:00
v-form(v-model='valid' ref='form')
2022-05-25 10:51:58 +02:00
v-text-field(
v-if='!cohort.id'
:rules="[$validators.required('common.name')]"
:label="$t('common.name')"
v-model='cohort.name'
:placeholder='$t("common.name")')
template(v-slot:append-outer v-if='!cohort.id')
v-btn(text @click='saveCohort' color='primary' :loading='loading'
:disabled='!valid || loading || !!cohort.id') {{$t('common.save')}}
h3(v-else class='text-h5' v-text='cohort.name')
2022-05-20 12:33:09 +02:00
2022-05-25 10:51:58 +02:00
v-row
2022-05-20 12:33:09 +02:00
v-col(cols=5)
v-autocomplete(v-model='filterTags'
2022-05-31 15:32:18 +02:00
cache-items
2022-05-20 12:33:09 +02:00
:prepend-icon="mdiTagMultiple"
2022-05-31 15:32:18 +02:00
2022-05-20 12:33:09 +02:00
chips small-chips multiple deletable-chips hide-no-data hide-selected persistent-hint
2022-05-25 10:51:58 +02:00
:disabled="!cohort.id"
2022-05-20 12:33:09 +02:00
placeholder='Tutte'
2022-05-31 15:32:18 +02:00
@input.native='searchTags'
2022-05-20 12:33:09 +02:00
:delimiters="[',', ';']"
2022-05-31 15:32:18 +02:00
:items="tags"
2022-05-20 12:33:09 +02:00
:label="$t('common.tags')")
v-col(cols=5)
v-autocomplete(v-model='filterPlaces'
2022-05-31 15:32:18 +02:00
cache-items
2022-05-20 12:33:09 +02:00
:prepend-icon="mdiMapMarker"
chips small-chips multiple deletable-chips hide-no-data hide-selected persistent-hint
2022-05-31 15:32:18 +02:00
auto-select-first
clearable
2022-05-25 10:51:58 +02:00
return-object
2022-05-31 15:32:18 +02:00
item-text='name'
2022-05-25 10:51:58 +02:00
:disabled="!cohort.id"
2022-05-31 15:32:18 +02:00
@input.native="searchPlaces"
2022-05-20 12:33:09 +02:00
:delimiters="[',', ';']"
2022-05-31 15:32:18 +02:00
:items="places"
2022-05-20 12:33:09 +02:00
:label="$t('common.places')")
2022-05-31 15:32:18 +02:00
//- template(v-slot:item="{ item, attrs, on }")
//- v-list-item(v-bind='attrs' v-on='on')
//- v-list-item-content(two-line)
//- v-list-item-title(v-text='item.name')
//- v-list-item-subtitle(v-text='item.address')
2022-05-20 12:33:09 +02:00
v-col(cols=2)
2022-05-25 10:51:58 +02:00
v-btn(color='primary' text @click='addFilter' :disabled='!cohort.id || !filterPlaces.length && !filterTags.length') add <v-icon v-text='mdiPlus'></v-icon>
2022-05-20 12:33:09 +02:00
v-data-table(
2022-05-31 15:32:18 +02:00
:headers='filterHeaders'
:items='filters'
:hide-default-footer='filters.length<5'
:footer-props='{ prevIcon: mdiChevronLeft, nextIcon: mdiChevronRight }')
template(v-slot:item.actions='{item}')
v-btn(@click='removeFilter(item)' color='error' icon)
v-icon(v-text='mdiDeleteForever')
template(v-slot:item.tags='{item}')
v-chip.ma-1(small v-for='tag in item.tags' v-text='tag' :key='tag')
template(v-slot:item.places='{item}')
v-chip.ma-1(small v-for='place in item.places' v-text='place.name' :key='place.id' )
2022-05-20 12:33:09 +02:00
v-card-actions
v-spacer
2022-05-25 10:51:58 +02:00
v-btn(text @click='dialog=false' color='warning') {{$t('common.close')}}
//- v-btn(text @click='saveCohort' color='primary' :loading='loading'
//- :disable='!valid || loading') {{$t('common.save')}}
2022-05-20 12:33:09 +02:00
v-card-text
v-data-table(
:headers='cohortHeaders'
:items='cohorts'
:hide-default-footer='cohorts.length<5'
:footer-props='{ prevIcon: mdiChevronLeft, nextIcon: mdiChevronRight }'
:search='search')
2022-05-25 10:51:58 +02:00
template(v-slot:item.filters='{item}')
span {{cohortFilters(item)}}
2022-05-20 12:33:09 +02:00
template(v-slot:item.actions='{item}')
v-btn(@click='editCohort(item)' color='primary' icon)
v-icon(v-text='mdiPencil')
v-btn(@click='removeCohort(item)' color='error' icon)
v-icon(v-text='mdiDeleteForever')
</template>
<script>
import get from 'lodash/get'
2022-05-31 15:32:18 +02:00
import debounce from 'lodash/debounce'
2022-05-20 12:33:09 +02:00
import { mdiPencil, mdiChevronLeft, mdiChevronRight, mdiMagnify, mdiPlus, mdiTagMultiple, mdiMapMarker, mdiDeleteForever, mdiCloseCircle } from '@mdi/js'
export default {
data () {
return {
mdiPencil, mdiChevronRight, mdiChevronLeft, mdiMagnify, mdiPlus, mdiTagMultiple, mdiMapMarker, mdiDeleteForever, mdiCloseCircle,
loading: false,
dialog: false,
valid: false,
search: '',
cohort: { name: '', id: null },
filterTags: [],
filterPlaces: [],
2022-05-31 15:32:18 +02:00
tags: [],
places: [],
2022-05-20 12:33:09 +02:00
cohorts: [],
filters: [],
tagName: '',
placeName: '',
cohortHeaders: [
{ value: 'name', text: 'Name' },
2022-05-25 10:51:58 +02:00
{ value: 'filters', text: 'Filters' },
2022-05-20 12:33:09 +02:00
{ value: 'actions', text: 'Actions', align: 'right' }
],
filterHeaders: [
{ value: 'tags', text: 'Tags' },
{ value: 'places', text: 'Places' },
{ value: 'actions', text: 'Actions', align: 'right' }
]
}
},
2022-05-25 10:51:58 +02:00
async fetch () {
this.cohorts = await this.$axios.$get('/cohorts?withFilters=true')
2022-05-20 12:33:09 +02:00
},
methods: {
2022-05-31 15:32:18 +02:00
searchTags: debounce(async function (ev) {
this.tags = await this.$axios.$get(`/tag?search=${ev.target.value}`)
}, 100),
searchPlaces: debounce(async function (ev) {
this.places = await this.$axios.$get(`/place?search=${ev.target.value}`)
}, 100),
2022-05-25 10:51:58 +02:00
cohortFilters (cohort) {
return cohort.filters.map(f => {
return '(' + f.tags?.join(', ') + f.places?.map(p => p.name).join(', ') + ')'
}).join(' - ')
},
2022-05-20 12:33:09 +02:00
async addFilter () {
this.loading = true
const tags = this.filterTags
const places = this.filterPlaces.map(p => ({ id: p.id, name: p.name }))
const filter = await this.$axios.$post('/filter', { cohortId: this.cohort.id, tags, places })
2022-05-25 10:51:58 +02:00
this.$fetch()
2022-05-20 12:33:09 +02:00
this.filters.push(filter)
this.filterTags = []
this.filterPlaces = []
this.loading = false
},
async editCohort (cohort) {
this.cohort = { ...cohort }
this.filters = await this.$axios.$get(`/filter/${cohort.id}`)
this.dialog = true
},
2022-05-25 10:51:58 +02:00
newCohort () {
this.cohort = { name: '', id: null },
this.filters = []
this.dialog = true
},
2022-05-20 12:33:09 +02:00
async saveCohort () {
if (!this.$refs.form.validate()) return
this.loading = true
this.cohort = await this.$axios.$post('/cohorts', this.cohort)
2022-05-25 10:51:58 +02:00
this.$fetch()
2022-05-20 12:33:09 +02:00
this.loading = false
},
async removeFilter(filter) {
try {
await this.$axios.$delete(`/filter/${filter.id}`)
this.filters = this.filters.filter(f => f.id !== filter.id)
2022-05-25 10:51:58 +02:00
this.$fetch()
2022-05-20 12:33:09 +02:00
} catch (e) {
const err = get(e, 'response.data.errors[0].message', e)
this.$root.$message(this.$t(err), { color: 'error' })
this.loading = false
}
},
async removeCohort (cohort) {
const ret = await this.$root.$confirm('admin.delete_cohort_confirm', { cohort: cohort.name })
if (!ret) { return }
try {
await this.$axios.$delete(`/cohort/${cohort.id}`)
this.cohorts = this.cohorts.filter(c => c.id !== cohort.id)
} catch (e) {
const err = get(e, 'response.data.errors[0].message', e)
this.$root.$message(this.$t(err), { color: 'error' })
this.loading = false
}
}
}
}
</script>