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: use patch api to refactor category drag-and-drop sorting feature #6461

Merged
merged 1 commit into from
Aug 14, 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
15 changes: 13 additions & 2 deletions ui/console-src/modules/contents/posts/categories/CategoryList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ const handleUpdateInBatch = useDebounceFn(async () => {
try {
batchUpdating.value = true;
const promises = categoriesToUpdate.map((category) =>
coreApiClient.content.category.updateCategory({
coreApiClient.content.category.patchCategory({
name: category.metadata.name,
category: category,
jsonPatchInner: [
{
op: "add",
path: "/spec/children",
value: category.spec.children || [],
},
{
op: "add",
path: "/spec/priority",
value: category.spec.priority || 0,
},
],
})
);
await Promise.all(promises);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { setFocus } from "@/formkit/utils/focus";
import { FormType } from "@/types/slug";
import useSlugify from "@console/composables/use-slugify";
import { useThemeCustomTemplates } from "@console/modules/interface/themes/composables/use-theme";
import { reset, submitForm } from "@formkit/core";
import type { Category } from "@halo-dev/api-client";
import {
IconRefreshLine,
Expand All @@ -21,7 +22,6 @@ import {
import { useQueryClient } from "@tanstack/vue-query";
import { cloneDeep } from "lodash-es";
import { useI18n } from "vue-i18n";
import { submitForm, reset } from "@formkit/core";

const props = withDefaults(
defineProps<{
Expand Down Expand Up @@ -109,28 +109,30 @@ const handleSaveCategory = async () => {
parentCategory = data;
}

const priority = parentCategory?.spec.children
formState.value.spec.priority = parentCategory?.spec.children
? parentCategory.spec.children.length + 1
: 0;

formState.value.spec.priority = priority;

const { data: createdCategory } =
await coreApiClient.content.category.createCategory({
category: formState.value,
});

if (parentCategory) {
parentCategory.spec.children = Array.from(
new Set([
...(parentCategory.spec.children || []),
createdCategory.metadata.name,
])
);

await coreApiClient.content.category.updateCategory({
await coreApiClient.content.category.patchCategory({
name: selectedParentCategory.value,
category: parentCategory,
jsonPatchInner: [
{
op: "add",
path: "/spec/children",
value: Array.from(
new Set([
...(parentCategory.spec.children || []),
createdCategory.metadata.name,
])
),
},
],
});
}
}
Expand Down
Loading