All files / src/hooks/merchant-api/sweepstakes useFindSweepstakeById.tsx

0% Statements 0/25
0% Branches 0/46
0% Functions 0/7
0% Lines 0/21

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                                                                                                                                                                                                           
import { SweepstakeData } from "@components/Sweepstakes/MiniBuilder/BuilderFormProvider";
import { customInstance } from "@services/api";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import { stringToEditorState } from "@utils/draft.editor.helpers";
import { useMemo } from "react";
import { useQuery } from "react-query";
 
const getSweepstakeVariants = (id?: string, publicResource?: boolean) => {
  if (!id) return;
  const publicUrl = `products/${id}/variants?&sort=displayOrder`;
  return customInstance({
    url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
    method: "GET",
  });
};
 
const getSweepstake = (id?: string, publicResource?: boolean) => {
  if (!id) return;
  const publicUrl = `products/${id}?filter=typeName:"sweepstake"`;
  return customInstance({
    url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
    method: "GET",
  });
};
 
export const useFindSweepstakeById = (
  id?: string,
  publicResource?: boolean,
) => {
  const { data, error, isLoading } = useQuery(
    ["find-sweepstake-by-id", id],
    async () => {
      const sweepstake = await getSweepstake(id, publicResource);
      const variants = await getSweepstakeVariants(id, publicResource);
      return { sweepstake, variants };
    },
    { cacheTime: 0 },
  );
 
  const customTickets = useMemo(() => {
    if (!data?.variants) return [];
    return (
      data.variants.data?.map((ticket: any) => ({
        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_tickets: ticket.showAvailableVariants ?? false,
        bundle: 1,
      })) ?? []
    );
  }, [data?.variants?.data]);
 
  const customData: SweepstakeData = useMemo(() => {
    return {
      general: {
        title: data?.sweepstake?.name || "",
        description: data?.sweepstake?.description || "",
        browseMore: data?.sweepstake?.canBrowseCampaigns || false,
        date: data?.sweepstake?.endsAt
          ? new Date(data?.sweepstake?.endsAt * 1000)
          : new Date(),
        creatorName: data?.sweepstake?.merchantName,
        creatorImage: data?.sweepstake?.merchantImageURL,
        creatorDescription: data?.sweepstake?.merchantDescription,
        featuredImage: {
          image: data?.sweepstake?.imageURL || "",
          active: Boolean(data?.sweepstake?.imageURL),
          useAsBackground: data?.sweepstake?.usesBackgroundImage || false,
        },
      },
      tickets: {
        customer_pays_credit: {
          active: data?.sweepstake?.needsTax || false,
          optional: data?.sweepstake?.allowFeeChoice || false,
        },
        multiple_winners: data?.sweepstake?.allowMultipleWinners || false,
        set_entries_limit: {
          active: Boolean(data?.sweepstake?.maxEntriesPerPrize) || false,
          max_entries: data?.sweepstake?.maxEntriesPerPrize || 0,
        },
        max_tickets: parseInt(data?.sweepstake?.maxPurchaseQuantity || 5),
        list: customTickets || [],
      },
      accID: data?.sweepstake?.accID,
    };
  }, [data?.sweepstake]);
 
  if (!id) return { data: undefined };
  return {
    data: customData,
    isLoading,
    redirect: (error as any)?.response?.status === 404 ? true : false,
  };
};