All files / src/features/Events/EventDetail/tickets useTicketPickup.ts

83.33% Statements 10/12
100% Branches 2/2
66.66% Functions 2/3
81.81% Lines 9/11

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                          5x 16x 16x 16x   16x       3x 2x 2x                         16x    
import { useState } from "react";
import { useQueryClient } from "react-query";
import { QKEY_EVENT_TICKETS } from "@constants/queryKeys";
import { useUpdateTicketPickup } from "@services/api/products/tickets";
import { EventTicketRow, TicketPickupStatus } from "./ticket.types";
 
/**
 * The "Ticket Picked Up" column's writes (frame 7594-66313).
 *
 * No confirmation, unlike check-in: pickup is a correctable record — every value
 * may follow every other, server-side too — so a misclick costs one more click,
 * not an irreversible admission.
 */
export const useTicketPickup = (eventId?: string) => {
  const queryClient = useQueryClient();
  const mutation = useUpdateTicketPickup(eventId);
  const [savingId, setSavingId] = useState<EventTicketRow["id"] | null>(null);
 
  const setPickupStatus = (
    row: EventTicketRow,
    pickupStatus: TicketPickupStatus,
  ) => {
    if (savingId !== null) return;
    setSavingId(row.id);
    mutation.mutate(
      { ticketId: row.id, pickupStatus },
      {
        // Refetched either way: on failure the list resyncs the chip to what the
        // server actually holds, and the axios interceptor raises the toast.
        onSettled: () => {
          setSavingId(null);
          queryClient.invalidateQueries(QKEY_EVENT_TICKETS);
        },
      },
    );
  };
 
  return { setPickupStatus, savingId };
};