Updated components to composition API

This commit is contained in:
Joan Sanchez 2022-03-08 08:57:00 +01:00
parent e89e325cad
commit b89163704d
2 changed files with 62 additions and 70 deletions

View File

@ -27,7 +27,7 @@
<q-btn dense flat no-wrap>
<q-avatar size="lg">
<q-img
:src="`/api/Images/user/160x160/${user.id}/download?access_token=${token}`"
:src="`/api/Images/user/160x160/${user.id}/download?access_token=${user.token}`"
spinner-color="white"
/>
</q-avatar>
@ -40,8 +40,9 @@
</q-header>
</template>
<script lang="ts">
import { Options, Vue } from 'vue-class-component';
<script lang="ts" setup>
import { computed } from 'vue';
import { useStore } from 'vuex';
import UserPanel from '@/components/UserPanel.vue';
interface UserProfile {
@ -51,18 +52,13 @@ interface UserProfile {
token: string;
}
@Options({
components: {
UserPanel,
},
})
export default class Topbar extends Vue {
get user(): UserProfile {
return this.$store.state.user;
}
get token(): string {
return this.$store.getters.token;
}
}
const store = useStore();
const user = computed((): UserProfile => {
return {
id: store.state.user.id,
username: store.state.user.username,
nickname: store.state.user.nickname,
token: store.getters.token,
};
});
</script>

View File

@ -2,16 +2,16 @@
<q-menu>
<div class="row no-wrap q-pa-md">
<div class="column">
<div class="text-h6 q-mb-md">{{ $t('components.userPanel.settings') }}</div>
<div class="text-h6 q-mb-md">{{ t('components.userPanel.settings') }}</div>
<q-toggle
:label="$t(`globals.lang['${language}']`)"
:label="t(`globals.lang['${locale}']`)"
icon="public"
color="orange"
false-value="es"
true-value="en"
v-model="language"
v-model="locale"
/>
<q-toggle v-model="mode" checked-icon="dark_mode" color="orange" unchecked-icon="light_mode" />
<q-toggle v-model="darkMode" checked-icon="dark_mode" color="orange" unchecked-icon="light_mode" />
<q-btn color="orange" outline size="sm" label="Settings" icon="settings" />
</div>
@ -21,7 +21,7 @@
<div class="column items-center" style="min-width: 150px">
<q-avatar size="80px">
<q-img
:src="`/api/Images/user/160x160/${user.id}/download?access_token=${token}`"
:src="`/api/Images/user/160x160/${user.id}/download?access_token=${user.token}`"
spinner-color="white"
/>
</q-avatar>
@ -37,10 +37,18 @@
</q-menu>
</template>
<script lang="ts">
import { Options, Vue } from 'vue-class-component';
<script lang="ts" setup>
import { onMounted, computed } from 'vue';
import { Dark, useQuasar } from 'quasar';
import { useStore } from 'vuex';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import axios from 'axios';
import { Dark } from 'quasar';
const quasar = useQuasar();
const store = useStore();
const router = useRouter();
const { t, locale } = useI18n();
interface UserProfile {
id: number;
@ -49,54 +57,42 @@ interface UserProfile {
token: string;
}
@Options({})
export default class UserPanel extends Vue {
private lang = 'es';
const darkMode = computed({
get(): boolean {
return Dark.isActive;
},
set(value: boolean): void {
Dark.set(value);
},
});
mounted(): void {
const user = computed((): UserProfile => {
return {
id: store.state.user.id,
username: store.state.user.username,
nickname: store.state.user.nickname,
token: store.getters.token,
};
});
onMounted(() => {
axios
.get('/api/accounts/acl')
.then((response) => {
this.$store.dispatch('updateUserData', response.data);
store.dispatch('updateUserData', response.data);
})
.catch(() => {
this.$q.notify({
message: this.$t('errors.statusUnauthorized'),
quasar.notify({
message: t('errors.statusUnauthorized'),
type: 'negative',
});
this.$store.dispatch('logOut');
this.$router.push('/login');
store.dispatch('logOut');
router.push('/login');
});
}
});
logout(): void {
this.$store.dispatch('logOut');
this.$router.push('/login');
}
get user(): UserProfile {
return this.$store.state.user;
}
get token(): string {
return this.$store.getters.token;
}
get mode(): boolean {
return Dark.isActive;
}
set mode(value: boolean) {
Dark.set(value);
}
get language(): string {
return this.lang;
}
set language(value: string) {
this.lang = value;
this.$i18n.locale = value;
}
function logout(): void {
store.dispatch('logOut');
router.push('/login');
}
</script>