Files
dancefinder-app/components/pages/events/index.vue
T

258 lines
7.2 KiB
Vue
Raw Normal View History

2019-01-15 13:21:24 +01:00
<template>
<div :key="isAuthenticated">
2020-06-21 19:06:41 +02:00
<span v-text="isAuthenticated" />
<v-container fluid grid-list-md class="app-fade-in" :key="range">
<v-layout row wrap v-if="!isAuthenticated">
<v-flex xs12>
2020-06-21 19:06:41 +02:00
<p><b v-text="$t('events.login')" /></p>
</v-flex>
</v-layout>
<v-layout row wrap>
<v-flex xs12>
<v-text-field
2020-01-25 14:59:14 +01:00
v-model="origin"
:label="$t('origins.origin')"
:placeholder="$t('origins.geolocation')"
>
<v-tooltip top slot="append-outer">
<template v-slot:activator="{ on }">
2020-01-25 14:59:14 +01:00
<v-icon v-on="on" v-on:click="fetchAddress()"
2020-06-21 19:06:41 +02:00
>mdi-crosshairs-gps
</v-icon>
</template>
2020-06-21 19:06:41 +02:00
<span v-text="$t('origins.fetchAddress')" />
</v-tooltip>
<v-tooltip top slot="prepend" v-if="isAuthenticated">
<template v-slot:activator="{ on }">
2020-01-25 14:59:14 +01:00
<v-icon
v-on="on"
:disabled="!origin"
v-on:click="saveOrigin(origin)"
2020-06-21 19:06:41 +02:00
>mdi-bookmark-plus-outline
</v-icon>
</template>
2020-06-21 19:06:41 +02:00
<span v-text="$t('origins.save')" />
</v-tooltip>
</v-text-field>
</v-flex>
</v-layout>
<v-layout row wrap>
<v-flex>
2020-01-25 14:59:14 +01:00
<v-btn-toggle
v-if="$vuetify.breakpoint.smAndUp"
v-model="range"
mandatory
>
<v-btn
text
v-for="r in ranges"
:key="r.value"
:value="r.value"
v-text="$t(`events.range.${r.value}`)"
/>
2019-01-23 15:35:39 +01:00
</v-btn-toggle>
2020-01-25 14:59:14 +01:00
<v-select
outline
v-if="$vuetify.breakpoint.xsOnly"
v-model="range"
:items="ranges"
item-text="name"
item-value="value"
/>
2019-01-21 20:58:25 +01:00
</v-flex>
</v-layout>
2020-01-25 14:59:14 +01:00
<list
2020-04-06 10:52:55 +02:00
:events="events || []"
2020-01-25 14:59:14 +01:00
:has-user="isAuthenticated"
:toggleIgnore="toggleIgnore"
/>
2019-01-21 20:58:25 +01:00
</v-container>
<v-snackbar
v-model="snackbar.active"
:color="snackbar.color"
:timeout="5000"
>
{{ snackbar.text }}
</v-snackbar>
2019-01-15 13:21:24 +01:00
</div>
</template>
<script>
2020-06-21 19:06:41 +02:00
import { useMutations, useRouter } from '@u3u/vue-hooks'
import { computed, ref, watch } from '@vue/composition-api'
import { useMutation, useQuery, useResult } from '@vue/apollo-composable'
import { useAuth } from '../../../plugins/auth'
2020-01-25 14:59:14 +01:00
2020-06-21 19:06:41 +02:00
import List from './List'
import {
fetchAddress,
findEvents,
saveOrigin,
toggleIgnoreBand,
toggleIgnoreCity,
toggleIgnoreDanceHall,
toggleIgnoreMunicipality,
toggleIgnoreState
} from '../../../utils/graph-client'
import { useTranslation } from '../../../plugins/i18n'
2020-01-25 14:59:14 +01:00
2020-06-21 19:06:41 +02:00
export default {
components: {
List
},
setup() {
const { setTitle } = useMutations(['setTitle'])
const { t } = useTranslation()
setTitle(t('app.links.events'))
const { loading: authLoading, isAuthenticated } = useAuth()
const { route, router } = useRouter()
const range = computed({
get: () => route.value.query.range || 'ONE_WEEK',
set: value => router.push(`/?range=${value}`)
})
const enabled = ref(false)
const { result: data, refetch } = useQuery(
findEvents,
{ includeOrigins: false },
() => ({ enabled: enabled.value })
)
const events = useResult(data, [], result => result.events)
const origins = useResult(data, [], result => result.origins)
watch(
range,
r => {
2020-06-21 17:59:14 +02:00
enabled.value = true
2020-06-21 19:06:41 +02:00
console.log('isAuthenticated', isAuthenticated.value)
2020-06-21 17:59:14 +02:00
refetch({
2020-06-21 19:06:41 +02:00
range: r,
2020-01-25 14:59:14 +01:00
includeOrigins: isAuthenticated.value || false
2020-06-21 17:59:14 +02:00
})
2020-06-21 19:06:41 +02:00
},
{ lazy: false }
)
const submitting = ref(true)
const ranges = [
{ name: '1 vecka', value: 'ONE_WEEK' },
{ name: '2 veckor', value: 'TWO_WEEKS' },
{ name: '1 månad', value: 'ONE_MONTH' },
{ name: '1 kvartal', value: 'ONE_QUARTER' },
{ name: '1 år', value: 'ONE_YEAR' }
]
const snackbar = ref({ active: false, color: 'success', text: '' })
const origin = ref('')
const fetchEvents = () => {
const originsTemp = [...(origins.value || [])]
if (origin.value) {
originsTemp.push(origin.value)
2020-06-21 17:59:14 +02:00
}
2020-06-21 19:06:41 +02:00
enabled.value = true
refetch({
range: range.value,
origins: originsTemp,
includeOrigins: isAuthenticated.value || false
})
}
2020-01-25 14:59:14 +01:00
2020-06-21 19:06:41 +02:00
const { mutate: doToggleIgnoreBand } = useMutation(toggleIgnoreBand)
const { mutate: doToggleIgnoreDanceHall } = useMutation(
toggleIgnoreDanceHall
)
const { mutate: doToggleIgnoreCity } = useMutation(toggleIgnoreCity)
const { mutate: doToggleIgnoreMunicipality } = useMutation(
toggleIgnoreMunicipality
)
const { mutate: doToggleIgnoreState } = useMutation(toggleIgnoreState)
2020-01-25 14:59:14 +01:00
2020-06-21 19:06:41 +02:00
const toggleIgnoreSuccess = name => {
return () => {
fetchEvents()
snackbar.value.color = 'success'
snackbar.value.text = `${name} har dolts`
snackbar.value.active = true
}
2020-06-21 19:06:41 +02:00
}
2020-06-21 19:06:41 +02:00
const toggleIgnoreFailed = name => {
return () => {
snackbar.value.color = 'error'
snackbar.value.text = `${name} kunde inte döljas`
snackbar.value.active = true
}
2020-06-21 19:06:41 +02:00
}
2020-06-21 19:06:41 +02:00
const toggleIgnore = (type, name) => {
switch (type) {
case 'band':
doToggleIgnoreBand({ name })
.then(toggleIgnoreSuccess(name))
.catch(toggleIgnoreFailed)
break
case 'danceHall':
doToggleIgnoreDanceHall({ name })
.then(toggleIgnoreSuccess(name))
.catch(toggleIgnoreFailed)
break
case 'city':
doToggleIgnoreCity({ name })
.then(toggleIgnoreSuccess(name))
.catch(toggleIgnoreFailed)
break
case 'municipality':
doToggleIgnoreMunicipality({ name })
.then(toggleIgnoreSuccess(name))
.catch(toggleIgnoreFailed)
break
case 'state':
doToggleIgnoreState({ name })
.then(toggleIgnoreSuccess(name))
.catch(toggleIgnoreFailed)
break
default:
}
2020-06-21 19:06:41 +02:00
}
2020-06-21 19:06:41 +02:00
const { mutate: doSaveOrigin } = useMutation(saveOrigin)
const addressEnabled = ref(false)
const { result: address, refetch: doFetchAddress } = useQuery(
fetchAddress,
{},
() => ({ enabled: addressEnabled.value })
)
const fetchAddressFn = () => {
if (window.navigator) {
window.navigator.geolocation.getCurrentPosition(pos => {
addressEnabled.value = true
doFetchAddress({
latlng: `${pos.coords.latitude},${pos.coords.longitude}`
}).then(() => {
origin.value = address.value.address
})
2020-06-21 17:59:14 +02:00
})
}
2020-01-25 14:59:14 +01:00
}
2020-06-21 19:06:41 +02:00
const saveOriginFn = o =>
doSaveOrigin({ origin: o }).then(() => {
origin.value = ''
fetchEvents()
})
return {
authLoading,
isAuthenticated,
range,
events,
origins,
query: fetchEvents,
submitting,
ranges,
snackbar,
toggleIgnore,
origin,
fetchAddress: fetchAddressFn,
saveOrigin: saveOriginFn
}
2020-01-25 14:59:14 +01:00
}
2020-06-21 19:06:41 +02:00
}
2019-01-15 13:21:24 +01:00
</script>