From 14968013dd5329fad1270da1ef23faf137842920 Mon Sep 17 00:00:00 2001 From: Alf Sebastian Houge Date: Sun, 26 Feb 2023 13:54:55 +0100 Subject: [PATCH] Write and read language setting from backend --- server/db/dbModels.go | 1 + server/db/migrations.go | 2 +- ui/src/router/views/settings.vue | 29 +++++++++++++++--- ui/src/state/modules/masters.js | 52 ++++++++++++++++++++++++++++++++ ui/src/state/modules/vehicles.js | 33 -------------------- 5 files changed, 79 insertions(+), 38 deletions(-) create mode 100644 ui/src/state/modules/masters.js diff --git a/server/db/dbModels.go b/server/db/dbModels.go index 71682bb..aec7665 100644 --- a/server/db/dbModels.go +++ b/server/db/dbModels.go @@ -19,6 +19,7 @@ type User struct { Name string `json:"name"` Vehicles []Vehicle `gorm:"many2many:user_vehicles;" json:"vehicles"` IsDisabled bool `json:"isDisabled"` + Language string `json:"language"` } func (b *User) MarshalJSON() ([]byte, error) { diff --git a/server/db/migrations.go b/server/db/migrations.go index b9824f8..b2f4112 100644 --- a/server/db/migrations.go +++ b/server/db/migrations.go @@ -29,7 +29,7 @@ var migrations = []localMigration{ }, { Name: "2023_02_26_13_42_AddLanguage", - Query: "ALTER TABLE users ADD COLUMN language text default 'English'", + Query: "ALTER TABLE users ADD COLUMN language text default 'English 🇬🇧'", }, } diff --git a/ui/src/router/views/settings.vue b/ui/src/router/views/settings.vue index ca4b326..768bace 100644 --- a/ui/src/router/views/settings.vue +++ b/ui/src/router/views/settings.vue @@ -22,6 +22,7 @@ export default { data: function() { return { settingsModel: { + language: this.me.language, currency: this.me.currency, distanceUnit: this.me.distanceUnit, dateFormat: this.me.dateFormat, @@ -36,7 +37,7 @@ export default { } }, computed: { - ...mapState('vehicles', ['currencyMasters', 'distanceUnitMasters']), + ...mapState('masters', ['currencyMasters', 'languageMasters', 'distanceUnitMasters']), passwordValid() { if (this.changePassModel.new === '' || this.changePassModel.renew === '') { return true @@ -58,6 +59,15 @@ export default { ) }) }, + filteredLanguageMasters() { + return this.languageMasters.filter((option) => { + return ( + option.nameNative + .toString() + .toLowerCase() + .indexOf(this.settingsModel.language.toLowerCase()) >= 0) + }) + }, }, methods: { changePassword() { @@ -126,6 +136,9 @@ export default { formatCurrency(option) { return `${option.namePlural} (${option.code})` }, + formatLanguage(option) { + return `${option.nameNative} ${option.emoji}` + }, }, } @@ -136,9 +149,17 @@ export default {
-

- {{ $t('settingdesc') }} -

+ + + { + commit('CACHE_LANGUAGE_MASTERS', response.data.languages) + commit('CACHE_FUEL_UNIT_MASTERS', response.data.fuelUnits) + commit('CACHE_FUEL_TYPE_MASTERS', response.data.fuelTypes) + commit('CACHE_CURRENCY_MASTERS', response.data.currencies) + commit('CACHE_DISTANCE_UNIT_MASTERS', response.data.distanceUnits) + commit('CACHE_ROLE_MASTERS', response.data.roles) + return response.data + }) + }, +} \ No newline at end of file diff --git a/ui/src/state/modules/vehicles.js b/ui/src/state/modules/vehicles.js index ba5fef8..173c00f 100644 --- a/ui/src/state/modules/vehicles.js +++ b/ui/src/state/modules/vehicles.js @@ -4,11 +4,6 @@ import { filter } from 'lodash' import parseISO from 'date-fns/parseISO' export const state = { vehicles: [], - roleMasters: [], - fuelUnitMasters: [], - distanceUnitMasters: [], - currencyMasters: [], - fuelTypeMasters: [], quickEntries: [], vehicleStats: new Map(), } @@ -29,24 +24,9 @@ export const mutations = { CACHE_VEHICLE_STATS(state, stats) { state.vehicleStats.set(stats.vehicleId, stats) }, - CACHE_FUEL_UNIT_MASTERS(state, masters) { - state.fuelUnitMasters = masters - }, - CACHE_DISTANCE_UNIT_MASTERS(state, masters) { - state.distanceUnitMasters = masters - }, - CACHE_FUEL_TYPE_MASTERS(state, masters) { - state.fuelTypeMasters = masters - }, - CACHE_CURRENCY_MASTERS(state, masters) { - state.currencyMasters = masters - }, CACHE_QUICK_ENTRIES(state, entries) { state.quickEntries = entries }, - CACHE_ROLE_MASTERS(state, roles) { - state.roleMasters = roles - }, } export const actions = { @@ -54,22 +34,9 @@ export const actions = { const { currentUser } = rootState.auth if (currentUser) { dispatch('fetchVehicles') - dispatch('fetchMasters') dispatch('fetchQuickEntries', { force: true }) } }, - fetchMasters({ commit, state, rootState }) { - return axios.get('/api/masters').then((response) => { - const fuelUnitMasters = response.data.fuelUnits - const fuelTypeMasters = response.data.fuelTypes - commit('CACHE_FUEL_UNIT_MASTERS', fuelUnitMasters) - commit('CACHE_FUEL_TYPE_MASTERS', fuelTypeMasters) - commit('CACHE_CURRENCY_MASTERS', response.data.currencies) - commit('CACHE_DISTANCE_UNIT_MASTERS', response.data.distanceUnits) - commit('CACHE_ROLE_MASTERS', response.data.roles) - return response.data - }) - }, fetchVehicles({ commit, state, rootState }) { return axios.get('/api/me/vehicles').then((response) => { const data = response.data