advent22/ui/src/components/AdminButton.vue

50 lines
1.1 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 setup lang="ts">
2024-08-27 00:25:42 +00:00
import { APIError } from "@/lib/api_error";
2024-08-23 16:38:04 +00:00
import { Credentials } from "@/lib/model";
import { advent22Store } from "@/lib/store";
import { ref } from "vue";
2023-09-10 00:24:56 +00:00
import BulmaButton from "./bulma/Button.vue";
import LoginModal from "./LoginModal.vue";
const modal_visible = ref(false);
const is_busy = ref(false);
const store = advent22Store();
function on_click() {
if (store.is_admin) {
store.logout();
} else {
// show login modal
is_busy.value = true;
modal_visible.value = true;
2023-09-11 23:36:36 +00:00
}
}
2023-09-11 23:10:17 +00:00
function on_submit(creds: Credentials) {
modal_visible.value = false;
2023-09-11 23:10:17 +00:00
store
.login(creds)
.catch((error) => APIError.alert(error))
.finally(() => (is_busy.value = false));
}
2023-09-13 15:20:52 +00:00
function on_cancel() {
modal_visible.value = false;
is_busy.value = false;
2023-09-10 00:24:56 +00:00
}
</script>