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

88.23% Statements 30/34
50% Branches 12/24
77.77% Functions 7/9
88.23% Lines 30/34

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                                                                      13x 13x 13x 13x 13x   13x             26x     13x     13x     13x                                             13x   13x                   13x 13x   13x 13x 13x     13x 13x       13x 13x 26x     13x 13x       13x   13x       13x   13x 13x     13x                                          
import { UseFormGetValues, FieldValues, UseFormWatch } from "react-hook-form";
import { Form } from "@canonical/react-components";
 
type Props = {
  snapName: string;
  getValues: UseFormGetValues<FieldValues>;
  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, getValues, 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 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: getValues("title"),
    images: [
      {
        url: getValues("banner_url"),
        type: "banner",
        status: "uploaded",
      },
      {
        url: getValues("icon_url"),
        type: "icon",
        status: "uploaded",
      },
    ],
    summary: getValues("summary"),
    description: getValues("description"),
    video: null,
    license: "",
  };
 
  const screenshotUrls = getValues("screenshot_urls");
 
  Iif (screenshotUrls.length) {
    screenshotUrls.forEach((screenshotUrl: string) => {
      listingData.images.push({
        url: screenshotUrl,
        type: "screenshot",
        status: "uploaded",
      });
    });
  }
 
  const primaryCategory = getValues("primary_category");
  const secondaryCategory = getValues("secondary_category");
 
  Eif (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 = getValues("video_urls");
 
  Iif (videoUrl) {
    listingData.video = [{ type: "video", status: "uploaded", url: videoUrl }];
  }
 
  listingData.license = getValues("license");
 
  Eif (getValues("primary_website")) {
    listingData.links.website.unshift(getValues("primary_website"));
  }
 
  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"
        defaultValue={JSON.stringify(listingData)}
      />
    </Form>
  );
}
 
export default PreviewForm;