Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: post tag management page #5593

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 118 additions & 145 deletions ui/console-src/modules/contents/posts/tags/TagList.vue
Original file line number Diff line number Diff line change
@@ -1,63 +1,75 @@
<script lang="ts" setup>
// core libs
import { onMounted, ref } from "vue";

// components
import type { Tag } from "@halo-dev/api-client";
import { onMounted, ref, watch } from "vue";
import {
IconAddCircle,
IconBookRead,
IconGrid,
IconList,
VButton,
VCard,
VEmpty,
VPageHeader,
VSpace,
VStatusDot,
VEntity,
VEntityField,
VLoading,
VDropdownItem,
VPagination,
} from "@halo-dev/components";
import HasPermission from "@/components/permission/HasPermission.vue";
import TagEditingModal from "./components/TagEditingModal.vue";
import PostTag from "./components/PostTag.vue";

// types
import type { Tag } from "@halo-dev/api-client";
import { usePostTag } from "./composables/use-post-tag";

import { formatDatetime } from "@/utils/date";

import { useRouteQuery } from "@vueuse/router";
import { apiClient } from "@/utils/api-client";
import { usePermission } from "@/utils/permission";

const { currentUserHasPermission } = usePermission();
import { usePostTag } from "./composables/use-post-tag";
import TagListItem from "./components/TagListItem.vue";

const viewTypes = [
{
name: "list",
icon: IconList,
},
{
name: "grid",
icon: IconGrid,
},
];
const editingModal = ref(false);
const selectedTag = ref<Tag | null>(null);

const viewType = ref("list");
const selectedTagNames = ref<string[]>([]);
const checkedAll = ref(false);

const { tags, isLoading, handleFetchTags, handleDelete } = usePostTag();
const page = useRouteQuery<number>("page", 1, {
transform: Number,
});
const size = useRouteQuery<number>("size", 20, {
transform: Number,
});

const editingModal = ref(false);
const selectedTag = ref<Tag | null>(null);
const {
tags,
total,
hasNext,
hasPrevious,
isLoading,
handleFetchTags,
handleDelete,
handleDeleteInBatch,
} = usePostTag({
page,
size,
});

const handleOpenEditingModal = (tag: Tag | null) => {
selectedTag.value = tag;
editingModal.value = true;
};

