收藏主机.
This commit is contained in:
29
orion-ops-ui/src/hooks/favorite.ts
Normal file
29
orion-ops-ui/src/hooks/favorite.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Message } from '@arco-design/web-vue';
|
||||
import { FavoriteType, addFavorite, cancelFavorite } from '@/api/meta/favorite';
|
||||
|
||||
export default function useFavorite(type: FavoriteType) {
|
||||
const toggle = async (record: any, id: number, cancelField = 'favorite') => {
|
||||
const request = { relId: id, type } as any;
|
||||
const loading = Message.loading(record[cancelField] ? '取消中' : '收藏中');
|
||||
try {
|
||||
if (record[cancelField]) {
|
||||
// 取消收藏
|
||||
await cancelFavorite(request);
|
||||
record[cancelField] = false;
|
||||
Message.success('已取消');
|
||||
} else {
|
||||
// 添加收藏
|
||||
await addFavorite(request);
|
||||
record[cancelField] = true;
|
||||
Message.success('已收藏');
|
||||
}
|
||||
} finally {
|
||||
loading.close();
|
||||
}
|
||||
};
|
||||
return {
|
||||
toggle
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user