-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helpers): extract navigation URL (#86)
- Loading branch information
Showing
10 changed files
with
147 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"vue-demo-store": patch | ||
--- | ||
|
||
Use correct URLs and target for navigation links |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"docs": patch | ||
--- | ||
|
||
Expand Navigation building section |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shopware-pwa/types": patch | ||
--- | ||
|
||
Expand Category type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shopware-pwa/helpers-next": patch | ||
--- | ||
|
||
Use technical URL as a fallback for navigation link |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { getCategoryUrl } from "./getCategoryUrl"; | ||
|
||
describe("getCategoryUrl", () => { | ||
it("should return / if passed category is an empty object", () => { | ||
expect(getCategoryUrl({} as any)).toBe("/"); | ||
}); | ||
|
||
it("should return / if passed category is undefined", () => { | ||
expect(getCategoryUrl(undefined as any)).toBe("/"); | ||
}); | ||
|
||
it("should return technical URL for navigation", () => { | ||
expect( | ||
getCategoryUrl({ | ||
type: "link", | ||
linkType: "category", | ||
internalLink: "123", | ||
} as any) | ||
).toBe("/navigation/123"); | ||
}); | ||
it("should return technical URL for product", () => { | ||
expect( | ||
getCategoryUrl({ | ||
type: "link", | ||
linkType: "product", | ||
internalLink: "123", | ||
} as any) | ||
).toBe("/detail/123"); | ||
}); | ||
|
||
it("should return technical URL for landing page", () => { | ||
expect( | ||
getCategoryUrl({ | ||
type: "link", | ||
linkType: "landing_page", | ||
internalLink: "123", | ||
} as any) | ||
).toBe("/landingPage/123"); | ||
}); | ||
|
||
it("should return external URL", () => { | ||
expect( | ||
getCategoryUrl({ | ||
type: "link", | ||
externalLink: "https://shopware.com", | ||
} as any) | ||
).toBe("https://shopware.com"); | ||
}); | ||
|
||
it("should return SEO URL", () => { | ||
expect( | ||
getCategoryUrl({ | ||
type: "link", | ||
seoUrls: [{ seoPathInfo: "/test" }], | ||
} as any) | ||
).toBe("/test"); | ||
}); | ||
|
||
it("should try to return SEO URL for unknown type", () => { | ||
expect( | ||
getCategoryUrl({ | ||
type: "unknown", | ||
seoUrls: [{ seoPathInfo: "test" }], | ||
} as any) | ||
).toBe("/test"); | ||
}); | ||
it("should try to return technical URL for unknown type", () => { | ||
expect( | ||
getCategoryUrl({ | ||
type: "unknown", | ||
id: "123", | ||
linkType: "category", | ||
} as any) | ||
).toBe("/navigation/123"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,47 @@ | ||
import { Category } from "@shopware-pwa/types"; | ||
import { getTranslatedProperty } from "../getTranslatedProperty"; | ||
|
||
type LinkedCategory = Pick< | ||
Category, | ||
"type" | "externalLink" | "seoUrls" | "internalLink" | "id" | "linkType" | ||
>; | ||
|
||
/** | ||
* Extract prefix for technical URL | ||
*/ | ||
function getEntityPrefix(category: LinkedCategory) { | ||
switch (category.linkType) { | ||
case "category": | ||
return "navigation"; | ||
case "product": | ||
return "detail"; | ||
case "landing_page": | ||
return "landingPage"; | ||
} | ||
} | ||
|
||
/** | ||
* Get URL for category. | ||
* Some link {@link isLinkCategory} | ||
* | ||
* @param {Category} category category entity | ||
* | ||
* @beta | ||
* @public | ||
*/ | ||
export const getCategoryUrl = (category: Partial<Category>): string => { | ||
export function getCategoryUrl(category: LinkedCategory): string { | ||
if (!category) return "/"; | ||
|
||
switch (category.type) { | ||
case "link": | ||
return getTranslatedProperty(category, "externalLink") || "/"; | ||
case "folder": | ||
case undefined: | ||
return "/"; | ||
case "link": | ||
return ( | ||
category.externalLink || | ||
category?.seoUrls?.[0]?.seoPathInfo || | ||
`/${getEntityPrefix(category)}/${category.internalLink}` | ||
); | ||
default: | ||
return category.seoUrls?.[0]?.seoPathInfo | ||
? `/${category.seoUrls[0].seoPathInfo}` | ||
: category.id | ||
? `/navigation/${category.id}` | ||
: "/"; | ||
: `/${getEntityPrefix(category)}/${category.id}`; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
909ffcd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
shopware-frontends-docs – ./
shopware-frontends-docs-shopware-frontends.vercel.app
shopware-frontends-docs-git-main-shopware-frontends.vercel.app
shopware-frontends-docs.vercel.app
909ffcd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
frontends-demo – ./templates/vue-demo-store
frontends-demo.vercel.app
frontends-demo-shopware-frontends.vercel.app
frontends-demo-git-main-shopware-frontends.vercel.app