const handleSelectPrevious = () => {
const handleDeleteTagInBatch = () => {
handleDeleteInBatch(selectedTagNames.value).then(() => {
selectedTagNames.value = [];
});
};

const handleCheckAllChange = () => {
if (checkedAll.value) {
selectedTagNames.value = tags.value?.map((tag) => tag.metadata.name) || [];
} else {
selectedTagNames.value = [];
}
};

const handleSelectPrevious = async () => {
if (!hasPrevious.value) {
return;
}

if (!tags.value) return;

const currentIndex = tags.value.findIndex(
Expand All @@ -69,12 +81,18 @@ const handleSelectPrevious = () => {
return;
}

if (currentIndex <= 0) {
selectedTag.value = null;
if (currentIndex === 0 && hasPrevious.value) {
page.value--;
await handleFetchTags();
selectedTag.value = tags.value[tags.value.length - 1];
}
};

const handleSelectNext = () => {
const handleSelectNext = async () => {
if (!hasNext.value) {
return;
}

if (!tags.value) return;

if (!selectedTag.value) {
Expand All @@ -87,6 +105,12 @@ const handleSelectNext = () => {
if (currentIndex !== tags.value.length - 1) {
selectedTag.value = tags.value[currentIndex + 1];
}

if (currentIndex === tags.value.length - 1 && hasNext.value) {
page.value++;
await handleFetchTags();
selectedTag.value = tags.value[0];
}
};

const onEditingModalClose = () => {
Expand All @@ -108,6 +132,10 @@ onMounted(async () => {
editingModal.value = true;
}
});

watch(selectedTagNames, (newVal) => {
checkedAll.value = newVal.length === tags.value?.length;
});
</script>
<template>
<TagEditingModal
Expand Down Expand Up @@ -139,27 +167,23 @@ onMounted(async () => {
<template #header>
<div class="block w-full bg-gray-50 px-4 py-3">
<div
class="relative flex flex-col items-start sm:flex-row sm:items-center"
class="relative flex h-9 flex-col flex-wrap items-start gap-4 sm:flex-row sm:items-center"
>
<div class="flex w-full flex-1 sm:w-auto">
<span class="text-base font-medium">
{{
$t("core.post_tag.header.title", { count: tags?.length || 0 })
}}
</span>
</div>
<div class="flex flex-row gap-2">
<div
v-for="(item, index) in viewTypes"
:key="index"
:class="{
'bg-gray-200 font-bold text-black': viewType === item.name,
}"
class="cursor-pointer rounded p-1 hover:bg-gray-200"
@click="viewType = item.name"
>
<component :is="item.icon" />
<HasPermission :permissions="['system:posts:manage']">
<div class="hidden items-center sm:flex">
<input
v-model="checkedAll"
type="checkbox"
@change="handleCheckAllChange"
/>
</div>
</HasPermission>
<div class="flex w-full flex-1 items-center sm:w-auto">
<VSpace v-if="selectedTagNames.length > 0">
<VButton type="danger" @click="handleDeleteTagInBatch">
{{ $t("core.common.buttons.delete") }}
</VButton>
</VSpace>
</div>
</div>
</div>
Expand All @@ -172,7 +196,7 @@ onMounted(async () => {
>
<template #actions>
<VSpace>
<VButton @click="handleFetchTags">
<VButton @click="() => handleFetchTags">
{{ $t("core.common.buttons.refresh") }}
</VButton>
<VButton type="primary" @click="editingModal = true">
Expand All @@ -186,93 +210,42 @@ onMounted(async () => {
</VEmpty>
</Transition>

<div v-else>
<Transition v-if="viewType === 'list'" appear name="fade">
<ul
class="box-border h-full w-full divide-y divide-gray-100"
role="list"
>
<li v-for="(tag, index) in tags" :key="index">
<VEntity
:is-selected="selectedTag?.metadata.name === tag.metadata.name"
>
<template #start>
<VEntityField>
<template #title>
<PostTag :tag="tag" />
</template>
<template #description>
<a
v-if="tag.status?.permalink"
:href="tag.status?.permalink"
:title="tag.status?.permalink"
target="_blank"
class="truncate text-xs text-gray-500 group-hover:text-gray-900"
>
{{ tag.status.permalink }}
</a>
</template>
</VEntityField>
</template>
<template #end>
<VEntityField v-if="tag.metadata.deletionTimestamp">
<template #description>
<VStatusDot
v-tooltip="$t('core.common.status.deleting')"
state="warning"
animate
/>
</template>
</VEntityField>
<VEntityField
:description="
$t('core.common.fields.post_count', {
count: tag.status?.postCount || 0,
})
"
/>
<VEntityField>
<template #description>
<span class="truncate text-xs tabular-nums text-gray-500">
{{ formatDatetime(tag.metadata.creationTimestamp) }}
</span>
</template>
</VEntityField>
</template>
<template
v-if="currentUserHasPermission(['system:posts:manage'])"
#dropdownItems
>
<VDropdownItem
v-permission="['system:posts:manage']"
@click="handleOpenEditingModal(tag)"
>
{{ $t("core.common.buttons.edit") }}
</VDropdownItem>
<VDropdownItem
v-permission="['system:posts:manage']"
type="danger"
@click="handleDelete(tag)"
>
{{ $t("core.common.buttons.delete") }}
</VDropdownItem>
</template>
</VEntity>
</li>
</ul>
</Transition>

<Transition v-else appear name="fade">
<div class="flex flex-wrap gap-3 p-4" role="list">
<PostTag
v-for="(tag, index) in tags"
:key="index"
<Transition appear name="fade">
<ul
class="box-border h-full w-full divide-y divide-gray-100"
role="list"
>
<li v-for="(tag, index) in tags" :key="index">
<TagListItem
:tag="tag"
@click="handleOpenEditingModal(tag)"
/>
</div>
</Transition>
</div>
:is-selected="selectedTag?.metadata.name === tag.metadata.name"
@editing="handleOpenEditingModal"
@delete="handleDelete"
>
<template #checkbox>
<input
v-model="selectedTagNames"
:value="tag.metadata.name"
type="checkbox"
/>
</template>
</TagListItem>
</li>
</ul>
</Transition>
<template #footer>
<VPagination
v-model:page="page"
v-model:size="size"
:page-label="$t('core.components.pagination.page_label')"
:size-label="$t('core.components.pagination.size_label')"
:total-label="
$t('core.components.pagination.total_label', { total: total })
"
:total="total"
:size-options="[20, 30, 50, 100]"
/>
</template>
</VCard>
</div>
</template>
Loading