2020-03-12 14:29:21 +01:00
< template >
2020-02-18 08:57:00 +01:00
< div >
2020-06-25 11:36:35 +02:00
< nav class = "breadcrumb" aria -label = " breadcrumbs " >
< ul >
< li >
2020-11-30 10:24:11 +01:00
< router -link : to = "{ name: RouteName.ACCOUNT_SETTINGS }" > { {
$t ( "Account" )
} } < / r o u t e r - l i n k >
2020-06-25 11:36:35 +02:00
< / li >
< li class = "is-active" >
2020-11-30 10:24:11 +01:00
< router -link : to = "{ name: RouteName.PREFERENCES }" > { {
$t ( "Preferences" )
} } < / r o u t e r - l i n k >
2020-06-25 11:36:35 +02:00
< / li >
< / ul >
< / nav >
< div >
< b -field :label ="$t('Language')" >
< b -select
: loading = "!config || !loggedUser"
2020-10-28 18:58:43 +01:00
v - model = "locale"
2020-06-25 11:36:35 +02:00
: placeholder = "$t('Select a language')"
>
2020-10-13 20:39:59 +02:00
< option v-for ="(language, lang) in langs" :value="lang" :key ="lang" >
2020-06-25 11:36:35 +02:00
{ { language } }
2020-02-18 08:57:00 +01:00
< / option >
2020-06-25 11:36:35 +02:00
< / b - s e l e c t >
< / b - f i e l d >
2021-02-12 18:19:49 +01:00
< b -field :label ="$t('Timezone')" v-if ="selectedTimezone" >
2020-06-25 11:36:35 +02:00
< b -select
: placeholder = "$t('Select a timezone')"
: loading = "!config || !loggedUser"
v - model = "selectedTimezone"
>
2020-11-30 10:24:11 +01:00
< optgroup
: label = "group"
v - for = "(groupTimezones, group) in timezones"
: key = "group"
>
2020-06-25 11:36:35 +02:00
< option
v - for = "timezone in groupTimezones"
: value = "`${group}/${timezone}`"
: key = "timezone"
>
{ { sanitize ( timezone ) } }
< / option >
< / optgroup >
< / b - s e l e c t >
< / b - f i e l d >
2020-10-21 09:04:29 +02:00
< em v-if ="Intl.DateTimeFormat().resolvedOptions().timeZone" > {{
2020-06-25 11:36:35 +02:00
$t ( "Timezone detected as {timezone}." , {
timezone : Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ,
} )
} } < / em >
2020-11-30 10:24:11 +01:00
< b -message v -else type = "is-danger" > { {
$t ( "Unable to detect timezone." )
} } < / b - m e s s a g e >
2021-02-12 18:19:49 +01:00
< hr / >
< b -field grouped >
< b -field : label = "$t('City or region')" expanded >
< address -auto -complete
v - if = "
loggedUser && loggedUser . settings && loggedUser . settings . location
"
: type = "AddressSearchType.ADMINISTRATIVE"
2021-03-02 10:02:06 +01:00
: doGeoLocation = "false"
2021-02-12 18:19:49 +01:00
v - model = "address"
>
< / a d d r e s s - a u t o - c o m p l e t e >
< / b - f i e l d >
< b -field :label ="$t('Radius')" >
< b -select
: placeholder = "$t('Select a radius')"
v - model = "locationRange"
>
< option
v - for = "index in [1, 5, 10, 25, 50, 100]"
: key = "index"
: value = "index"
>
{ { $tc ( "{count} km" , index , { count : index } ) } }
< / option >
< / b - s e l e c t >
< / b - f i e l d >
< / b - f i e l d >
< p >
{ {
$t (
2021-03-04 15:26:03 +01:00
"Your city or region and the radius will only be used to suggest you events nearby. The event radius will consider the administrative center of the area."
2021-02-12 18:19:49 +01:00
)
} }
< / p >
2020-06-25 11:36:35 +02:00
< / div >
2020-02-18 08:57:00 +01:00
< / div >
2020-03-12 14:29:21 +01:00
< / template >
< script lang = "ts" >
2020-02-18 08:57:00 +01:00
import { Component , Vue , Watch } from "vue-property-decorator" ;
2021-02-12 18:19:49 +01:00
import ngeohash from "ngeohash" ;
2020-10-28 18:58:43 +01:00
import { saveLocaleData } from "@/utils/auth" ;
2020-02-18 08:57:00 +01:00
import { TIMEZONES } from "../../graphql/config" ;
2020-11-30 10:24:11 +01:00
import {
USER _SETTINGS ,
SET _USER _SETTINGS ,
UPDATE _USER _LOCALE ,
} from "../../graphql/user" ;
2020-02-18 08:57:00 +01:00
import { IConfig } from "../../types/config.model" ;
2021-02-12 18:19:49 +01:00
import { IUser , IUserSettings } from "../../types/current-user.model" ;
2020-06-16 18:00:27 +02:00
import langs from "../../i18n/langs.json" ;
2020-06-25 11:36:35 +02:00
import RouteName from "../../router/name" ;
2021-02-12 18:19:49 +01:00
import AddressAutoComplete from "../../components/Event/AddressAutoComplete.vue" ;
import { AddressSearchType } from "@/types/enums" ;
import { Address , IAddress } from "@/types/address.model" ;
2020-03-12 14:29:21 +01:00
2020-02-18 08:57:00 +01:00
@ Component ( {
apollo : {
config : TIMEZONES ,
loggedUser : USER _SETTINGS ,
} ,
2021-02-12 18:19:49 +01:00
components : {
AddressAutoComplete ,
} ,
2020-02-18 08:57:00 +01:00
} )
2020-03-12 14:29:21 +01:00
export default class Preferences extends Vue {
2020-02-18 08:57:00 +01:00
config ! : IConfig ;
2020-03-12 14:29:21 +01:00
2020-06-27 19:12:45 +02:00
loggedUser ! : IUser ;
2020-02-18 08:57:00 +01:00
2021-02-12 18:19:49 +01:00
selectedTimezone : string | undefined = undefined ;
2020-02-18 08:57:00 +01:00
2020-06-16 18:00:27 +02:00
locale : string | null = null ;
2020-06-25 11:36:35 +02:00
RouteName = RouteName ;
2020-10-13 20:39:59 +02:00
langs : Record < string , string > = langs ;
2021-02-12 18:19:49 +01:00
AddressSearchType = AddressSearchType ;
2020-02-18 08:57:00 +01:00
@ Watch ( "loggedUser" )
2020-10-13 20:39:59 +02:00
setSavedTimezone ( loggedUser : IUser ) : void {
2021-02-12 18:19:49 +01:00
if ( loggedUser ? . settings ? . timezone ) {
2020-02-18 08:57:00 +01:00
this . selectedTimezone = loggedUser . settings . timezone ;
} else {
this . selectedTimezone = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ;
}
2021-02-12 18:19:49 +01:00
if ( loggedUser ? . locale ) {
2020-06-16 18:00:27 +02:00
this . locale = loggedUser . locale ;
} else {
this . locale = this . $i18n . locale ;
}
2020-02-18 08:57:00 +01:00
}
2020-10-13 20:39:59 +02:00
// eslint-disable-next-line class-methods-use-this
2020-02-18 08:57:00 +01:00
sanitize ( timezone : string ) : string {
2020-11-30 10:24:11 +01:00
return timezone
. split ( "_" )
. join ( " " )
. replace ( "St " , "St. " )
. split ( "/" )
. join ( " - " ) ;
2020-02-18 08:57:00 +01:00
}
2020-10-13 20:39:59 +02:00
get timezones ( ) : Record < string , string [ ] > {
2020-02-18 08:57:00 +01:00
if ( ! this . config || ! this . config . timezones ) return { } ;
2020-11-30 10:24:11 +01:00
return this . config . timezones . reduce (
( acc : { [ key : string ] : Array < string > } , val : string ) => {
const components = val . split ( "/" ) ;
const [ prefix , suffix ] = [
components . shift ( ) as string ,
components . join ( "/" ) ,
] ;
const pushOrCreate = (
acc2 : { [ key : string ] : Array < string > } ,
prefix2 : string ,
suffix2 : string
) => {
// eslint-disable-next-line no-param-reassign
( acc2 [ prefix2 ] = acc2 [ prefix2 ] || [ ] ) . push ( suffix2 ) ;
return acc2 ;
} ;
if ( suffix ) {
return pushOrCreate ( acc , prefix , suffix ) ;
}
return pushOrCreate ( acc , this . $t ( "Other" ) as string , prefix ) ;
} ,
{ }
) ;
2020-02-18 08:57:00 +01:00
}
@ Watch ( "selectedTimezone" )
2020-10-13 20:39:59 +02:00
async updateTimezone ( ) : Promise < void > {
2020-06-16 18:00:27 +02:00
if ( this . selectedTimezone !== this . loggedUser . settings . timezone ) {
2021-02-12 18:19:49 +01:00
this . updateUserSettings ( { timezone : this . selectedTimezone } ) ;
2020-06-16 18:00:27 +02:00
}
}
2020-10-28 18:58:43 +01:00
@ Watch ( "locale" )
2020-10-13 20:39:59 +02:00
async updateLocale ( ) : Promise < void > {
2020-10-28 18:58:43 +01:00
if ( this . locale ) {
await this . $apollo . mutate ( {
mutation : UPDATE _USER _LOCALE ,
variables : {
locale : this . locale ,
} ,
} ) ;
saveLocaleData ( this . locale ) ;
}
2020-02-18 08:57:00 +01:00
}
2021-02-12 18:19:49 +01:00
get address ( ) : IAddress | null {
if (
this . loggedUser ? . settings ? . location ? . name &&
this . loggedUser ? . settings ? . location ? . geohash
) {
const { latitude , longitude } = ngeohash . decode (
this . loggedUser ? . settings ? . location ? . geohash
) ;
const name = this . loggedUser ? . settings ? . location ? . name ;
return {
description : name ,
locality : "" ,
type : "administrative" ,
geom : ` ${ longitude } ; ${ latitude } ` ,
street : "" ,
postalCode : "" ,
region : "" ,
country : "" ,
} ;
}
return null ;
}
set address ( address : IAddress | null ) {
if ( address && address . geom ) {
const { geom } = address ;
const addressObject = new Address ( address ) ;
const queryText = addressObject . poiInfos . name ;
const [ lon , lat ] = geom . split ( ";" ) ;
const geohash = ngeohash . encode ( lat , lon , 6 ) ;
if ( queryText && geom ) {
this . updateUserSettings ( {
location : {
geohash ,
name : queryText ,
} ,
} ) ;
}
}
}
get locationRange ( ) : number | undefined {
return this . loggedUser ? . settings ? . location ? . range ;
}
set locationRange ( locationRange : number | undefined ) {
if ( locationRange ) {
this . updateUserSettings ( {
location : {
range : locationRange ,
} ,
} ) ;
}
}
private async updateUserSettings ( userSettings : IUserSettings ) {
await this . $apollo . mutate < { setUserSetting : string } > ( {
mutation : SET _USER _SETTINGS ,
variables : userSettings ,
} ) ;
}
2020-03-12 14:29:21 +01:00
}
2020-02-18 08:57:00 +01:00
< / script >