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 118 119 120 121 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 5x 5x 5x 5x 21x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x 1x 1x 1x 1x 3x 2x 2x 3x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x | import { useState } from 'react';
import type {
AdminCancelParticipationResponse,
AdminCreateParticipationResponse,
} from '@/models/admin';
import { ApiError } from '@/services/api';
import { cancelAdminParticipation, createAdminParticipation } from '@/services/adminService';
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
interface CreateForm {
eventId: string;
userId: string;
reason: string;
}
const INITIAL_CREATE_FORM: CreateForm = {
eventId: '',
userId: '',
reason: '',
};
type CreateField = keyof CreateForm;
type CreateErrors = Partial<Record<CreateField, string>>;
function apiErrorMessage(err: unknown, fallback: string): string {
if (err instanceof ApiError) {
const details = err.details ? Object.values(err.details).filter(Boolean).join(' ') : '';
return details ? `${err.message} ${details}` : err.message;
}
return fallback;
}
export function useAdminParticipationActionsViewModel(
token: string | null,
onMutationSuccess: () => Promise<unknown> | void,
) {
const [createForm, setCreateForm] = useState<CreateForm>(INITIAL_CREATE_FORM);
const [createErrors, setCreateErrors] = useState<CreateErrors>({});
const [createError, setCreateError] = useState<string | null>(null);
const [createResult, setCreateResult] = useState<AdminCreateParticipationResponse | null>(null);
const [isCreating, setIsCreating] = useState(false);
const [cancelingId, setCancelingId] = useState<string | null>(null);
const [cancelError, setCancelError] = useState<string | null>(null);
const [cancelResult, setCancelResult] = useState<AdminCancelParticipationResponse | null>(null);
function setCreateField<K extends CreateField>(field: K, value: CreateForm[K]) {
setCreateForm((current) => ({ ...current, [field]: value }));
setCreateErrors((current) => ({ ...current, [field]: undefined }));
setCreateError(null);
}
async function submitCreate() {
const errors: CreateErrors = {};
const eventId = createForm.eventId.trim();
const userId = createForm.userId.trim();
if (!UUID_RE.test(eventId)) errors.eventId = 'Event ID must be a valid UUID.';
if (!UUID_RE.test(userId)) errors.userId = 'User ID must be a valid UUID.';
setCreateErrors(errors);
setCreateResult(null);
if (Object.keys(errors).length > 0) return;
if (!token) {
setCreateError('Admin session is not available.');
return;
}
setIsCreating(true);
setCreateError(null);
try {
const result = await createAdminParticipation(token, {
event_id: eventId,
user_id: userId,
status: 'APPROVED',
reason: createForm.reason.trim() || null,
});
setCreateResult(result);
setCreateForm(INITIAL_CREATE_FORM);
await onMutationSuccess();
} catch (err) {
setCreateError(apiErrorMessage(err, 'Failed to create participation.'));
} finally {
setIsCreating(false);
}
}
async function cancelParticipation(participationId: string) {
if (!token) {
setCancelError('Admin session is not available.');
return;
}
if (!window.confirm('Cancel this participation?')) return;
setCancelingId(participationId);
setCancelError(null);
setCancelResult(null);
try {
const result = await cancelAdminParticipation(token, participationId);
setCancelResult(result);
await onMutationSuccess();
} catch (err) {
setCancelError(apiErrorMessage(err, 'Failed to cancel participation.'));
} finally {
setCancelingId(null);
}
}
return {
createForm,
createErrors,
createError,
createResult,
isCreating,
cancelingId,
cancelError,
cancelResult,
setCreateField,
submitCreate,
cancelParticipation,
};
}
|