All files / src/hooks/merchant-api/events useFindEventById.tsx

0% Statements 0/19
0% Branches 0/62
0% Functions 0/5
0% Lines 0/18

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 122 123 124 125 126 127                                                                                                                                                                                                                                                             
import { customInstance } from "@services/api";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import { stringToEditorState } from "@utils/draft.editor.helpers";
import {
  DateType,
  LocationType,
} from "features/Minibuilders/EventsMinibuilder/types";
import { useQuery } from "react-query";
 
export const useFindEventById = (id?: string, publicResource?: boolean) => {
  if (!id) return { data: undefined };
 
  const getEvent = (id: string) => {
    const publicUrl = `products/${id}?filter=typeName:"event"`;
    return customInstance({
      url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
      method: "GET",
    });
  };
 
  const getEventVariants = (id: string) => {
    const publicUrl = `products/${id}/variants?&sort=displayOrder`;
    return customInstance({
      url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
      method: "GET",
    });
  };
 
  const { data, error, isLoading } = useQuery(
    ["find-event-by-id", id],
    async () => {
      const event = await getEvent(id);
      const variants = await getEventVariants(id);
      return { event, variants };
    },
    { cacheTime: 0, refetchOnWindowFocus: false },
  );
 
  if (!data)
    return {
      error,
      isLoading,
      redirect: (error as any)?.response?.status === 404 ? true : false,
    };
 
  const customTickets =
    data.variants?.data?.map((ticket: any) => {
      return {
        id: ticket.id,
        name: ticket.name,
        amount: (ticket.price / 100).toFixed(2),
        description: {
          enabled: Boolean(ticket.description),
          text: stringToEditorState(ticket.description),
        },
        thumbnail: ticket.imageURL ? ticket.imageURL : "",
        active: ticket.isEnabled,
        in_stock: ticket.inventory,
        display: ticket.showAvailableVariants,
        bundle: 1,
      };
    }) ?? [];
 
  const customData = {
    general: {
      title: data?.event?.name || "",
      description: data?.event?.description || "",
      creatorName: data?.event?.merchantName,
      creatorImage: data?.event?.merchantImageURL,
      creatorDescription: data?.event?.merchantDescription,
      browseMore: data?.event?.canBrowseCampaigns || false,
      featuredImage: {
        image: data?.event?.imageURL || "",
        active: Boolean(data?.event?.imageURL),
        useAsBackground: data?.event?.usesBackgroundImage || false,
      },
    },
    date_and_location: {
      date: {
        type: data?.event?.endsAt ? "period" : ("one day" as DateType),
        day: data?.event?.endsAt
          ? null
          : new Date(data?.event?.startsAt * 1000),
        include_time: data?.event?.includeTime,
        startDate: data?.event?.endsAt
          ? new Date(data?.event?.startsAt * 1000)
          : null,
        endDate: data?.event?.endsAt
          ? new Date(data?.event?.endsAt * 1000)
          : null,
      },
      location: {
        showMap: data?.event?.showMap || false,
        type: data?.event?.locationURL
          ? "online"
          : ("in person" as LocationType),
        location: data?.event?.locationShortAddress || "",
        country: data?.event?.locationAddress?.country || "US",
        address: data?.event?.locationAddress?.line1 || "",
        state: data?.event?.locationAddress?.state || "",
        city: data?.event?.locationAddress?.city || "",
        zip: data?.event?.locationAddress?.zip || "",
        event_url: data?.event?.locationURL || "",
        manual: Boolean(
          !data?.event?.locationShortAddress &&
            data?.event?.locationAddress?.line1,
        ),
      },
    },
    tickets: {
      customer_pays_credit: {
        active: data?.event?.needsTax || false,
        optional: data?.event?.allowFeeChoice || false,
        maxTickets: data?.event?.maxPurchaseQuantity || 5,
      },
      list: customTickets || [],
    },
    accID: data?.event?.accID,
  };
 
  return {
    data: customData,
    isLoading: isLoading,
    redirect: (error as any)?.response?.status === 404 ? true : false,
  };
};