Changeset View
Changeset View
Standalone View
Standalone View
src/resources/js/app.js
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | methods: { | ||||
} | } | ||||
store.state.afterLogin = null | store.state.afterLogin = null | ||||
// Refresh the token before it expires | // Refresh the token before it expires | ||||
let timeout = response.expires_in || 0 | let timeout = response.expires_in || 0 | ||||
// We'll refresh 60 seconds before the token expires | // We'll refresh 60 seconds before the token expires | ||||
// or immediately when we have no expiration time (on token re-use) | |||||
if (timeout > 60) { | if (timeout > 60) { | ||||
timeout -= 60 | timeout -= 60 | ||||
} | } | ||||
// TODO: We probably should try a few times in case of an error | // TODO: We probably should try a few times in case of an error | ||||
// TODO: We probably should prevent axios from doing any requests | // TODO: We probably should prevent axios from doing any requests | ||||
// while the token is being refreshed | // while the token is being refreshed | ||||
this.refreshTimeout = setTimeout(() => { | this.refreshTimeout = setTimeout(() => { | ||||
axios.post('/api/auth/refresh').then(response => { | axios.post('/api/auth/refresh').then(response => { | ||||
this.loginUser(response.data, false, true) | this.loginUser(response.data, false, true) | ||||
}) | }) | ||||
}, timeout * 1000) | }, timeout * 1000) | ||||
}, | }, | ||||
// Set user state to "not logged in" | // Set user state to "not logged in" | ||||
logoutUser() { | logoutUser() { | ||||
store.commit('logoutUser') | store.commit('logoutUser') | ||||
localStorage.setItem('token', '') | localStorage.setItem('token', '') | ||||
delete axios.defaults.headers.common.Authorization | delete axios.defaults.headers.common.Authorization | ||||
this.$router.push({ name: 'login' }) | this.$router.push({ name: 'login' }) | ||||
clearTimeout(this.refreshTimeout) | clearTimeout(this.refreshTimeout) | ||||
}, | }, | ||||
// Display "loading" overlay inside of the specified element | // Display "loading" overlay inside of the specified element | ||||
addLoader(elem) { | addLoader(elem) { | ||||
$(elem).css({position: 'relative'}).append($(loader).addClass('small')) | $(elem).css({position: 'relative'}).append($(loader).addClass('small')) | ||||
}, | }, | ||||
// Remove loader element added in addLoader() | // Remove loader element added in addLoader() | ||||
removeLoader(elem) { | removeLoader(elem) { | ||||
$(elem).find('.app-loader').remove() | $(elem).find('.app-loader').remove() | ||||
}, | }, | ||||
startLoading() { | startLoading() { | ||||
this.isLoading = true | this.isLoading = true | ||||
// Lock the UI with the 'loading...' element | // Lock the UI with the 'loading...' element | ||||
let loading = $('#app > .app-loader').show() | let loading = $('#app > .app-loader').removeClass('fadeOut') | ||||
if (!loading.length) { | if (!loading.length) { | ||||
$('#app').append($(loader)) | $('#app').append($(loader)) | ||||
} | } | ||||
}, | }, | ||||
// Hide "loading" overlay | // Hide "loading" overlay | ||||
stopLoading() { | stopLoading() { | ||||
$('#app > .app-loader').addClass('fadeOut') | $('#app > .app-loader').addClass('fadeOut') | ||||
this.isLoading = false | this.isLoading = false | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | methods: { | ||||
} | } | ||||
link.href = window.URL.createObjectURL(response.data) | link.href = window.URL.createObjectURL(response.data) | ||||
link.download = filename | link.download = filename | ||||
link.click() | link.click() | ||||
}) | }) | ||||
}, | }, | ||||
price(price, currency) { | price(price, currency) { | ||||
return (price/100).toLocaleString('de-DE', { style: 'currency', currency: currency || 'CHF' }) | return ((price || 0) / 100).toLocaleString('de-DE', { style: 'currency', currency: currency || 'CHF' }) | ||||
}, | }, | ||||
priceLabel(cost, units = 1, discount) { | priceLabel(cost, units = 1, discount) { | ||||
let index = '' | let index = '' | ||||
if (units < 0) { | if (units < 0) { | ||||
units = 1 | units = 1 | ||||
} | } | ||||
▲ Show 20 Lines • Show All 167 Lines • Show Last 20 Lines |