Skip to content

Commit 79e195f

Browse files
committed
链接修改分类后排序为新分类最后一位
1 parent 74708d9 commit 79e195f

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

store/slices/linksSlice.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,34 @@ const linksSlice = createSlice({
5959
updateLink: (state, action: PayloadAction<UpdateLinkInput>) => {
6060
const index = state.items.findIndex(link => link.id === action.payload.id);
6161
if (index !== -1) {
62+
const oldLink = state.items[index];
63+
const newCategory = action.payload.category;
64+
65+
// 检查分类是否改变
66+
const categoryChanged = newCategory && newCategory !== oldLink.category;
67+
68+
// 如果分类改变,将链接移到新分类的最后
69+
let newOrder = oldLink.order;
70+
if (categoryChanged) {
71+
// 找到新分类中所有链接的最大 order 值
72+
const linksInNewCategory = state.items.filter(
73+
link => link.category === newCategory && link.id !== action.payload.id
74+
);
75+
76+
if (linksInNewCategory.length > 0) {
77+
const maxOrder = Math.max(...linksInNewCategory.map(link => link.order));
78+
newOrder = maxOrder + 1;
79+
} else {
80+
// 如果新分类中没有其他链接,使用当前所有链接的最大 order + 1
81+
const maxOrder = Math.max(...state.items.map(link => link.order));
82+
newOrder = maxOrder + 1;
83+
}
84+
}
85+
6286
state.items[index] = {
63-
...state.items[index],
87+
...oldLink,
6488
...action.payload,
89+
order: newOrder,
6590
updatedAt: Date.now(),
6691
};
6792
state.error = null;

0 commit comments

Comments
 (0)