hippofish/packages/client/src/widgets/user-list.vue

117 lines
2.9 KiB
Vue
Raw Normal View History

2023-01-16 20:19:19 +01:00
<template>
<MkContainer :show-header="widgetProps.showHeader" class="mkw-userList">
2023-04-08 02:01:42 +02:00
<template #header
><i class="ph-user-list ph-bold ph-lg"></i>
{{ list ? list.name : i18n.ts._widgets.userList }}</template
>
<template #func="{ buttonStyleClass }"
><button
class="_button"
:class="buttonStyleClass"
@click="configure()"
>
<i class="ph-gear-six ph-bold ph-lg"></i></button
></template>
2023-02-15 21:46:27 +01:00
2023-01-16 20:19:19 +01:00
<div class="wsdlkfj">
<div v-if="widgetProps.listId == null" class="init">
2023-04-08 02:01:42 +02:00
<MkButton primary @click="chooseList">{{
i18n.ts._widgets._userList.chooseList
}}</MkButton>
2023-01-16 20:19:19 +01:00
</div>
2023-04-08 02:01:42 +02:00
<MkLoading v-else-if="fetching" />
2023-01-16 20:19:19 +01:00
<div v-else class="users">
2023-04-08 02:01:42 +02:00
<MkAvatars :user-ids="users" class="userAvatars" />
2023-01-16 20:19:19 +01:00
</div>
</div>
</MkContainer>
</template>
2023-02-15 21:46:27 +01:00
2023-01-16 20:19:19 +01:00
<script lang="ts" setup>
2023-04-08 02:01:42 +02:00
import { useWidgetPropsManager, Widget, WidgetComponentExpose } from "./widget";
import { GetFormResultType } from "@/scripts/form";
import MkContainer from "@/components/MkContainer.vue";
import MkAvatars from "@/components/MkAvatars.vue";
import * as os from "@/os";
import { useInterval } from "@/scripts/use-interval";
import { i18n } from "@/i18n";
import MkButton from "@/components/MkButton.vue";
2023-01-16 20:19:19 +01:00
2023-04-08 02:01:42 +02:00
const name = "userList";
2023-01-16 20:19:19 +01:00
const widgetPropsDef = {
showHeader: {
2023-04-08 02:01:42 +02:00
type: "boolean" as const,
2023-01-16 20:19:19 +01:00
default: true,
},
listId: {
2023-04-08 02:01:42 +02:00
type: "string" as const,
2023-01-16 20:19:19 +01:00
default: null,
hidden: true,
},
};
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
// 現時点ではvueの制限によりimportしたtypeをジェネリックに渡せない
//const props = defineProps<WidgetComponentProps<WidgetProps>>();
//const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
2023-04-08 02:01:42 +02:00
const props = defineProps<{ widget?: Widget<WidgetProps> }>();
const emit = defineEmits<{ (ev: "updateProps", props: WidgetProps) }>();
const { widgetProps, configure, save } = useWidgetPropsManager(
name,
2023-01-16 20:19:19 +01:00
widgetPropsDef,
props,
2023-04-08 02:01:42 +02:00
emit
2023-01-16 20:19:19 +01:00
);
let list = $ref();
let users = $ref([]);
let fetching = $ref(true);
async function chooseList() {
2023-04-08 02:01:42 +02:00
const lists = await os.api("users/lists/list");
2023-01-16 20:19:19 +01:00
const { canceled, result: list } = await os.select({
title: i18n.ts.selectList,
2023-04-08 02:01:42 +02:00
items: lists.map((x) => ({
value: x,
text: x.name,
2023-01-16 20:19:19 +01:00
})),
default: widgetProps.listId,
});
if (canceled) return;
widgetProps.listId = list.id;
save();
fetch();
}
const fetch = () => {
if (widgetProps.listId == null) {
fetching = false;
return;
}
2023-04-08 02:01:42 +02:00
os.api("users/lists/show", {
2023-01-16 20:19:19 +01:00
listId: widgetProps.listId,
2023-04-08 02:01:42 +02:00
}).then((_list) => {
2023-01-16 20:19:19 +01:00
list = _list;
2023-04-08 02:01:42 +02:00
os.api("users/show", {
2023-01-16 20:19:19 +01:00
userIds: list.userIds,
2023-04-08 02:01:42 +02:00
}).then((_users) => {
2023-01-16 20:31:21 +01:00
users = list.userIds;
2023-01-16 20:19:19 +01:00
fetching = false;
});
});
};
useInterval(fetch, 1000 * 60, {
immediate: true,
afterMounted: true,
});
defineExpose<WidgetComponentExpose>({
name,
configure,
id: props.widget ? props.widget.id : null,
});
</script>
2023-02-15 21:46:27 +01:00
2023-01-16 20:40:06 +01:00
<style lang="scss" module>
2023-01-16 20:19:19 +01:00
.wsdlkfj {
> .init {
padding: 16px;
}
}
</style>