halo/ui/console-src/modules/interface/menus/components/MenuList.vue

261 lines
7.1 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
import {
Dialog,
Toast,
VButton,
VCard,
VDropdownItem,
VEmpty,
VEntity,
VEntityField,
VLoading,
VSpace,
VStatusDot,
VTag,
} from "@halo-dev/components";
import MenuEditingModal from "./MenuEditingModal.vue";
import { onMounted, ref } from "vue";
import type { Menu } from "@halo-dev/api-client";
import { apiClient } from "@/utils/api-client";
import { useRouteQuery } from "@vueuse/router";
import { usePermission } from "@/utils/permission";
import { useI18n } from "vue-i18n";
import { useQuery } from "@tanstack/vue-query";
const { currentUserHasPermission } = usePermission();
const { t } = useI18n();
const props = withDefaults(
defineProps<{
selectedMenu?: Menu;
}>(),
{
selectedMenu: undefined,
}
);
const emit = defineEmits<{
(event: "update:selectedMenu", menu: Menu): void;
}>();
const selectedMenuToUpdate = ref<Menu>();
const menuEditingModal = ref<boolean>(false);
const {
data: menus,
isLoading,
refetch,
} = useQuery<Menu[]>({
queryKey: ["menus"],
queryFn: async () => {
const { data } = await apiClient.extension.menu.listV1alpha1Menu({
page: 0,
size: 0,
});
return data.items;
},
onSuccess(data) {
if (props.selectedMenu) {
const updatedMenu = data?.find(
(menu) => menu.metadata.name === props.selectedMenu?.metadata.name
);
if (updatedMenu) {
emit("update:selectedMenu", updatedMenu);
}
}
},
refetchInterval(data) {
const deletingMenus = data?.filter(
(menu) => !!menu.metadata.deletionTimestamp
);
return deletingMenus?.length ? 1000 : false;
},
});
const menuQuery = useRouteQuery("menu");
const handleSelect = (menu: Menu) => {
emit("update:selectedMenu", menu);
menuQuery.value = menu.metadata.name;
};
const handleDeleteMenu = async (menu: Menu) => {
Dialog.warning({
title: t("core.menu.operations.delete_menu.title"),
description: t("core.menu.operations.delete_menu.description"),
confirmType: "danger",
confirmText: t("core.common.buttons.confirm"),
cancelText: t("core.common.buttons.cancel"),
onConfirm: async () => {
try {
await apiClient.extension.menu.deleteV1alpha1Menu({
refactor: method parameters of api client (halo-dev/console#605) <!-- Thanks for sending a pull request! Here are some tips for you: 1. 如果这是你的第一次,请阅读我们的贡献指南:<https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>。 1. If this is your first time, please read our contributor guidelines: <https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>. 2. 请根据你解决问题的类型为 Pull Request 添加合适的标签。 2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request. 3. 请确保你已经添加并运行了适当的测试。 3. Ensure you have added or ran the appropriate tests for your PR. --> #### What type of PR is this? /kind improvement /milestone 2.0 <!-- 添加其中一个类别: Add one of the following kinds: /kind bug /kind cleanup /kind documentation /kind feature /kind optimization 适当添加其中一个或多个类别(可选): Optionally add one or more of the following kinds if applicable: /kind api-change /kind deprecation /kind failing-test /kind flake /kind regression --> #### What this PR does / why we need it: 修改 api-client 的请求参数结构,改为所有参数由一个对象包裹,而不是将各个参数作为方法的参数,防止因为后端参数结构发生改变,或者生成 api-client 时参数顺序发生改变导致请求异常。如: ```diff await apiClient.extension.storage.group.updatestorageHaloRunV1alpha1Group( - formState.value.metadata.name, - formState.value + { + name: formState.value.metadata.name, + group: formState.value, + } ); ``` #### Which issue(s) this PR fixes: <!-- PR 合并时自动关闭 issue。 Automatically closes linked issue when PR is merged. 用法:`Fixes #<issue 号>`,或者 `Fixes (粘贴 issue 完整链接)` Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> None #### Screenshots: <!-- 如果此 PR 有 UI 的改动,最好截图说明这个 PR 的改动。 If there are UI changes to this PR, it is best to take a screenshot to illustrate the changes to this PR. eg. Before: ![screenshot-before](https://user-images.githubusercontent.com/screenshot.png) After: ![screenshot-after](https://user-images.githubusercontent.com/screenshot.png) --> None #### Special notes for your reviewer: /cc @halo-dev/sig-halo-admin #### Does this PR introduce a user-facing change? <!-- 如果当前 Pull Request 的修改不会造成用户侧的任何变更,在 `release-note` 代码块儿中填写 `NONE`。 否则请填写用户侧能够理解的 Release Note。如果当前 Pull Request 包含破坏性更新(Break Change), Release Note 需要以 `action required` 开头。 If no, just write "NONE" in the release-note block below. If yes, a release note is required: Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required". --> ```release-note None ```
2022-09-06 02:26:11 +00:00
name: menu.metadata.name,
});
const deleteItemsPromises = menu.spec.menuItems?.map((item) =>
apiClient.extension.menuItem.deleteV1alpha1MenuItem({
name: item,
})
);
if (!deleteItemsPromises) {
return;
}
await Promise.all(deleteItemsPromises);
Toast.success(t("core.common.toast.delete_success"));
} catch (e) {
console.error("Failed to delete menu", e);
} finally {
await refetch();
}
},
});
};
const handleOpenEditingModal = (menu?: Menu) => {
selectedMenuToUpdate.value = menu;
menuEditingModal.value = true;
};
onMounted(async () => {
await refetch();
if (menuQuery.value) {
const menu = menus.value?.find((m) => m.metadata.name === menuQuery.value);
if (menu) {
handleSelect(menu);
}
return;
}
if (menus.value?.length) {
handleSelect(menus.value[0]);
}
});
// primary menu
const { data: primaryMenuName, refetch: refetchPrimaryMenuName } = useQuery({
queryKey: ["primary-menu-name"],
queryFn: async () => {
const { data } = await apiClient.extension.configMap.getV1alpha1ConfigMap({
name: "system",
});
if (!data.data?.menu) {
return "";
}
const menuConfig = JSON.parse(data.data.menu);
return menuConfig.primary;
},
});
const handleSetPrimaryMenu = async (menu: Menu) => {
const { data: systemConfigMap } =
await apiClient.extension.configMap.getV1alpha1ConfigMap({
name: "system",
});
if (systemConfigMap.data) {
const menuConfigToUpdate = JSON.parse(systemConfigMap.data?.menu || "{}");
menuConfigToUpdate.primary = menu.metadata.name;
systemConfigMap.data["menu"] = JSON.stringify(menuConfigToUpdate);
await apiClient.extension.configMap.updateV1alpha1ConfigMap({
name: "system",
configMap: systemConfigMap,
});
}
await refetchPrimaryMenuName();
Toast.success(t("core.menu.operations.set_primary.toast_success"));
};
</script>
<template>
<MenuEditingModal
v-if="menuEditingModal"
:menu="selectedMenuToUpdate"
@close="menuEditingModal = false"
@created="handleSelect"
/>
<VCard :body-class="['!p-0']" :title="$t('core.menu.title')">
<VLoading v-if="isLoading" />
<Transition v-else-if="!menus?.length" appear name="fade">
<VEmpty
:message="$t('core.menu.empty.message')"
:title="$t('core.menu.empty.title')"
>
<template #actions>
<VSpace>
<VButton size="sm" @click="refetch()">
{{ $t("core.common.buttons.refresh") }}
</VButton>
</VSpace>
</template>
</VEmpty>
</Transition>
<Transition v-else appear name="fade">
<ul class="box-border h-full w-full divide-y divide-gray-100" role="list">
<li
v-for="(menu, index) in menus"
:key="index"
@click="handleSelect(menu)"
>
<VEntity
:is-selected="selectedMenu?.metadata.name === menu.metadata.name"
>
<template #start>
<VEntityField
:title="menu.spec?.displayName"
:description="
$t('core.menu.list.fields.items_count', {
count: menu.spec.menuItems?.length || 0,
})
"
>
<template v-if="menu.metadata.name === primaryMenuName" #extra>
<VTag>
{{ $t("core.menu.list.fields.primary") }}
</VTag>
</template>
</VEntityField>
</template>
<template #end>
<VEntityField v-if="menu.metadata.deletionTimestamp">
<template #description>
<VStatusDot
v-tooltip="$t('core.common.status.deleting')"
state="warning"
animate
/>
</template>
</VEntityField>
</template>
<template
v-if="currentUserHasPermission(['system:menus:manage'])"
#dropdownItems
>
<VDropdownItem @click="handleSetPrimaryMenu(menu)">
{{ $t("core.menu.operations.set_primary.button") }}
</VDropdownItem>
<VDropdownItem @click="handleOpenEditingModal(menu)">
{{ $t("core.common.buttons.edit") }}
</VDropdownItem>
<VDropdownItem type="danger" @click="handleDeleteMenu(menu)">
{{ $t("core.common.buttons.delete") }}
</VDropdownItem>
</template>
</VEntity>
</li>
</ul>
</Transition>
<template v-if="currentUserHasPermission(['system:menus:manage'])" #footer>
<VButton block type="secondary" @click="handleOpenEditingModal()">
{{ $t("core.common.buttons.new") }}
</VButton>
</template>
</VCard>
</template>