advent22/ui/src/components/AdminButton.vue

57 lines
1.2 KiB
Vue
Raw Normal View History

2023-09-10 00:24:56 +00:00
<template>
2023-09-13 15:20:52 +00:00
<LoginModal v-if="modal_visible" @submit="on_submit" @cancel="on_cancel" />
2023-09-10 00:24:56 +00:00
<BulmaButton
2023-09-22 20:42:28 +00:00
v-bind="$attrs"
2023-11-02 00:37:00 +00:00
:icon="'fa-solid fa-toggle-' + (store.is_admin ? 'on' : 'off')"
2023-09-12 22:35:57 +00:00
:busy="is_busy"
2023-09-20 22:03:32 +00:00
text="Admin"
@click.left="on_click"
2023-09-10 00:24:56 +00:00
/>
</template>
<script lang="ts">
2023-09-21 20:01:39 +00:00
import { Credentials } from "@/lib/api";
2023-11-02 00:37:00 +00:00
import { advent22Store } from "@/plugins/store";
2023-09-10 00:24:56 +00:00
import { Options, Vue } from "vue-class-component";
import BulmaButton from "./bulma/Button.vue";
import LoginModal from "./LoginModal.vue";
@Options({
components: {
BulmaButton,
LoginModal,
},
})
export default class extends Vue {
2023-09-11 23:36:36 +00:00
public modal_visible = false;
2023-09-12 22:35:57 +00:00
public is_busy = false;
2023-11-02 00:37:00 +00:00
public readonly store = advent22Store();
2023-09-11 23:36:36 +00:00
public on_click() {
2023-11-02 00:37:00 +00:00
if (this.store.is_admin) {
this.store.logout();
2023-09-11 23:36:36 +00:00
} else {
// show login modal
2023-09-12 22:35:57 +00:00
this.is_busy = true;
2023-09-11 23:36:36 +00:00
this.modal_visible = true;
}
}
2023-09-11 23:10:17 +00:00
2023-09-21 20:01:39 +00:00
public on_submit(creds: Credentials) {
2023-09-11 23:36:36 +00:00
this.modal_visible = false;
2023-09-11 23:10:17 +00:00
2023-11-02 00:37:00 +00:00
this.store
.login(creds)
2023-11-02 00:41:42 +00:00
.catch(this.$advent22.alert_user_error)
2023-11-02 00:37:00 +00:00
.finally(() => (this.is_busy = false));
2023-09-11 23:10:17 +00:00
}
2023-09-13 15:20:52 +00:00
public on_cancel() {
this.modal_visible = false;
this.is_busy = false;
}
2023-09-10 00:24:56 +00:00
}
</script>