salix-front/src/composables/useState.js

73 lines
1.3 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,
2022-03-24 15:49:33 +00:00
});
const roles = ref([]);
2022-04-19 11:50:54 +00:00
const drawer = ref(true);
2022-03-24 15:49:33 +00:00
export function useState() {
function getUser() {
return computed(() => {
return {
id: user.value.id,
name: user.value.name,
2022-03-24 15:49:33 +00:00
nickname: user.value.nickname,
lang: user.value.lang,
darkMode: user.value.darkMode,
2022-03-24 15:49:33 +00:00
};
});
}
function setUser(data) {
user.value = {
id: data.id,
name: data.name,
2022-03-24 15:49:33 +00:00
nickname: data.nickname,
lang: data.lang,
darkMode: data.darkMode,
2022-03-24 15:49:33 +00:00
};
}
function getRoles() {
return computed(() => {
return roles.value;
});
}
function setRoles(data) {
roles.value = data;
}
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,
2022-10-27 12:59:19 +00:00
set,
get,
unset,
2022-04-19 11:50:54 +00:00
drawer
2022-03-24 15:49:33 +00:00
};
}