All files / publisher/pages/Listing/PreviewForm PreviewForm.tsx

100% Statements 46/46
57.69% Branches 15/26
100% Functions 9/9
100% Lines 46/46

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                                                    146x 146x 146x 146x 146x 146x 146x 146x 146x 146x 146x 146x 146x 146x 146x 146x   146x       146x     146x     146x     146x     146x                                             146x   146x 146x 146x               146x 146x   146x 143x 149x     143x 143x       146x 146x 296x     146x 146x       146x   146x 146x     146x   146x 146x 145x     146x                                            
import { FieldValues, UseFormWatch } from "react-hook-form";
import { Form } from "@canonical/react-components";
 
type Props = {
  snapName: string;
  watch: UseFormWatch<FieldValues>;
  data: {
    categories: { name: string; slug: string }[];
  };
};
 
type ListingData = {
  categories: { name: string; slug: string }[];
  links: {
    website: Array<string>;
    contact: Array<string>;
    donations: Array<string>;
    source: Array<string>;
    issues: Array<string>;
  };
  snap_name: string;
  title: string;
  images: Array<{
    url: string;
    type: string;
    status: string;
  }>;
  summary: string;
  description: string;
  video: { type: string; status: string; url: string }[] | null;
  license: string;
};
 
function PreviewForm({ snapName, watch, data }: Props) {
  const watchWebsites = watch("websites");
  const watchContacts = watch("contacts");
  const watchDonations = watch("donations");
  const watchSource = watch("source_code");
  const watchIssues = watch("issues");
  const watchTitle = watch("title");
  const watchBannerUrls = watch("banner_urls");
  const watchIconUrl = watch("icon_url");
  const watchSummary = watch("summary");
  const watchDescription = watch("description");
  const watchScreenshotUrls = watch("screenshot_urls");
  const watchPrimaryCategory = watch("primary_category");
  const watchSecondaryCategory = watch("secondary_category");
  const watchVideoUrls = watch("video_urls");
  const watchLicense = watch("license");
  const watchPrimaryWebsite = watch("primary_website");
 
  const listingData: ListingData = {
    categories: [],
    links: {
      website: watchWebsites
        ? watchWebsites.map((item: { url: string }) => item.url)
        : [],
      contact: watchContacts
        ? watchContacts.map((item: { url: string }) => item.url)
        : [],
      donations: watchDonations
        ? watchDonations.map((item: { url: string }) => item.url)
        : [],
      source: watchSource
        ? watchSource.map((item: { url: string }) => item.url)
        : [],
      issues: watchIssues
        ? watchIssues.map((item: { url: string }) => item.url)
        : [],
    },
    snap_name: snapName,
    title: watchTitle,
    images: [
      {
        url: watchBannerUrls ? watchBannerUrls : null,
        type: "banner",
        status: "uploaded",
      },
      {
        url: watchIconUrl,
        type: "icon",
        status: "uploaded",
      },
    ],
    summary: watchSummary,
    description: watchDescription,
    video: null,
    license: "",
  };
 
  const screenshotUrls = watchScreenshotUrls;
 
  Eif (screenshotUrls.length) {
    screenshotUrls.forEach((screenshotUrl: string) => {
      listingData.images.push({
        url: screenshotUrl,
        type: "screenshot",
        status: "uploaded",
      });
    });
  }
 
  const primaryCategory = watchPrimaryCategory;
  const secondaryCategory = watchSecondaryCategory;
 
  if (primaryCategory) {
    const primaryCategoryData = data.categories.find(
      (cat) => cat.slug === primaryCategory,
    );
 
    Eif (primaryCategoryData) {
      listingData.categories.push(primaryCategoryData);
    }
  }
 
  Eif (secondaryCategory) {
    const secondaryCategoryData = data.categories.find(
      (cat) => cat.slug === secondaryCategory,
    );
 
    Eif (secondaryCategoryData) {
      listingData.categories.push(secondaryCategoryData);
    }
  }
 
  const videoUrl = watchVideoUrls;
 
  Eif (videoUrl) {
    listingData.video = [{ type: "video", status: "uploaded", url: videoUrl }];
  }
 
  listingData.license = watchLicense;
 
  const primaryWebsite = watchPrimaryWebsite;
  if (primaryWebsite) {
    listingData.links.website.unshift(primaryWebsite);
  }
 
  return (
    <Form
      id="preview-form"
      action={`/${snapName}/preview`}
      method="POST"
      encType="multipart/form-data"
      className="u-hide"
      target="_blank"
      rel="opener"
    >
      <input type="hidden" name="csrf_token" defaultValue={window.CSRF_TOKEN} />
      <input
        type="hidden"
        name="state"
        data-testid="state-input"
        defaultValue={JSON.stringify(listingData)}
      />
    </Form>
  );
}
 
export default PreviewForm;