useHostMutations.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { useMutation, useQueryClient } from '@tanstack/react-query';
  2. import { HttpUtil } from '@/utils';
  3. import { keys } from '@/api/queryKeys';
  4. import type { BulkAddHostValues } from '@/schemas/api/host';
  5. const JSON_HEADERS = { headers: { 'Content-Type': 'application/json' } };
  6. export function useHostMutations() {
  7. const queryClient = useQueryClient();
  8. const invalidate = () => queryClient.invalidateQueries({ queryKey: keys.hosts.root() });
  9. const bulkCreateMut = useMutation({
  10. mutationFn: (payload: BulkAddHostValues) =>
  11. HttpUtil.post('/panel/api/hosts/bulk/add', payload, JSON_HEADERS),
  12. onSuccess: (msg) => {
  13. if (msg?.success) invalidate();
  14. },
  15. });
  16. const updateMut = useMutation({
  17. mutationFn: ({ groupId, payload }: { groupId: string; payload: BulkAddHostValues }) =>
  18. HttpUtil.post(`/panel/api/hosts/update/${groupId}`, payload, JSON_HEADERS),
  19. onSuccess: (msg) => {
  20. if (msg?.success) invalidate();
  21. },
  22. });
  23. const removeMut = useMutation({
  24. mutationFn: (groupId: string) => HttpUtil.post(`/panel/api/hosts/del/${groupId}`),
  25. onSuccess: (msg) => {
  26. if (msg?.success) invalidate();
  27. },
  28. });
  29. const setEnableMut = useMutation({
  30. mutationFn: ({ groupId, enable }: { groupId: string; enable: boolean }) =>
  31. HttpUtil.post(`/panel/api/hosts/setEnable/${groupId}`, { enable }),
  32. onSuccess: (msg) => {
  33. if (msg?.success) invalidate();
  34. },
  35. });
  36. const reorderMut = useMutation({
  37. mutationFn: (groupIds: string[]) =>
  38. HttpUtil.post('/panel/api/hosts/reorder', { ids: groupIds }, JSON_HEADERS),
  39. onSuccess: (msg) => {
  40. if (msg?.success) invalidate();
  41. },
  42. });
  43. const bulkEnableMut = useMutation({
  44. mutationFn: ({ groupIds, enable }: { groupIds: string[]; enable: boolean }) =>
  45. HttpUtil.post('/panel/api/hosts/bulk/setEnable', { ids: groupIds, enable }, JSON_HEADERS),
  46. onSuccess: (msg) => {
  47. if (msg?.success) invalidate();
  48. },
  49. });
  50. const bulkDelMut = useMutation({
  51. mutationFn: (groupIds: string[]) =>
  52. HttpUtil.post('/panel/api/hosts/bulk/del', { ids: groupIds }, JSON_HEADERS),
  53. onSuccess: (msg) => {
  54. if (msg?.success) invalidate();
  55. },
  56. });
  57. return {
  58. bulkCreate: (payload: BulkAddHostValues) => bulkCreateMut.mutateAsync(payload),
  59. update: (groupId: string, payload: BulkAddHostValues) =>
  60. updateMut.mutateAsync({ groupId, payload }),
  61. remove: (groupId: string) => removeMut.mutateAsync(groupId),
  62. setEnable: (groupId: string, enable: boolean) => setEnableMut.mutateAsync({ groupId, enable }),
  63. reorder: (groupIds: string[]) => reorderMut.mutateAsync(groupIds),
  64. bulkSetEnable: (groupIds: string[], enable: boolean) =>
  65. bulkEnableMut.mutateAsync({ groupIds, enable }),
  66. bulkDel: (groupIds: string[]) => bulkDelMut.mutateAsync(groupIds),
  67. };
  68. }