-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): added breadcrumbs for categories on create/edit modal (…
…#3420) What: - Adds breadcrumbs to create modal - Adds breadcrumbs to edit modal <img width="581" alt="2" src="https://user-images.githubusercontent.com/5105988/223782603-f168d554-65bd-4cfc-bdcd-eabdd9f06b20.png"> <img width="1115" alt="1" src="https://user-images.githubusercontent.com/5105988/223782607-1ae441c9-c9eb-4cb0-9015-2038db55dd64.png"> RESOLVES CORE-1210
- Loading branch information
Showing
6 changed files
with
130 additions
and
15 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 @@ | ||
--- | ||
"@medusajs/admin-ui": patch | ||
--- | ||
|
||
feat(admin-ui): added breadcrumbs for categories on create/edit modal |
54 changes: 54 additions & 0 deletions
54
packages/admin-ui/ui/src/domain/product-categories/components/tree-crumbs.tsx
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,54 @@ | ||
import React from "react" | ||
import { ProductCategory } from "@medusajs/medusa" | ||
import { getAncestors } from "../utils" | ||
|
||
type TreeCrumbsProps = React.HtmlHTMLAttributes<HTMLDivElement> & { | ||
nodes: ProductCategory[] | ||
currentNode: ProductCategory | ||
showPlaceholder: boolean | ||
placeholderText: string | ||
} | ||
|
||
const TreeCrumbs: React.FC<TreeCrumbsProps> = ({ | ||
nodes, | ||
currentNode, | ||
showPlaceholder = false, | ||
placeholderText = "", | ||
...props | ||
}) => { | ||
const ancestors = getAncestors(currentNode, nodes) | ||
|
||
return ( | ||
<span {...props}> | ||
<span className="text-grey-40"> | ||
{ancestors.map((ancestor, index) => { | ||
const categoryName = ancestor.name | ||
|
||
return ( | ||
<div key={ancestor.id} className="inline-block"> | ||
<span> | ||
{categoryName.length > 25 | ||
? categoryName.substring(0, 25) + "..." | ||
: categoryName} | ||
</span> | ||
|
||
{(showPlaceholder || ancestors.length !== index + 1) && ( | ||
<span className="mx-2">/</span> | ||
)} | ||
</div> | ||
) | ||
})} | ||
|
||
{showPlaceholder && ( | ||
<span> | ||
<span className="border-grey-40 rounded-[10px] border-[1px] border-dashed px-[8px] py-[4px]"> | ||
{placeholderText} | ||
</span> | ||
</span> | ||
)} | ||
</span> | ||
</span> | ||
) | ||
} | ||
|
||
export default TreeCrumbs |
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
31 changes: 31 additions & 0 deletions
31
packages/admin-ui/ui/src/domain/product-categories/utils/index.tsx
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,31 @@ | ||
export const flattenCategoryTree = (rootCategories) => { | ||
return rootCategories.reduce((acc, category) => { | ||
if (category?.category_children.length) { | ||
acc = acc | ||
.concat(flattenCategoryTree(category.category_children)) | ||
.concat(category) | ||
} else { | ||
acc.push(category) | ||
} | ||
|
||
return acc | ||
}, []) | ||
} | ||
|
||
export const getAncestors = (targetNode, nodes, acc = []) => { | ||
let parentCategory = null | ||
|
||
acc.push(targetNode) | ||
|
||
if (targetNode.parent_category_id) { | ||
parentCategory = nodes.find((n) => n.id === targetNode.parent_category_id) | ||
|
||
acc = getAncestors(parentCategory, nodes, acc) | ||
} | ||
|
||
if (!parentCategory) { | ||
return acc.reverse() | ||
} | ||
|
||
return acc | ||
} |