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

adding create resource button to overview page #134

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .github/workflows/i18n.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
run: |
if [[ $(git status --porcelain) ]]; then
echo "i18n generation caused changes, failing the build."
git diff
exit 1
else
echo "No changes detected."
Expand Down
2 changes: 2 additions & 0 deletions locales/en/plugin__kuadrant-console-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"Create": "Create",
"Create AuthPolicy": "Create AuthPolicy",
"Create DNS Policy": "Create DNS Policy",
"Create Gateway": "Create Gateway",
"Create HTTPRoute": "Create HTTPRoute",
"Create Policies in": "Create Policies in",
"Create Policy": "Create Policy",
"Create RateLimit Policy": "Create RateLimit Policy",
Expand Down
91 changes: 90 additions & 1 deletion src/components/KuadrantOverviewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
DropdownItem,
DropdownList,
MenuToggle,
Button,
} from '@patternfly/react-core';
import {
GlobeIcon,
Expand All @@ -37,16 +38,38 @@ import ResourceList from './ResourceList';
import { sortable } from '@patternfly/react-table';
import { INTERNAL_LINKS, EXTERNAL_LINKS } from '../constants/links';
import resourceGVKMapping from '../utils/latest';
import { useHistory } from 'react-router-dom';

export type MenuToggleElement = HTMLDivElement | HTMLButtonElement;

interface Resource {
name: string;
gvk: {
group: string;
version: string;
kind: string;
};
}
export const resources: Resource[] = [
{ name: 'AuthPolicies', gvk: resourceGVKMapping['AuthPolicy'] },
{ name: 'DNSPolicies', gvk: resourceGVKMapping['DNSPolicy'] },
{ name: 'RateLimitPolicies', gvk: resourceGVKMapping['RateLimitPolicy'] },
{ name: 'TLSPolicies', gvk: resourceGVKMapping['TLSPolicy'] },
];
const KuadrantOverviewPage: React.FC = () => {
const history = useHistory();
const { t } = useTranslation('plugin__kuadrant-console-plugin');
const { ns } = useParams<{ ns: string }>();
const [activeNamespace, setActiveNamespace] = useActiveNamespace();
const [isExpanded, setIsExpanded] = React.useState(true);
const [isOpen, setIsOpen] = React.useState(false);
const [isCreateOpen, setIsCreateOpen] = React.useState(false);
const [hideCard, setHideCard] = React.useState(
sessionStorage.getItem('hideGettingStarted') === 'true',
);
const onToggleClick = () => {
setIsCreateOpen(!isCreateOpen);
};

React.useEffect(() => {
if (ns && ns !== activeNamespace) {
Expand Down Expand Up @@ -118,6 +141,30 @@ const KuadrantOverviewPage: React.FC = () => {
},
];

const handleCreateResource = (resource) => {
const resolvedNamespace = activeNamespace === '#ALL_NS#' ? 'default' : activeNamespace;

if (resource === 'Gateway') {
const gateway = resourceGVKMapping['Gateway'];
history.push(
`/k8s/ns/${resolvedNamespace}/${gateway.group}~${gateway.version}~${gateway.kind}/~new`,
);
} else {
const httpRoute = resourceGVKMapping['HTTPRoute'];
history.push(
`/k8s/ns/${resolvedNamespace}/${httpRoute.group}~${httpRoute.version}~${httpRoute.kind}/~new`,
);
}
};

const onMenuSelect = (_event: React.MouseEvent<Element, MouseEvent>, policyType: string) => {
const resource = resourceGVKMapping[policyType];
const resolvedNamespace = activeNamespace === '#ALL_NS#' ? 'default' : activeNamespace;
const targetUrl = `/k8s/ns/${resolvedNamespace}/${resource.group}~${resource.version}~${resource.kind}/~new`;
history.push(targetUrl);
setIsOpen(false);
};

return (
<>
<Helmet>
Expand Down Expand Up @@ -288,6 +335,37 @@ const KuadrantOverviewPage: React.FC = () => {
<Card>
<CardTitle>
<Title headingLevel="h2">{t('Policies')}</Title>
<Dropdown
isOpen={isCreateOpen}
onSelect={onMenuSelect}
onOpenChange={setIsCreateOpen}
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
<MenuToggle
ref={toggleRef}
onClick={onToggleClick}
isExpanded={isCreateOpen}
variant="primary"
className="kuadrant-overview-create-button pf-u-mt-md pf-u-mr-md"
>
{t('Create Policy')}
</MenuToggle>
)}
>
<DropdownList class="kuadrant-overview-create-list pf-u-p-0">
<DropdownItem value="AuthPolicy" key="auth-policy">
{t('AuthPolicy')}
</DropdownItem>
<DropdownItem value="RateLimitPolicy" key="rate-limit-policy">
{t('RateLimitPolicy')}
</DropdownItem>
<DropdownItem value="DNSPolicy" key="dns-policy">
{t('DNSPolicy')}
</DropdownItem>
<DropdownItem value="TLSPolicy" key="tls-policy">
{t('TLSPolicy')}
</DropdownItem>
</DropdownList>
</Dropdown>
</CardTitle>
<CardBody className="pf-u-p-10">
<ResourceList
Expand All @@ -305,12 +383,17 @@ const KuadrantOverviewPage: React.FC = () => {
</Card>
</FlexItem>
</Flex>

<Flex className="pf-u-mt-xl">
<FlexItem flex={{ default: 'flex_1' }}>
<Card>
<CardTitle>
<Title headingLevel="h2">{t('Gateways')}</Title>
<Button
onClick={() => handleCreateResource('Gateway')}
className="kuadrant-overview-create-button pf-u-mt-md pf-u-mr-md"
>
{t(`Create Gateway`)}
</Button>
</CardTitle>
<CardBody className="pf-u-p-10">
<ResourceList
Expand All @@ -326,6 +409,12 @@ const KuadrantOverviewPage: React.FC = () => {
<Card>
<CardTitle>
<Title headingLevel="h2">{t('APIs / HTTPRoutes')}</Title>
<Button
onClick={() => handleCreateResource('HTTPRoute')}
className="kuadrant-overview-create-button pf-u-mt-md pf-u-mr-md"
>
{t(`Create HTTPRoute`)}
</Button>
</CardTitle>
<CardBody className="pf-u-p-10">
<ResourceList
Expand Down
12 changes: 11 additions & 1 deletion src/components/kuadrant.css
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@

.kuadrant-policy-list-body {
width: 100%;
}
}

.kuadrant-overview-create-button {
position: absolute;
top: 0;
right: 0;
}

.kuadrant-overview-create-list, .kuadrant-overview-create-button {
font-family: RedHatText, helvetica, arial, sans-serif;
}
Loading