advent22/ui/src/components/AdminButton.vue

58 lines
1.2 KiB
Vue
Raw Normal View History

2023-09-10 00:24:56 +00:00
<template>
2023-09-11 23:36:36 +00:00
<LoginModal
v-if="modal_visible"
@submit="on_submit"
@cancel="modal_visible = false"
/>
2023-09-10 00:24:56 +00:00
<BulmaButton
class="button is-light"
2023-09-11 23:36:36 +00:00
@click.left="on_click"
:icon="'fa-solid fa-toggle-' + (modelValue ? 'on' : 'off')"
2023-09-10 00:24:56 +00:00
text="Admin"
/>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import BulmaButton from "./bulma/Button.vue";
import LoginModal from "./LoginModal.vue";
@Options({
components: {
BulmaButton,
LoginModal,
},
2023-09-11 23:10:17 +00:00
props: {
modelValue: Boolean,
},
emits: ["update:modelValue"],
2023-09-10 00:24:56 +00:00
})
export default class extends Vue {
2023-09-11 23:10:17 +00:00
// true, iff Benutzer Admin ist
public modelValue!: boolean;
2023-09-11 23:36:36 +00:00
public modal_visible = false;
public on_click() {
if (this.modelValue) {
// logout
this.$advent22.clear_api_auth();
this.$emit("update:modelValue", false);
} else {
// show login modal
this.modal_visible = true;
}
}
2023-09-11 23:10:17 +00:00
2023-09-11 23:36:36 +00:00
public on_submit(username: string, password: string) {
this.modal_visible = false;
this.$advent22.set_api_auth(username, password);
2023-09-11 23:10:17 +00:00
this.$advent22
.api_get<boolean>("admin/is_admin")
.then((is_admin) => this.$emit("update:modelValue", is_admin));
}
2023-09-10 00:24:56 +00:00
}
</script>