Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cyclops-ui): Filter Modules by TargetNamespace #666

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions cyclops-ctrl/internal/mapper/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ func ModuleListToDTO(modules []cyclopsv1alpha1.Module) []dto.Module {
for _, module := range modules {
values := make(map[string]interface{})
out = append(out, dto.Module{
Name: module.Name,
Namespace: module.Namespace,
Version: module.Spec.TemplateRef.Version,
Template: k8sTemplateRefToDTO(module.Spec.TemplateRef, module.Status.TemplateResolvedVersion),
Values: values,
IconURL: module.Status.IconURL,
Name: module.Name,
Namespace: module.Namespace,
TargetNamespace: mapTargetNamespace(module.Spec.TargetNamespace),
Version: module.Spec.TemplateRef.Version,
Template: k8sTemplateRefToDTO(module.Spec.TemplateRef, module.Status.TemplateResolvedVersion),
Values: values,
IconURL: module.Status.IconURL,
})
}

Expand Down
93 changes: 75 additions & 18 deletions cyclops-ui/src/components/pages/Modules/Modules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,40 @@ import { mapResponseError } from "../../../utils/api/errors";

const { Title } = Typography;

interface ModuleInterface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this for now, we'd have to implement it in more places. Let's keep the scope of the PR to just what was defined in the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure will remove it!

name: string;
namespace: string;
targetNamespace: string;
template: {
repo: string;
path: string;
version: string;
resolvedVersion: string;
sourceType: string;
};
version: string;
values: { [key: string]: any };
status: string;
iconURL: string;
reconciliationStatus: { [key: string]: any };
}

const Modules = () => {
const [allData, setAllData] = useState([]);
const [filteredData, setFilteredData] = useState([]);
const [allData, setAllData] = useState<ModuleInterface[]>([]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert to empty array

const [filteredData, setFilteredData] = useState<ModuleInterface[]>([]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert to empty array

const [loadingModules, setLoadingModules] = useState(false);
const [moduleHealthFilter, setModuleHealthFilter] = useState<string[]>([
"Healthy",
"Unhealthy",
"Progressing",
"Unknown",
]);
const [moduleNamespaceFilter, setModuleNamespaceFilter] = useState<string[]>(
[],
);
const [searchInputFilter, setsearchInputFilter] = useState("");
const resourceFilter = ["Healthy", "Unhealthy", "Progressing", "Unknown"];
const [namespaceFilterData, setNamespaceFilterData] = useState<string[]>([]);
const [error, setError] = useState({
message: "",
description: "",
Expand All @@ -49,6 +71,7 @@ const Modules = () => {
.get(`/api/modules/list`)
.then((res) => {
setAllData(res.data);
populateNamespaceData(res.data);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the call of this function outside the fetchModules loop.

In this useEffect, before the fetchModules function, create a call towards /api/namespaces. This will return all available namespaces with which you can populate both the namespaceFilterData and the moduleNamespaceFilter

setLoadingModules(false);
})
.catch((error) => {
Expand All @@ -64,6 +87,16 @@ const Modules = () => {
};
}, []);

const populateNamespaceData = (allData: ModuleInterface[]) => {
const namespaceData = allData
.map((module) => module.targetNamespace)
.filter((targetNamespace, index, self) => {
return self.indexOf(targetNamespace) === index;
});
setNamespaceFilterData(namespaceData);
setModuleNamespaceFilter(namespaceData);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot use setModuleNamespaceFilter here because it is called every 10 seconds and resets the filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true didnt check this my bad, will update

};

useEffect(() => {
var updatedList = [...allData];
updatedList = updatedList.filter((module: any) => {
Expand All @@ -72,34 +105,58 @@ const Modules = () => {
-1
);
});
const newfilteredData = updatedList.filter((module: any) =>
moduleHealthFilter
.map((status) => status.toLowerCase())
.includes(module.status.toLowerCase()),
const newfilteredData = updatedList.filter(
(module: any) =>
moduleHealthFilter
.map((status) => status.toLowerCase())
.includes(module.status.toLowerCase()) &&
moduleNamespaceFilter
.map((targetNamespace) => targetNamespace.toLowerCase())
.includes(module.targetNamespace.toLowerCase()),
);
setFilteredData(newfilteredData);
}, [moduleHealthFilter, allData, searchInputFilter]);
}, [moduleNamespaceFilter, moduleHealthFilter, allData, searchInputFilter]);

const handleClick = () => {
window.location.href = "/modules/new";
};
const handleSelectItem = (selectedItems: any[]) => {
setModuleHealthFilter(selectedItems);
};
const handleNamespaceSelectItem = (selectedItems: any[]) => {
setModuleNamespaceFilter(selectedItems);
};

const resourceFilterPopover = () => {
return (
<Checkbox.Group
style={{ display: "block" }}
onChange={handleSelectItem}
value={moduleHealthFilter}
>
{resourceFilter.map((item, index) => (
<Checkbox key={index} value={item}>
{item}
</Checkbox>
))}
</Checkbox.Group>
<>
<Checkbox.Group
style={{ display: "block", margin: "5px 0px" }}
onChange={handleSelectItem}
value={moduleHealthFilter}
>
<b>Health</b>
<br />
{resourceFilter.map((item, index) => (
<Checkbox key={index} value={item}>
{item}
</Checkbox>
))}
</Checkbox.Group>
<Checkbox.Group
style={{ display: "block", margin: "5px 0px" }}
onChange={handleNamespaceSelectItem}
value={moduleNamespaceFilter}
>
<b>Namespace</b>
<br />
{namespaceFilterData.map((item, index) => (
<Checkbox key={index} value={item}>
{item}
</Checkbox>
))}
</Checkbox.Group>
</>
);
};
const handleSearch = (event: any) => {
Expand Down
Loading