All files / src/viewmodels/invitation useInvitationsViewModel.ts

78.57% Statements 44/56
18.18% Branches 2/11
100% Functions 11/11
80.76% Lines 42/52

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1172x 2x 2x           2x 2x                             3x 3x       2x 10x 10x 10x 10x 10x   10x 4x         4x 4x 4x 4x 3x   1x 1x         4x       10x   1x 1x 1x 1x 1x 1x                 1x           10x   1x 1x 1x 1x 1x 1x                 1x           10x 4x     10x                    
import { useState, useCallback, useEffect } from 'react';
import { useAuth } from '@/contexts/AuthContext';
import {
  listMyInvitations,
  acceptInvitation,
  declineInvitation,
} from '@/services/invitationService';
import { ReceivedInvitation } from '@/models/invitation';
import { ApiError } from '@/services/api';
import i18n from '@/i18n';
 
export interface InvitationsViewModel {
  invitations: ReceivedInvitation[];
  isLoading: boolean;
  isActionLoading: string | null; // Stores invitation_id of the currently acting invitation
  error: string | null;
  fetchInvitations: () => Promise<void>;
  handleAccept: (invitationId: string) => Promise<void>;
  handleDecline: (invitationId: string) => Promise<void>;
}
 
function getPendingInvitations(
  response: Awaited<ReturnType<typeof listMyInvitations>>,
): ReceivedInvitation[] {
  return (response.pending ?? []).filter(
    (invitation) => invitation.status === 'PENDING',
  );
}
 
export function useInvitationsViewModel(): InvitationsViewModel {
  const { token } = useAuth();
  const [invitations, setInvitations] = useState<ReceivedInvitation[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [isActionLoading, setIsActionLoading] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);
 
  const fetchInvitations = useCallback(async () => {
    Iif (!token) {
      setInvitations([]);
      setIsLoading(false);
      return;
    }
    setIsLoading(true);
    setError(null);
    try {
      const response = await listMyInvitations(token);
      setInvitations(getPendingInvitations(response));
    } catch (err) {
      if (err instanceof ApiError) {
        setError(err.message);
      } else E{
        setError(i18n.t('profile.invitations.loadFailed'));
      }
    } finally {
      setIsLoading(false);
    }
  }, [token]);
 
  const handleAccept = useCallback(
    async (invitationId: string) => {
      Iif (!token) return;
      setIsActionLoading(invitationId);
      try {
        await acceptInvitation(invitationId, token);
        setInvitations((prev) =>
          prev.filter((invitation) => invitation.invitation_id !== invitationId),
        );
      } catch (err) {
        if (err instanceof ApiError) {
          setError(err.message);
        } else {
          setError(i18n.t('profile.invitations.acceptFailed'));
        }
      } finally {
        setIsActionLoading(null);
      }
    },
    [token],
  );
 
  const handleDecline = useCallback(
    async (invitationId: string) => {
      Iif (!token) return;
      setIsActionLoading(invitationId);
      try {
        await declineInvitation(invitationId, token);
        setInvitations((prev) =>
          prev.filter((invitation) => invitation.invitation_id !== invitationId),
        );
      } catch (err) {
        if (err instanceof ApiError) {
          setError(err.message);
        } else {
          setError(i18n.t('profile.invitations.declineFailed'));
        }
      } finally {
        setIsActionLoading(null);
      }
    },
    [token],
  );
 
  useEffect(() => {
    fetchInvitations();
  }, [fetchInvitations]);
 
  return {
    invitations,
    isLoading,
    isActionLoading,
    error,
    fetchInvitations,
    handleAccept,
    handleDecline,
  };
}