Files
opencloud/ui/store/index.js
T
Jörn Friedrich Dreyer 0f64397e79 update ui
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
2020-07-10 15:51:13 +02:00

86 lines
1.9 KiB
JavaScript

import {
// eslint-disable-next-line camelcase
AccountsService_ListAccounts
} from '../client/accounts'
import axios from 'axios'
const state = {
config: null,
initialized: false,
accounts: {}
}
const getters = {
config: state => state.config,
isInitialized: state => state.initialized,
getAccountsSorted: state => {
return Object.values(state.accounts).sort((a1, a2) => {
if (a1.onPremisesSamAccountName === a2.onPremisesSamAccountName) {
return a1.id.localeCompare(a2.id)
}
return a1.onPremisesSamAccountName.localeCompare(a2.onPremisesSamAccountName)
})
}
}
const mutations = {
LOAD_CONFIG (state, config) {
state.config = config
},
SET_INITIALIZED (state, value) {
state.initialized = value
},
SET_ACCOUNTS (state, accounts) {
state.accounts = accounts
}
}
const actions = {
loadConfig ({ commit }, config) {
commit('LOAD_CONFIG', config)
},
async initialize ({ commit, dispatch }) {
await dispatch('fetchAccounts')
commit('SET_INITIALIZED', true)
},
async fetchAccounts ({ commit, dispatch, rootGetters }) {
injectAuthToken(rootGetters)
const response = await AccountsService_ListAccounts({
$domain: rootGetters.configuration.server,
body: {}
})
if (response.status === 201) {
const accounts = response.data.accounts
commit('SET_ACCOUNTS', accounts || [])
} else {
dispatch('showMessage', {
title: 'Failed to fetch accounts.',
desc: response.statusText,
status: 'danger'
}, { root: true })
}
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}
function injectAuthToken (rootGetters) {
axios.interceptors.request.use(config => {
if (typeof config.headers.Authorization === 'undefined') {
const token = rootGetters.user.token
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
}
return config
})
}