All files / components/ContentfulRichText/nodes EmbeddedEntryBlock.svelte

100% Statements 64/64
100% Branches 2/2
100% Functions 0/0
100% Lines 64/64

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 651x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<script lang="ts">
  import { getContext } from "svelte";
  import type { Node as NodeType, EntryLinkBlock } from "@contentful/rich-text-types";
  import { linksKey, type LinksContext, imageSizeTypeKey } from "../context";
  import { isEntryBlock } from "../predicates";
  import ContentfulRichText from "../ContentfulRichText.svelte";
  import { getSources } from "$lib/imageServices/contentful";
  import ContactCard from "$lib/components/ContactCard";
  import Image from "$lib/components/Image";
 
  export let node: NodeType;
  if (!isEntryBlock(node)) {
    throw new Error("Node is not an embedded entry block");
  }
 
  const linksContext = getContext<LinksContext | undefined>(linksKey);
  if (!linksContext) throw new Error("no context was provided for embedded entry block");
 
  let entry: EntryLinkBlock = node;
  const entryId = entry.data.target.sys.id;
  const entryBlock = linksContext.linksEntriesMaps.block.get(entryId);
 
  if (!entryBlock) throw new Error(`the entry ${entryId} was not found in the context`);
</script>
 
{#if entryBlock?.__typename === "Contact"}
  <ContactCard address={undefined} contacts={[entryBlock]} />
{:else if entryBlock?.__typename === "ImageWrapper" && entryBlock?.linkedImage?.url}
  <Image
    src={entryBlock?.linkedImage?.url}
    sources={getSources}
    blurhash={entryBlock?.linkedImage?.blurhash ?? undefined}
    alt={entryBlock.linkedImage?.description ?? "Hero image"}
    width={entryBlock.linkedImage?.width ?? undefined}
    height={entryBlock.linkedImage?.height ?? undefined}
    sizeType={getContext(imageSizeTypeKey)}
  />
{:else if entryBlock?.__typename === "CallToAction" && entryBlock?.callToActionDestination?.json}
  <div class="embedded-entry-CTA">
    <ContentfulRichText
      document={entryBlock.callToActionDestination.json}
      links={entryBlock.callToActionDestination?.links}
    />
  </div>
{:else}
  <!--
    TODO: Add support for the other types of
    entries that can be an "embedded-entry-block":
      - Video Wrapper
      - Location
      - Anything else?
  -->
  <span
    >Entry block of type
    <pre>{entryBlock.__typename}</pre>
    is not supported
  </span>
{/if}
 
<style>
  pre {
    display: inline-block;
  }
</style>