salix-front/src/composables/useState.js

92 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-03-24 15:49:33 +00:00
import { ref, computed } from 'vue';
2022-10-27 12:59:19 +00:00
const state = ref({});
2022-03-24 15:49:33 +00:00
const user = ref({
id: 0,
name: '',
2022-03-24 15:49:33 +00:00
nickname: '',
lang: '',
darkMode: null,
2023-11-16 19:44:35 +00:00
companyFk: null,
2024-01-22 14:22:22 +00:00
warehouseFk: null,
2022-03-24 15:49:33 +00:00
});
2024-06-05 06:58:12 +00:00
if (sessionStorage.getItem('user'))
user.value = JSON.parse(sessionStorage.getItem('user'));
2022-03-24 15:49:33 +00:00
const roles = ref([]);
const acls = ref([]);
2024-04-19 10:09:04 +00:00
const tokenConfig = ref({});
2022-04-19 11:50:54 +00:00
const drawer = ref(true);
2022-11-15 13:59:04 +00:00
const headerMounted = ref(false);
2022-04-19 11:50:54 +00:00
2022-03-24 15:49:33 +00:00
export function useState() {
function getUser() {
return computed(() => {
2024-05-15 09:46:04 +00:00
return user.value;
2022-03-24 15:49:33 +00:00
});
}
function setUser(data) {
2024-06-05 06:58:12 +00:00
const currentUser = { ...JSON.parse(sessionStorage.getItem('user')), ...data };
sessionStorage.setItem('user', JSON.stringify(currentUser));
user.value = currentUser;
return currentUser;
2022-03-24 15:49:33 +00:00
}
function getRoles() {
return computed(() => {
return roles.value;
});
}
function setRoles(data) {
roles.value = data;
}
function getAcls() {
return computed(() => acls.value);
}
function setAcls(data) {
acls.value = data;
}
2024-04-19 10:09:04 +00:00
function getTokenConfig() {
return computed(() => {
return tokenConfig.value;
});
}
function setTokenConfig(data) {
tokenConfig.value = data;
}
2022-03-24 15:49:33 +00:00
2022-10-27 12:59:19 +00:00
function set(name, data) {
state.value[name] = ref(data);
}
function get(name) {
return state.value[name];
}
function unset(name) {
delete state.value[name];
}
2022-03-24 15:49:33 +00:00
return {
getUser,
setUser,
getRoles,
setRoles,
getAcls,
setAcls,
2024-04-19 10:09:04 +00:00
getTokenConfig,
setTokenConfig,
2022-10-27 12:59:19 +00:00
set,
get,
unset,
2022-11-15 13:59:04 +00:00
drawer,
2023-11-16 19:44:35 +00:00
headerMounted,
2022-03-24 15:49:33 +00:00
};
}