All files / publisher/utils getListingChanges.ts

100% Statements 31/31
100% Branches 40/40
100% Functions 2/2
100% Lines 30/30

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                                                        18x   18x               1x               18x                       18x                 18x   18x 23x 14x     23x 5x               5x 4x         18x 3x   3x 2x     3x 2x     3x     18x       4x 2x         4x 2x     4x       1x     4x       1x             18x    
import { FieldValues } from "react-hook-form";
 
import formatImageChanges from "./formatImageChanges";
 
import type { ListingData } from "../types";
 
export default function getListingChanges(
  dirtyFields: Record<string, boolean>,
  fieldValues: FieldValues,
  data: ListingData,
) {
  const changes: {
    [key: string]: unknown;
    images?: {
      url: string;
      type: string;
      status: string;
      name?: string;
    }[];
    links?: {
      website: string[];
      donations: string[];
      contact: string[];
      issues: string[];
      source: string[];
    };
    categories?: string[] | undefined;
    public_metrics_blacklist?: string[];
  } = {};
 
  if (
    dirtyFields.banner_urls ||
    dirtyFields.icon_url ||
    dirtyFields.screenshot_urls ||
    dirtyFields.icon ||
    dirtyFields.banner ||
    dirtyFields.screenshots
  ) {
    changes.images = formatImageChanges(
      data.banner_urls,
      fieldValues.icon_url,
      fieldValues.screenshot_urls,
      fieldValues.screenshots,
    );
  }
 
  const forbiddenKeys = [
    "primary_category",
    "secondary_category",
    "websites",
    "contacts",
    "donations",
    "source_code",
    "issues",
    "icon",
    "icon_url",
  ];
 
  const linksKeys = [
    "websites",
    "contacts",
    "donations",
    "source_code",
    "issues",
    "primary_website",
  ];
 
  const getUrls = (item: { url: string }) => item.url;
 
  for (const [key, value] of Object.entries(dirtyFields)) {
    if (!forbiddenKeys.includes(key) && value !== false) {
      changes[key] = fieldValues[key];
    }
 
    if (linksKeys.includes(key)) {
      changes.links = {
        contact: fieldValues.contacts.map(getUrls),
        donations: fieldValues.donations.map(getUrls),
        issues: fieldValues.issues.map(getUrls),
        source: fieldValues.source_code.map(getUrls),
        website: fieldValues.websites.map(getUrls),
      };
 
      if (fieldValues.primary_website) {
        changes.links.website.unshift(fieldValues.primary_website);
      }
    }
  }
 
  if (dirtyFields.primary_category || dirtyFields.secondary_category) {
    const categories = [];
 
    if (fieldValues.primary_category) {
      categories.push(fieldValues.primary_category);
    }
 
    if (fieldValues.secondary_category) {
      categories.push(fieldValues.secondary_category);
    }
 
    changes.categories = categories;
  }
 
  if (
    dirtyFields.public_metrics_territories ||
    dirtyFields.public_metrics_distros
  ) {
    if (fieldValues.public_metrics_territories === true) {
      changes.public_metrics_blacklist = [
        "weekly_installed_base_by_operating_system_normalized",
      ];
    }
 
    if (fieldValues.public_metrics_distros === true) {
      changes.public_metrics_blacklist = ["installed_base_by_country_percent"];
    }
 
    if (
      fieldValues.public_metrics_territories === true &&
      fieldValues.public_metrics_distros === true
    ) {
      changes.public_metrics_blacklist = [];
    }
 
    if (
      fieldValues.public_metrics_territories === false &&
      fieldValues.public_metrics_distros === false
    ) {
      changes.public_metrics_blacklist = [
        "installed_base_by_country_percent",
        "weekly_installed_base_by_operating_system_normalized",
      ];
    }
  }
 
  return changes;
}