mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
Introduce stale while revalidate pattern (#1555)
This Improves the frontend performance with stale while revalidate pattern. There are noticeable performance problems in the frontend that needed addressing. While implementing the stale-while-revalidate pattern to display cached responses while re-fetching up-to-date data in the background, in the same vein we used the opportunity to remove legacy code involving redux as much as possible, cleaned up many components and converted them to functional react components. Co-authored-by: Sebastian Sdorra <sebastian.sdorra@cloudogu.com> Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
committed by
GitHub
parent
ad5c8102c0
commit
3a8d031ed5
@@ -21,47 +21,37 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
import React, { FC, useState } from "react";
|
||||
import React, { FC, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Permission } from "@scm-manager/ui-types";
|
||||
import { ConfirmAlert } from "@scm-manager/ui-components";
|
||||
import { Namespace, Permission, Repository } from "@scm-manager/ui-types";
|
||||
import { ConfirmAlert, ErrorNotification } from "@scm-manager/ui-components";
|
||||
import { useDeletePermission } from "@scm-manager/ui-api";
|
||||
|
||||
type Props = {
|
||||
permission: Permission;
|
||||
namespace: string;
|
||||
repoName: string;
|
||||
namespaceOrRepository: Namespace | Repository;
|
||||
confirmDialog?: boolean;
|
||||
deletePermission: (permission: Permission, namespace: string, repoName: string) => void;
|
||||
loading: boolean;
|
||||
};
|
||||
|
||||
const DeletePermissionButton: FC<Props> = ({
|
||||
confirmDialog = true,
|
||||
permission,
|
||||
namespace,
|
||||
deletePermission,
|
||||
repoName
|
||||
}) => {
|
||||
const DeletePermissionButton: FC<Props> = ({ namespaceOrRepository, permission, confirmDialog = true }) => {
|
||||
const [showConfirmAlert, setShowConfirmAlert] = useState(false);
|
||||
const { isLoading, error, remove, isDeleted } = useDeletePermission(namespaceOrRepository);
|
||||
const [t] = useTranslation("repos");
|
||||
useEffect(() => {
|
||||
if (isDeleted) {
|
||||
setShowConfirmAlert(false);
|
||||
}
|
||||
}, [isDeleted]);
|
||||
|
||||
const deletePermissionCallback = () => {
|
||||
deletePermission(permission, namespace, repoName);
|
||||
const deletePermission = () => {
|
||||
remove(permission);
|
||||
};
|
||||
|
||||
const confirmDelete = () => {
|
||||
setShowConfirmAlert(true);
|
||||
};
|
||||
|
||||
const isDeletable = () => {
|
||||
return permission._links.delete;
|
||||
};
|
||||
|
||||
const action = confirmDialog ? confirmDelete : deletePermissionCallback;
|
||||
|
||||
if (!isDeletable()) {
|
||||
return null;
|
||||
}
|
||||
const action = confirmDialog ? confirmDelete : deletePermission;
|
||||
|
||||
if (showConfirmAlert) {
|
||||
return (
|
||||
@@ -72,7 +62,8 @@ const DeletePermissionButton: FC<Props> = ({
|
||||
{
|
||||
className: "is-outlined",
|
||||
label: t("permission.delete-permission-button.confirm-alert.submit"),
|
||||
onClick: () => deletePermissionCallback()
|
||||
isLoading,
|
||||
onClick: () => deletePermission()
|
||||
},
|
||||
{
|
||||
label: t("permission.delete-permission-button.confirm-alert.cancel"),
|
||||
@@ -85,11 +76,14 @@ const DeletePermissionButton: FC<Props> = ({
|
||||
}
|
||||
|
||||
return (
|
||||
<a className="level-item" onClick={action}>
|
||||
<span className="icon is-small">
|
||||
<i className="fas fa-trash" />
|
||||
</span>
|
||||
</a>
|
||||
<>
|
||||
<ErrorNotification error={error} />
|
||||
<a className="level-item" onClick={action}>
|
||||
<span className="icon is-small">
|
||||
<i className="fas fa-trash" />
|
||||
</span>
|
||||
</a>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -26,29 +26,37 @@ import React, { FC } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { LabelWithHelpIcon, Notification } from "@scm-manager/ui-components";
|
||||
import SinglePermission from "../containers/SinglePermission";
|
||||
import { PermissionCollection, RepositoryRole } from "@scm-manager/ui-types";
|
||||
import { Namespace, PermissionCollection, Repository, RepositoryRole } from "@scm-manager/ui-types";
|
||||
|
||||
type Props = {
|
||||
availableRepositoryRoles: RepositoryRole[];
|
||||
availableRoles: RepositoryRole[];
|
||||
availableVerbs: string[];
|
||||
namespace: string;
|
||||
repoName?: string;
|
||||
permissions: PermissionCollection;
|
||||
namespaceOrRepository: Namespace | Repository;
|
||||
};
|
||||
|
||||
const PermissionsTable: FC<Props> = ({
|
||||
availableRepositoryRoles,
|
||||
availableRoles,
|
||||
availableVerbs,
|
||||
namespace,
|
||||
repoName,
|
||||
permissions,
|
||||
namespaceOrRepository,
|
||||
permissions: permissionCollection
|
||||
}) => {
|
||||
const [t] = useTranslation("repos");
|
||||
|
||||
if (permissions?.length === 0) {
|
||||
if (permissionCollection._embedded.permissions?.length === 0) {
|
||||
return <Notification type="info">{t("permission.noPermissions")}</Notification>;
|
||||
}
|
||||
|
||||
permissionCollection?._embedded.permissions.sort((a, b) => {
|
||||
if (a.name > b.name) {
|
||||
return 1;
|
||||
} else if (a.name < b.name) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<table className="card-table table is-hoverable is-fullwidth">
|
||||
<thead>
|
||||
@@ -69,14 +77,13 @@ const PermissionsTable: FC<Props> = ({
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{permissions.map((permission) => {
|
||||
{permissionCollection?._embedded.permissions.map(permission => {
|
||||
return (
|
||||
<SinglePermission
|
||||
availableRepositoryRoles={availableRepositoryRoles}
|
||||
availableRepositoryVerbs={availableVerbs}
|
||||
availableRoles={availableRoles}
|
||||
availableVerbs={availableVerbs}
|
||||
key={permission.name + permission.groupPermission.toString()}
|
||||
namespace={namespace}
|
||||
repoName={repoName}
|
||||
namespaceOrRepository={namespaceOrRepository}
|
||||
permission={permission}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -21,11 +21,10 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
import React from "react";
|
||||
import { WithTranslation, withTranslation } from "react-i18next";
|
||||
import React, { FC } from "react";
|
||||
import { Select } from "@scm-manager/ui-components";
|
||||
|
||||
type Props = WithTranslation & {
|
||||
type Props = {
|
||||
availableRoles?: string[];
|
||||
handleRoleChange: (p: string) => void;
|
||||
role: string;
|
||||
@@ -34,34 +33,36 @@ type Props = WithTranslation & {
|
||||
loading?: boolean;
|
||||
};
|
||||
|
||||
class RoleSelector extends React.Component<Props> {
|
||||
render() {
|
||||
const { availableRoles, role, handleRoleChange, loading, label, helpText } = this.props;
|
||||
const emptyOption = {
|
||||
label: "",
|
||||
value: ""
|
||||
};
|
||||
|
||||
if (!availableRoles) return null;
|
||||
const createSelectOptions = (roles: string[]) => {
|
||||
return roles.map(role => {
|
||||
return {
|
||||
label: role,
|
||||
value: role
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const options = role ? this.createSelectOptions(availableRoles) : ["", ...this.createSelectOptions(availableRoles)];
|
||||
|
||||
return (
|
||||
<Select
|
||||
onChange={handleRoleChange}
|
||||
value={role ? role : ""}
|
||||
options={options}
|
||||
loading={loading}
|
||||
label={label}
|
||||
helpText={helpText}
|
||||
/>
|
||||
);
|
||||
const RoleSelector: FC<Props> = ({ availableRoles, role, handleRoleChange, loading, label, helpText }) => {
|
||||
if (!availableRoles) {
|
||||
return null;
|
||||
}
|
||||
const options = role ? createSelectOptions(availableRoles) : [emptyOption, ...createSelectOptions(availableRoles)];
|
||||
|
||||
createSelectOptions(roles: string[]) {
|
||||
return roles.map(role => {
|
||||
return {
|
||||
label: role,
|
||||
value: role
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
return (
|
||||
<Select
|
||||
onChange={handleRoleChange}
|
||||
value={role ? role : ""}
|
||||
options={options}
|
||||
loading={loading}
|
||||
label={label}
|
||||
helpText={helpText}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default withTranslation("repos")(RoleSelector);
|
||||
export default RoleSelector;
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
||||
// @ts-ignore
|
||||
import React, { FC } from "react";
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { mount, shallow } from "@scm-manager/ui-tests/enzyme-router";
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import "@scm-manager/ui-tests/enzyme";
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import "@scm-manager/ui-tests/i18n";
|
||||
import DeletePermissionButton from "./DeletePermissionButton";
|
||||
|
||||
jest.mock("@scm-manager/ui-components", () => ({
|
||||
ConfirmAlert: (({ children }) => <div className="modal">{children}</div>) as FC<never>,
|
||||
DeleteButton: jest.requireActual("@scm-manager/ui-components").DeleteButton
|
||||
}));
|
||||
|
||||
describe("DeletePermissionButton", () => {
|
||||
it("should render nothing, if the delete link is missing", () => {
|
||||
const permission = {
|
||||
_links: {}
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
const navLink = shallow(<DeletePermissionButton permission={permission} deletePermission={() => {}} />);
|
||||
expect(navLink.text()).toBe("");
|
||||
});
|
||||
|
||||
it("should render the delete icon", () => {
|
||||
const permission = {
|
||||
_links: {
|
||||
delete: {
|
||||
href: "/permission"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
const deleteIcon = mount(<DeletePermissionButton permission={permission} deletePermission={() => {}} />);
|
||||
expect(deleteIcon.html()).not.toBe("");
|
||||
});
|
||||
|
||||
it("should open the confirm dialog on button click", () => {
|
||||
const permission = {
|
||||
_links: {
|
||||
delete: {
|
||||
href: "/permission"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
const button = mount(<DeletePermissionButton permission={permission} deletePermission={() => {}} />);
|
||||
button.find(".fa-trash").simulate("click");
|
||||
|
||||
expect(button.find(".modal")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should call the delete permission function with delete url", () => {
|
||||
const permission = {
|
||||
_links: {
|
||||
delete: {
|
||||
href: "/permission"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let calledUrl = null;
|
||||
|
||||
function capture(permission) {
|
||||
calledUrl = permission._links.delete.href;
|
||||
}
|
||||
|
||||
const button = mount(
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
||||
// @ts-ignore
|
||||
<DeletePermissionButton permission={permission} confirmDialog={false} deletePermission={capture} />
|
||||
);
|
||||
button.find(".fa-trash").simulate("click");
|
||||
|
||||
expect(calledUrl).toBe("/permission");
|
||||
});
|
||||
});
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import * as validator from "./permissionValidation";
|
||||
|
||||
describe("permission validation", () => {
|
||||
it("should return true if permission is valid and does not exist", () => {
|
||||
const permissions = [];
|
||||
const name = "PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(validator.isPermissionValid(name, groupPermission, permissions)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return true if permission is valid and does not exists with same group permission", () => {
|
||||
const permissions = [
|
||||
{
|
||||
name: "PermissionName",
|
||||
groupPermission: true,
|
||||
type: "READ",
|
||||
_links: {},
|
||||
verbs: []
|
||||
}
|
||||
];
|
||||
const name = "PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(validator.isPermissionValid(name, groupPermission, permissions)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if permission is valid but exists", () => {
|
||||
const permissions = [
|
||||
{
|
||||
name: "PermissionName",
|
||||
groupPermission: false,
|
||||
type: "READ",
|
||||
_links: {},
|
||||
verbs: []
|
||||
}
|
||||
];
|
||||
const name = "PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(validator.isPermissionValid(name, groupPermission, permissions)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false if permission does not exist but is invalid", () => {
|
||||
const permissions = [];
|
||||
const name = "@PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(validator.isPermissionValid(name, groupPermission, permissions)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false if permission is not valid and does not exist", () => {
|
||||
const permissions = [];
|
||||
const name = "@PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(validator.isPermissionValid(name, groupPermission, permissions)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import { validation } from "@scm-manager/ui-components";
|
||||
import { PermissionCollection } from "@scm-manager/ui-types";
|
||||
|
||||
const isNameValid = validation.isNameValid;
|
||||
|
||||
export { isNameValid };
|
||||
|
||||
export const isPermissionValid = (name: string, groupPermission: boolean, permissions: PermissionCollection) => {
|
||||
return isNameValid(name) && !currentPermissionIncludeName(name, groupPermission, permissions);
|
||||
};
|
||||
|
||||
const currentPermissionIncludeName = (name: string, groupPermission: boolean, permissions: PermissionCollection) => {
|
||||
for (let i = 0; i < permissions.length; i++) {
|
||||
if (permissions[i].name === name && permissions[i].groupPermission === groupPermission) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
Reference in New Issue
Block a user