Skip to content

Commit

Permalink
Fix map shipping translations to translation entities (#5303)
Browse files Browse the repository at this point in the history
* Fix map shipping translations to translation entities

* Add changeset

* Exort sumCompleted
  • Loading branch information
poulch authored Dec 5, 2024
1 parent d5a6731 commit 57cefdc
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-stingrays-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Adding tranalstions to shipping methods no more cause error
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// @ts-strict-ignore
import { useShippingMethodTranslationsQuery } from "@dashboard/graphql";
import usePaginator, { PaginatorContext } from "@dashboard/hooks/usePaginator";
import TranslationsEntitiesList from "@dashboard/translations/components/TranslationsEntitiesList";
import { languageEntityUrl, TranslatableEntities } from "@dashboard/translations/urls";
import { mapEdgesToItems } from "@dashboard/utils/maps";
import React from "react";

import { TranslationsEntityListProps } from "./types";
import { sumCompleted } from "./utils";
import { mapTranslationsToEntities } from "./utils";

const TranslationsShippingMethodList: React.FC<TranslationsEntityListProps> = ({
params,
Expand All @@ -27,17 +25,7 @@ const TranslationsShippingMethodList: React.FC<TranslationsEntityListProps> = ({
<PaginatorContext.Provider value={paginationValues}>
<TranslationsEntitiesList
disabled={loading}
entities={mapEdgesToItems(data?.translations)?.map(
node =>
node.__typename === "ShippingMethodTranslatableContent" && {
completion: {
current: sumCompleted([node.translation?.name, node.translation?.description]),
max: 2,
},
id: node?.shippingMethod.id,
name: node?.name,
},
)}
entities={mapTranslationsToEntities(data)}
getRowHref={id =>
languageEntityUrl(variables.language, TranslatableEntities.shippingMethods, id)
}
Expand Down
117 changes: 117 additions & 0 deletions src/translations/views/EntityLists/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { ShippingMethodTranslationsQuery } from "@dashboard/graphql";

import { mapTranslationsToEntities } from "./utils";

describe("mapTranslationsToEntities", () => {
it("should return empty array if data is undefined", () => {
// Arrange
const data = undefined;

// Act
const result = mapTranslationsToEntities(data);

// Assert
expect(result).toEqual([]);
});

it("should return empty array if translations is undefined", () => {
// Arrange
const data = {
translations: undefined,
} as unknown as ShippingMethodTranslationsQuery;

// Act
const result = mapTranslationsToEntities(data);

// Assert
expect(result).toEqual([]);
});

it("should return empty array if items is undefined", () => {
// Arrange
const data = {
translations: {
edges: undefined,
},
} as unknown as ShippingMethodTranslationsQuery;

// Act
const result = mapTranslationsToEntities(data);

// Assert
expect(result).toEqual([]);
});

it("should return correct array", () => {
// Arrange
const data = {
translations: {
edges: [
{
node: {
__typename: "ShippingMethodTranslatableContent",
translation: {
name: "name",
description: "description",
},
shippingMethod: {
id: "id",
},
name: "name",
},
},
],
},
} as ShippingMethodTranslationsQuery;

// Act
const result = mapTranslationsToEntities(data);

// Assert
expect(result).toEqual([
{
completion: {
current: 2,
max: 2,
},
id: "id",
name: "name",
},
]);
});

it("should return empty string when no shipping method", () => {
// Arrange
const data = {
translations: {
edges: [
{
node: {
__typename: "ShippingMethodTranslatableContent",
translation: {
name: "name",
description: "description",
},
name: "name",
},
},
],
},
} as ShippingMethodTranslationsQuery;

// Act
const result = mapTranslationsToEntities(data);

// Assert
expect(result).toEqual([
{
completion: {
current: 2,
max: 2,
},
id: "",
name: "name",
},
]);
});
});
33 changes: 33 additions & 0 deletions src/translations/views/EntityLists/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
import { ShippingMethodTranslationsQuery } from "@dashboard/graphql";
import { TranslatableEntity } from "@dashboard/translations/components/TranslationsEntitiesList";
import { mapEdgesToItems } from "@dashboard/utils/maps";

export function mapTranslationsToEntities(
data: ShippingMethodTranslationsQuery | undefined,
): TranslatableEntity[] {
if (!data?.translations) {
return [];
}

const items = mapEdgesToItems(data.translations);

if (!items) {
return [];
}

return items.reduce((acc, node) => {
if (node.__typename === "ShippingMethodTranslatableContent") {
acc.push({
completion: {
current: sumCompleted([node.translation?.name, node.translation?.description]),
max: 2,
},
id: node?.shippingMethod?.id ?? "",
name: node?.name,
});
}

return acc;
}, [] as TranslatableEntity[]);
}

export function sumCompleted(list: any[]): number {
return list.reduce((acc, field) => acc + (field ? 1 : 0), 0);
}

0 comments on commit 57cefdc

Please sign in to comment.