All files / publisher/listing/components/PreviewForm PreviewForm.tsx

100% Statements 19/19
50% Branches 3/6
100% Functions 7/7
100% Lines 19/19

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                                                        5x     5x 5x   5x   5x 5x                                       5x   5x 5x 5x               5x 5x   5x 5x           5x 5x           5x         5x   5x                                        
import { Form } from "@canonical/react-components";
 
type Props = {
  snapName: string;
  getValues: Function;
};
 
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;
};
 
function PreviewForm({ snapName, getValues }: Props) {
  const listingData: ListingData = {
    categories: [],
    links: {
      website: getValues("websites").map((item: { url: string }) => item.url),
      contact: getValues("contacts").map((item: { url: string }) => item.url),
      donations: getValues("donations").map(
        (item: { url: string }) => item.url,
      ),
      source: getValues("source_code").map((item: { url: string }) => item.url),
      issues: getValues("issues").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"),
  };
 
  const screenshotUrls = getValues("screenshot_urls");
 
  Eif (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) {
    listingData.categories.push({
      name: primaryCategory,
      slug: primaryCategory,
    });
  }
 
  Eif (secondaryCategory) {
    listingData.categories.push({
      name: secondaryCategory,
      slug: secondaryCategory,
    });
  }
 
  window.localStorage.setItem(
    `${snapName}-initial`,
    JSON.stringify(listingData),
  );
 
  window.localStorage.setItem(snapName, JSON.stringify(listingData));
 
  return (
    <Form
      id="preview-form"
      action={`/${snapName}/preview`}
      method="POST"
      encType="multipart/form-data"
      className="u-hide"
      target="_blank"
    >
      <input type="hidden" name="csrf_token" defaultValue={window.CSRF_TOKEN} />
      <input
        type="hidden"
        name="state"
        defaultValue={JSON.stringify(listingData)}
      />
    </Form>
  );
}
 
export default PreviewForm;