All files / src/viewmodels/admin useAdminMutation.ts

13.63% Statements 3/22
100% Branches 0/0
0% Functions 0/1
13.63% Lines 3/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 261x 1x   1x                                            
import { useState } from 'react';
import { ApiError } from '@/services/api';
 
export function useAdminMutation(onSuccess?: () => void) {
  const [busyId, setBusyId] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [message, setMessage] = useState<string | null>(null);
 
  async function run(id: string, action: () => Promise<unknown>, successMessage: string) {
    setBusyId(id);
    setError(null);
    setMessage(null);
    try {
      await action();
      setMessage(successMessage);
      onSuccess?.();
    } catch (err) {
      setError(err instanceof ApiError ? err.message : 'Admin action failed.');
    } finally {
      setBusyId(null);
    }
  }
 
  return { busyId, error, message, run, clearMessage: () => setMessage(null) };
}