-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56376ac
commit 3b31788
Showing
7 changed files
with
243 additions
and
77 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
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,94 @@ | ||
// SPDX-FileCopyrightText: 2024 Dusan Mijatovic (Netherlands eScience Center) | ||
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import Alert from '@mui/material/Alert' | ||
import {CategoryEntry} from '~/types/Category' | ||
import {TreeNode} from '~/types/TreeNode' | ||
import ContentLoader from '../layout/ContentLoader' | ||
import {CategoryList} from './CategoryList' | ||
import List from '@mui/material/List' | ||
|
||
type CategoriesDialogBodyProps={ | ||
categories: TreeNode<CategoryEntry>[], | ||
state: 'loading' | 'error' | 'ready' | 'saving', | ||
errorMsg: string | null | ||
noItemsMsg: string | ||
selectedCategoryIds: Set<string>, | ||
setSelectedCategoryIds: (ids:Set<string>)=>void | ||
} | ||
|
||
export default function CategoriesDialogBody({ | ||
categories,state,errorMsg,noItemsMsg, | ||
selectedCategoryIds, setSelectedCategoryIds | ||
}:CategoriesDialogBodyProps) { | ||
|
||
|
||
function isSelected(node: TreeNode<CategoryEntry>) { | ||
const val = node.getValue() | ||
// default approach | ||
// return selectedCategoryIds.has(val.id) | ||
|
||
// ALTERNATIVE APPROACH | ||
// directly selected | ||
if (selectedCategoryIds.has(val.id)) return true | ||
|
||
// any of children selected? | ||
const found = node.children().find(item=>{ | ||
return isSelected(item) | ||
}) | ||
// debugger | ||
if (found) return true | ||
// none of children selected either | ||
return false | ||
} | ||
|
||
function onSelect(node: TreeNode<CategoryEntry>) { | ||
const val = node.getValue() | ||
if (selectedCategoryIds.has(val.id)) { | ||
selectedCategoryIds.delete(val.id) | ||
} else { | ||
selectedCategoryIds.add(val.id) | ||
} | ||
setSelectedCategoryIds(new Set(selectedCategoryIds)) | ||
} | ||
|
||
switch (state) { | ||
case 'loading': | ||
case 'saving': | ||
return ( | ||
<div className="flex-1 flex justify-center items-center"> | ||
<ContentLoader/> | ||
</div> | ||
) | ||
|
||
case 'error': | ||
return ( | ||
<Alert severity="error" sx={{marginTop: '0.5rem'}}> | ||
{errorMsg ?? '500 - Unexpected error'} | ||
</Alert> | ||
) | ||
|
||
case 'ready': | ||
return ( | ||
<> | ||
{(categories === null || categories.length === 0) | ||
? | ||
<Alert severity="info" sx={{'padding': '2rem'}}> | ||
{noItemsMsg} | ||
</Alert> | ||
: | ||
<List> | ||
<CategoryList | ||
categories={categories} | ||
isSelected={isSelected} | ||
onSelect={onSelect} | ||
/> | ||
</List> | ||
} | ||
</> | ||
) | ||
} | ||
|
||
} |
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,127 @@ | ||
// SPDX-FileCopyrightText: 2024 Dusan Mijatovic (Netherlands eScience Center) | ||
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {useState} from 'react' | ||
import ExpandLess from '@mui/icons-material/ExpandLess' | ||
import ExpandMore from '@mui/icons-material/ExpandMore' | ||
import Checkbox from '@mui/material/Checkbox' | ||
import Collapse from '@mui/material/Collapse' | ||
import IconButton from '@mui/material/IconButton' | ||
import List from '@mui/material/List' | ||
import ListItem from '@mui/material/ListItem' | ||
import ListItemButton from '@mui/material/ListItemButton' | ||
import ListItemText from '@mui/material/ListItemText' | ||
|
||
import {TreeNode} from '~/types/TreeNode' | ||
import {CategoryEntry} from '~/types/Category' | ||
|
||
type NodeWithChildrenProps = { | ||
node: TreeNode<CategoryEntry> | ||
onSelect: (node: TreeNode<CategoryEntry>) => void | ||
isSelected: (node: TreeNode<CategoryEntry>) => boolean | ||
} | ||
|
||
function NodeWithChildren({ | ||
node, | ||
isSelected, | ||
onSelect | ||
}:NodeWithChildrenProps){ | ||
// open/close children panel | ||
const [open,setOpen] = useState(true) | ||
// get category | ||
const cat = node.getValue() | ||
|
||
return ( | ||
<> | ||
<ListItem | ||
key={cat.id} | ||
title={cat.short_name} | ||
secondaryAction={ | ||
<IconButton | ||
edge="end" | ||
aria-label="expand" | ||
onClick={()=>setOpen(!open)} | ||
> | ||
{open ? <ExpandLess /> : <ExpandMore />} | ||
</IconButton> | ||
} | ||
disablePadding | ||
dense | ||
> | ||
<ListItemButton onClick={() => onSelect(node)}> | ||
<Checkbox disableRipple checked={isSelected(node)} /> | ||
<ListItemText | ||
primary={cat.name} | ||
// secondary={cat.name} | ||
/> | ||
</ListItemButton> | ||
</ListItem> | ||
{/* Children block */} | ||
<Collapse | ||
in={open} | ||
timeout="auto" | ||
unmountOnExit={false} | ||
> | ||
<List sx={{pl:'1rem'}}> | ||
<CategoryList | ||
categories={node.children()} | ||
onSelect={onSelect} | ||
isSelected={isSelected} | ||
/> | ||
</List> | ||
</Collapse> | ||
</> | ||
) | ||
} | ||
|
||
export type CategoryListProps = { | ||
onSelect: (node: TreeNode<CategoryEntry>) => void | ||
isSelected: (node: TreeNode<CategoryEntry>) => boolean | ||
categories: TreeNode<CategoryEntry>[] | ||
} | ||
|
||
export function CategoryList({ | ||
categories, | ||
isSelected, | ||
onSelect | ||
}: CategoryListProps) { | ||
// loop all categories | ||
return categories.map(node => { | ||
const cat = node.getValue() | ||
|
||
// single cat element without children | ||
if (node.childrenCount() === 0) { | ||
return ( | ||
<ListItem | ||
key={cat.id} | ||
// title={cat.name} | ||
title={cat.short_name} | ||
disablePadding | ||
dense | ||
> | ||
<ListItemButton onClick={() => onSelect(node)}> | ||
<Checkbox | ||
disableRipple | ||
checked={isSelected(node)} | ||
/> | ||
<ListItemText | ||
primary={cat.name} | ||
// secondary={cat.name} | ||
/> | ||
</ListItemButton> | ||
</ListItem> | ||
) | ||
} | ||
|
||
return ( | ||
<NodeWithChildren | ||
key={cat.id} | ||
node={node} | ||
isSelected={isSelected} | ||
onSelect={onSelect} | ||
/> | ||
) | ||
}) | ||
} |
1 change: 1 addition & 0 deletions
1
frontend/components/software/TreeSelect.tsx → frontend/components/category/TreeSelect.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// SPDX-FileCopyrightText: 2024 Dusan Mijatovic (Netherlands eScience Center) | ||
// SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) <[email protected]> | ||
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center | ||
// | ||
|
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