Hangar/frontend/pages/staff.vue

88 lines
2.4 KiB
Vue
Raw Normal View History

2021-01-22 01:19:00 +08:00
<template>
<v-data-table
v-if="staff"
:headers="headers"
:items="staff.result"
:options.sync="options"
:server-items-length="staff.pagination.count"
:loading="loading"
class="elevation-1"
2021-02-05 04:30:47 +08:00
>
<template #item.roles="{ item }">
{{ item.roles.map((r) => r.title).join(', ') }}
</template>
<template #item.joinDate="{ item }">
{{ $util.prettyDate(new Date(item.joinDate)) }}
</template>
</v-data-table>
2021-01-22 01:19:00 +08:00
</template>
<script lang="ts">
import { Component, Vue, Watch } from 'nuxt-property-decorator';
import { PaginatedResult, User } from 'hangar-api';
2021-02-05 04:30:47 +08:00
import { DataOptions, DataTableHeader } from 'vuetify';
import { Context } from '@nuxt/types';
2021-01-22 01:19:00 +08:00
@Component
export default class StaffPage extends Vue {
2021-02-05 04:30:47 +08:00
headers: DataTableHeader[] = [
{ text: 'Username', value: 'name' },
{ text: 'Roles', value: 'roles' },
2021-02-05 04:30:47 +08:00
{ text: 'Joined', value: 'joinDate' },
];
staff?: PaginatedResult<User>;
loading = false;
2021-01-31 10:00:11 +08:00
options = { page: 1, itemsPerPage: 10 } as DataOptions;
initialLoad = true;
head() {
return {
title: this.$t('pages.staff'),
};
}
@Watch('options', { deep: true })
onOptionsChanged() {
if (this.initialLoad) {
this.initialLoad = false;
return;
}
this.loading = true;
this.$api.request<PaginatedResult<User>>('staff', false, 'get', this.requestOptions).then((staff) => {
this.staff = staff;
this.loading = false;
});
}
get requestOptions() {
if (!this.options) {
return {};
}
let sort = '';
if (this.options.sortBy.length === 1) {
sort = this.options.sortBy[0];
if (this.options.sortDesc[0]) {
sort = '-' + sort;
}
}
return {
limit: this.options.itemsPerPage,
offset: (this.options.page - 1) * this.options.itemsPerPage,
sort,
};
}
async asyncData({ $api, $util }: Context): Promise<{ staff: PaginatedResult<User> | void }> {
const staff = await $api
.request<PaginatedResult<User>>('staff', false, 'get', { limit: 10, offset: 0 })
.catch((err) => $util.handleRequestError(err, 'Could not load staff'));
return { staff };
}
}
2021-01-22 01:19:00 +08:00
</script>
<style lang="scss" scoped></style>