firefish/packages/client/src/pages/channel-editor.vue

144 lines
3.3 KiB
Vue
Raw Normal View History

2023-07-20 04:17:05 +09:00
<template>
<MkStickyContainer>
<template #header
><MkPageHeader :actions="headerActions" :tabs="headerTabs"
/></template>
<MkSpacer :content-max="700">
<div class="_formRoot">
<MkInput v-model="name" class="_formBlock">
<template #label>{{ i18n.ts.name }}</template>
</MkInput>
<MkTextarea v-model="description" class="_formBlock">
<template #label>{{ i18n.ts.description }}</template>
</MkTextarea>
<div class="banner">
<MkButton v-if="bannerId == null" @click="setBannerImage"
2023-10-17 02:12:35 +09:00
><i :class="icon('ph-plus')"></i>
2023-07-20 04:17:05 +09:00
{{ i18n.ts._channel.setBanner }}</MkButton
>
<div v-else-if="bannerUrl">
<img :src="bannerUrl" style="width: 100%" />
<MkButton @click="removeBannerImage()"
2023-10-17 02:12:35 +09:00
><i :class="icon('ph-trash')"></i>
2023-07-20 04:17:05 +09:00
{{ i18n.ts._channel.removeBanner }}</MkButton
>
</div>
</div>
<div class="_formBlock">
<MkButton primary @click="save()"
2023-10-17 02:12:35 +09:00
><i :class="icon('ph-floppy-disk-back')"></i>
2023-07-20 04:17:05 +09:00
{{
channelId ? i18n.ts.save : i18n.ts.create
}}</MkButton
>
</div>
</div>
</MkSpacer>
</MkStickyContainer>
</template>
<script lang="ts" setup>
2023-09-04 17:47:24 +09:00
import { computed, ref, watch } from "vue";
2023-07-20 04:17:05 +09:00
import MkTextarea from "@/components/form/textarea.vue";
import MkButton from "@/components/MkButton.vue";
import MkInput from "@/components/form/input.vue";
import { selectFile } from "@/scripts/select-file";
import * as os from "@/os";
import { useRouter } from "@/router";
import { definePageMetadata } from "@/scripts/page-metadata";
import { i18n } from "@/i18n";
2023-10-17 02:12:35 +09:00
import icon from "@/scripts/icon";
2023-07-20 04:17:05 +09:00
const router = useRouter();
const props = defineProps<{
channelId?: string;
}>();
2023-09-04 17:47:24 +09:00
const channel = ref(null);
const name = ref(null);
const description = ref(null);
const bannerUrl = ref<string | null>(null);
const bannerId = ref<string | null>(null);
2023-07-20 04:17:05 +09:00
watch(
2023-08-25 07:19:12 +09:00
() => bannerId.value,
2023-07-20 04:17:05 +09:00
async () => {
2023-08-25 07:19:12 +09:00
if (bannerId.value == null) {
bannerUrl.value = null;
2023-07-20 04:17:05 +09:00
} else {
2023-08-25 07:19:12 +09:00
bannerUrl.value = (
2023-07-20 04:17:05 +09:00
await os.api("drive/files/show", {
2023-08-25 07:19:12 +09:00
fileId: bannerId.value,
2023-07-20 04:17:05 +09:00
})
).url;
}
},
);
async function fetchChannel() {
if (props.channelId == null) return;
2023-08-25 07:19:12 +09:00
channel.value = await os.api("channels/show", {
2023-07-20 04:17:05 +09:00
channelId: props.channelId,
});
2023-08-25 07:19:12 +09:00
name.value = channel.value.name;
description.value = channel.value.description;
bannerId.value = channel.value.bannerId;
bannerUrl.value = channel.value.bannerUrl;
2023-07-20 04:17:05 +09:00
}
fetchChannel();
function save() {
const params = {
2023-08-25 07:19:12 +09:00
name: name.value,
description: description.value,
bannerId: bannerId.value,
2023-07-20 04:17:05 +09:00
};
if (props.channelId) {
params.channelId = props.channelId;
os.api("channels/update", params).then(() => {
os.success();
});
} else {
os.api("channels/create", params).then((created) => {
os.success();
router.push(`/channels/${created.id}`);
});
}
}
function setBannerImage(evt) {
selectFile(evt.currentTarget ?? evt.target, null).then((file) => {
2023-08-25 07:19:12 +09:00
bannerId.value = file.id;
2023-07-20 04:17:05 +09:00
});
}
function removeBannerImage() {
2023-08-25 07:19:12 +09:00
bannerId.value = null;
2023-07-20 04:17:05 +09:00
}
2023-08-25 07:19:12 +09:00
const headerActions = computed(() => []);
2023-07-20 04:17:05 +09:00
2023-08-25 07:19:12 +09:00
const headerTabs = computed(() => []);
2023-07-20 04:17:05 +09:00
definePageMetadata(
computed(() =>
props.channelId
? {
title: i18n.ts._channel.edit,
2023-10-17 02:12:35 +09:00
icon: `${icon("ph-television")}`,
2023-07-20 04:17:05 +09:00
}
: {
title: i18n.ts._channel.create,
2023-10-17 02:12:35 +09:00
icon: `${icon("ph-television")}`,
2023-07-20 04:17:05 +09:00
},
),
);
</script>