Updated useState composable

This commit is contained in:
Joan Sanchez 2022-03-09 10:16:33 +01:00
parent 286605695b
commit 581efb8ddb
3 changed files with 31 additions and 15 deletions

View File

@ -46,7 +46,7 @@ import { useSession } from '@/core/composables/useSession';
import UserPanel from '@/components/UserPanel.vue'; import UserPanel from '@/components/UserPanel.vue';
const session = useSession(); const session = useSession();
const { getUser } = useState(); const state = useState();
const user = getUser; const user = state.getUser();
const token = session.getToken(); const token = session.getToken();
</script> </script>

View File

@ -62,7 +62,7 @@ const darkMode = computed({
}, },
}); });
const user = state.getUser; const user = state.getUser();
const token = session.getToken(); const token = session.getToken();
onMounted(() => { onMounted(() => {

View File

@ -6,15 +6,32 @@ interface User {
nickname: string; nickname: string;
} }
interface useState {
getUser: () => ComputedRef<User>;
setUser: (data: User) => void;
getRoles: () => ComputedRef<string[]>;
setRoles: (data: string[]) => void;
}
const user: Ref<User> = ref({ const user: Ref<User> = ref({
id: 0, id: 0,
username: '', username: '',
nickname: '', nickname: '',
}); });
const roles = ref([]); const roles: Ref<string[]> = ref([]);
export function useState(): useState {
function getUser(): ComputedRef<User> {
return computed(() => {
return {
id: user.value.id,
username: user.value.username,
nickname: user.value.nickname,
};
});
}
export function useState() {
function setUser(data: User): void { function setUser(data: User): void {
user.value = { user.value = {
id: data.id, id: data.id,
@ -23,21 +40,20 @@ export function useState() {
}; };
} }
const getUser: ComputedRef<User> = computed(() => { function getRoles(): ComputedRef<string[]> {
return { return computed(() => {
id: user.value.id, return roles.value;
username: user.value.username, });
nickname: user.value.nickname, }
};
});
/* function setRoles(data) { function setRoles(data: string[]): void {
roles.value = data; roles.value = data;
} */ }
return { return {
roles,
getUser, getUser,
setUser, setUser,
getRoles,
setRoles,
}; };
} }