Skip to content
This repository was archived by the owner on Jan 20, 2021. It is now read-only.

Commit 0593302

Browse files
authored
ui: Allowing user to use local/browser timezone (#903)
Allowing user to use local/browser timezone
1 parent dd20907 commit 0593302

File tree

7 files changed

+44
-8
lines changed

7 files changed

+44
-8
lines changed

src/components/header/UserMenu.vue

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@
3232
<span class="user-menu-item-name">{{ $t('label.profilename') }}</span>
3333
</router-link>
3434
</a-menu-item>
35-
<a-menu-item class="user-menu-item" key="1" disabled>
35+
<a-menu-item class="user-menu-item" key="1">
36+
<a @click="toggleUseBrowserTimezone">
37+
<a-icon class="user-menu-item-icon" type="clock-circle"/>
38+
<span class="user-menu-item-name" style="margin-right: 5px">{{ $t('label.use.local.timezone') }}</span>
39+
<a-switch
40+
:checked="$store.getters.usebrowsertimezone" />
41+
</a>
42+
</a-menu-item>
43+
<a-menu-item class="user-menu-item" key="2" disabled>
3644
<a :href="$config.docBase" target="_blank">
3745
<a-icon class="user-menu-item-icon" type="question-circle-o"></a-icon>
3846
<span class="user-menu-item-name">{{ $t('label.help') }}</span>
3947
</a>
4048
</a-menu-item>
4149
<a-menu-divider/>
42-
<a-menu-item class="user-menu-item" key="2">
50+
<a-menu-item class="user-menu-item" key="3">
4351
<a href="javascript:;" @click="handleLogout">
4452
<a-icon class="user-menu-item-icon" type="logout"/>
4553
<span class="user-menu-item-name">{{ $t('label.logout') }}</span>
@@ -64,6 +72,9 @@ export default {
6472
methods: {
6573
...mapActions(['Logout']),
6674
...mapGetters(['nickname', 'avatar']),
75+
toggleUseBrowserTimezone () {
76+
this.$store.dispatch('SetUseBrowserTimezone', !this.$store.getters.usebrowsertimezone)
77+
},
6778
handleLogout () {
6879
return this.Logout({}).then(() => {
6980
this.$router.push('/user/login')
@@ -85,7 +96,7 @@ export default {
8596
}
8697
8798
&-item {
88-
width: 160px;
99+
width: auto;
89100
}
90101
91102
&-item-name {

src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,7 @@
21572157
"label.usageinterface": "Usage Interface",
21582158
"label.usagename": "Usage Type",
21592159
"label.usageunit": "Unit",
2160+
"label.use.local.timezone": "Use Local Timezone",
21602161
"label.use.kubectl.access.cluster": "<code><b>kubectl</b></code> and <code><b>kubeconfig</b></code> file to access cluster",
21612162
"label.use.vm.ip": "Use VM IP:",
21622163
"label.use.vm.ips": "Use VM IPs",

src/store/getters.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const getters = {
3434
isLdapEnabled: state => state.user.isLdapEnabled,
3535
cloudian: state => state.user.cloudian,
3636
zones: state => state.user.zones,
37-
timezoneoffset: state => state.user.timezoneoffset
37+
timezoneoffset: state => state.user.timezoneoffset,
38+
usebrowsertimezone: state => state.user.usebrowsertimezone
3839
}
3940

4041
export default getters

src/store/modules/app.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import {
2626
DEFAULT_FIXED_SIDEMENU,
2727
DEFAULT_FIXED_HEADER_HIDDEN,
2828
DEFAULT_CONTENT_WIDTH_TYPE,
29-
DEFAULT_MULTI_TAB
29+
DEFAULT_MULTI_TAB,
30+
USE_BROWSER_TIMEZONE
3031
} from '@/store/mutation-types'
3132

3233
const app = {
@@ -95,6 +96,10 @@ const app = {
9596
},
9697
SET_METRICS: (state, bool) => {
9798
state.metrics = bool
99+
},
100+
SET_USE_BROWSER_TIMEZONE: (state, bool) => {
101+
Vue.ls.set(USE_BROWSER_TIMEZONE, bool)
102+
state.usebrowsertimezone = bool
98103
}
99104
},
100105
actions: {
@@ -139,6 +144,9 @@ const app = {
139144
},
140145
SetMetrics ({ commit }, bool) {
141146
commit('SET_METRICS', bool)
147+
},
148+
SetUseBrowserTimezone ({ commit }, bool) {
149+
commit('SET_USE_BROWSER_TIMEZONE', bool)
142150
}
143151
}
144152
}

src/store/modules/user.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import router from '@/router'
2424
import store from '@/store'
2525
import { login, logout, api } from '@/api'
2626
import i18n from '@/locales'
27-
import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, APIS, ASYNC_JOB_IDS, ZONES, TIMEZONE_OFFSET } from '@/store/mutation-types'
27+
import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, APIS, ASYNC_JOB_IDS, ZONES, TIMEZONE_OFFSET, USE_BROWSER_TIMEZONE } from '@/store/mutation-types'
2828

2929
const user = {
3030
state: {
@@ -39,7 +39,8 @@ const user = {
3939
isLdapEnabled: false,
4040
cloudian: {},
4141
zones: {},
42-
timezoneoffset: '0.0'
42+
timezoneoffset: 0.0,
43+
usebrowsertimezone: false
4344
},
4445

4546
mutations: {
@@ -50,6 +51,10 @@ const user = {
5051
Vue.ls.set(TIMEZONE_OFFSET, timezoneoffset)
5152
state.timezoneoffset = timezoneoffset
5253
},
54+
SET_USE_BROWSER_TIMEZONE: (state, bool) => {
55+
Vue.ls.set(USE_BROWSER_TIMEZONE, bool)
56+
state.usebrowsertimezone = bool
57+
},
5358
SET_PROJECT: (state, project = {}) => {
5459
Vue.ls.set(CURRENT_PROJECT, project)
5560
state.project = project
@@ -109,6 +114,9 @@ const user = {
109114
commit('SET_TOKEN', result.sessionkey)
110115
commit('SET_TIMEZONE_OFFSET', result.timezoneoffset)
111116

117+
const cachedUseBrowserTimezone = Vue.ls.get(USE_BROWSER_TIMEZONE, false)
118+
commit('SET_USE_BROWSER_TIMEZONE', cachedUseBrowserTimezone)
119+
112120
commit('SET_APIS', {})
113121
commit('SET_NAME', '')
114122
commit('SET_AVATAR', '')
@@ -133,12 +141,14 @@ const user = {
133141
const cachedApis = Vue.ls.get(APIS, {})
134142
const cachedZones = Vue.ls.get(ZONES, [])
135143
const cachedTimezoneOffset = Vue.ls.get(TIMEZONE_OFFSET, 0.0)
144+
const cachedUseBrowserTimezone = Vue.ls.get(USE_BROWSER_TIMEZONE, false)
136145
const hasAuth = Object.keys(cachedApis).length > 0
137146
if (hasAuth) {
138147
console.log('Login detected, using cached APIs')
139148
commit('SET_ZONES', cachedZones)
140149
commit('SET_APIS', cachedApis)
141150
commit('SET_TIMEZONE_OFFSET', cachedTimezoneOffset)
151+
commit('SET_USE_BROWSER_TIMEZONE', cachedUseBrowserTimezone)
142152

143153
// Ensuring we get the user info so that store.getters.user is never empty when the page is freshly loaded
144154
api('listUsers', { username: Cookies.get('username'), listall: true }).then(response => {

src/store/mutation-types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const APIS = 'APIS'
3131
export const ZONES = 'ZONES'
3232
export const ASYNC_JOB_IDS = 'ASYNC_JOB_IDS'
3333
export const TIMEZONE_OFFSET = 'TIMEZONE_OFFSET'
34+
export const USE_BROWSER_TIMEZONE = 'USE_BROWSER_TIMEZONE'
3435

3536
export const CONTENT_WIDTH_TYPE = {
3637
Fluid: 'Fluid',

src/utils/plugins.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ export const notifierPlugin = {
151151
export const toLocaleDatePlugin = {
152152
install (Vue) {
153153
Vue.prototype.$toLocaleDate = function (date) {
154-
var milliseconds = Date.parse(date)
155154
var timezoneOffset = this.$store.getters.timezoneoffset
155+
if (this.$store.getters.usebrowsertimezone) {
156+
// Since GMT+530 is returned as -330 (mins to GMT)
157+
timezoneOffset = new Date().getTimezoneOffset() / -60
158+
}
159+
var milliseconds = Date.parse(date)
156160
// e.g. "Tue, 08 Jun 2010 19:13:49 GMT", "Tue, 25 May 2010 12:07:01 UTC"
157161
var dateWithOffset = new Date(milliseconds + (timezoneOffset * 60 * 60 * 1000)).toUTCString()
158162
// e.g. "08 Jun 2010 19:13:49 GMT", "25 May 2010 12:07:01 UTC"

0 commit comments

Comments
 (0)