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 117 | 1x 1x 1x 1x 1x 1x | import { useCallback, useEffect, useState } from 'react';
import { useAuth } from '@/contexts/AuthContext';
import {
acceptInvitation,
declineInvitation,
listMyInvitations,
} from '@/services/invitationService';
import type { ReceivedInvitation } from '@/models/invitation';
import { ApiError } from '@/services/api';
import i18n from '@/i18n';
export interface InvitationsViewModel {
invitations: ReceivedInvitation[];
isLoading: boolean;
isActionLoading: string | null;
error: string | null;
fetchInvitations: () => Promise<void>;
handleAccept: (invitationId: string) => Promise<{ event_id: string } | null>;
handleDecline: (invitationId: string) => Promise<void>;
dismissError: () => void;
}
function mergeInvitationBuckets(response: Awaited<ReturnType<typeof listMyInvitations>>): ReceivedInvitation[] {
return [...(response.pending ?? []), ...(response.past?.items ?? [])];
}
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 () => {
if (!token) {
setInvitations([]);
setIsLoading(false);
return;
}
setIsLoading(true);
setError(null);
try {
const response = await listMyInvitations(token);
setInvitations(mergeInvitationBuckets(response));
} catch (err) {
setError(err instanceof ApiError ? err.message : i18n.t('errors.invitations_load_failed'));
} finally {
setIsLoading(false);
}
}, [token]);
const handleAccept = useCallback(
async (invitationId: string): Promise<{ event_id: string } | null> => {
if (!token) return null;
setIsActionLoading(invitationId);
setError(null);
try {
const response = await acceptInvitation(invitationId, token);
setInvitations((prev) =>
prev.map((i) =>
i.invitation_id === invitationId
? { ...i, status: 'ACCEPTED', updated_at: response.updated_at }
: i,
),
);
return { event_id: response.event_id };
} catch (err) {
setError(err instanceof ApiError ? err.message : i18n.t('errors.invitations_accept_failed'));
return null;
} finally {
setIsActionLoading(null);
}
},
[token],
);
const handleDecline = useCallback(
async (invitationId: string) => {
if (!token) return;
setIsActionLoading(invitationId);
setError(null);
try {
const response = await declineInvitation(invitationId, token);
setInvitations((prev) =>
prev.map((i) =>
i.invitation_id === invitationId
? { ...i, status: 'DECLINED', updated_at: response.updated_at }
: i,
),
);
} catch (err) {
setError(err instanceof ApiError ? err.message : i18n.t('errors.invitations_decline_failed'));
} finally {
setIsActionLoading(null);
}
},
[token],
);
useEffect(() => {
fetchInvitations();
}, [fetchInvitations]);
const dismissError = useCallback(() => setError(null), []);
return {
invitations,
isLoading,
isActionLoading,
error,
fetchInvitations,
handleAccept,
handleDecline,
dismissError,
};
}
|