replace confirmAlert function usage with the actual component

This commit is contained in:
Konstantin Schaper
2020-10-05 13:10:53 +02:00
parent e69e918d55
commit a9cf787364
5 changed files with 232 additions and 214 deletions

View File

@@ -21,14 +21,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
import React from "react"; import React, { FC, useState } from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { compose } from "redux"; import { compose } from "redux";
import { withRouter } from "react-router-dom"; import { withRouter } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { History } from "history"; import { History } from "history";
import { RepositoryRole } from "@scm-manager/ui-types"; import { RepositoryRole } from "@scm-manager/ui-types";
import { confirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components"; import { ConfirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components";
import { deleteRole, getDeleteRoleFailure, isDeleteRolePending } from "../modules/roles"; import { deleteRole, getDeleteRoleFailure, isDeleteRolePending } from "../modules/roles";
type Props = WithTranslation & { type Props = WithTranslation & {
@@ -42,48 +42,58 @@ type Props = WithTranslation & {
history: History; history: History;
}; };
class DeleteRepositoryRole extends React.Component<Props> { const DeleteRepositoryRole: FC<Props> = ({
static defaultProps = { confirmDialog = true,
confirmDialog: true history,
deleteRole,
role,
loading,
error,
t
}: Props) => {
const [showConfirmAlert, setShowConfirmAlert] = useState(false);
const roleDeleted = () => {
history.push("/admin/roles/");
}; };
roleDeleted = () => { const deleteRoleCallback = () => {
this.props.history.push("/admin/roles/"); deleteRole(role, roleDeleted);
}; };
deleteRole = () => { const confirmDelete = () => {
this.props.deleteRole(this.props.role, this.roleDeleted); setShowConfirmAlert(true);
}; };
confirmDelete = () => { const isDeletable = () => {
const { t } = this.props; return role._links.delete;
confirmAlert({ };
title: t("repositoryRole.delete.confirmAlert.title"),
message: t("repositoryRole.delete.confirmAlert.message"), const action = confirmDialog ? confirmDelete : deleteRoleCallback;
buttons: [
if (!isDeletable()) {
return null;
}
if (showConfirmAlert) {
return (
<ConfirmAlert
title={t("repositoryRole.delete.confirmAlert.title")}
message={t("repositoryRole.delete.confirmAlert.message")}
buttons={[
{ {
className: "is-outlined", className: "is-outlined",
label: t("repositoryRole.delete.confirmAlert.submit"), label: t("repositoryRole.delete.confirmAlert.submit"),
onClick: () => this.deleteRole() onClick: () => deleteRoleCallback()
}, },
{ {
label: t("repositoryRole.delete.confirmAlert.cancel"), label: t("repositoryRole.delete.confirmAlert.cancel"),
onClick: () => null onClick: () => null
} }
] ]}
}); close={() => setShowConfirmAlert(false)}
}; />
);
isDeletable = () => {
return this.props.role._links.delete;
};
render() {
const { loading, error, confirmDialog, t } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deleteRole;
if (!this.isDeletable()) {
return null;
} }
return ( return (
@@ -93,8 +103,7 @@ class DeleteRepositoryRole extends React.Component<Props> {
<Level right={<DeleteButton label={t("repositoryRole.delete.button")} action={action} loading={loading} />} /> <Level right={<DeleteButton label={t("repositoryRole.delete.button")} action={action} loading={loading} />} />
</> </>
); );
} };
}
const mapStateToProps = (state: any, ownProps: Props) => { const mapStateToProps = (state: any, ownProps: Props) => {
const loading = isDeleteRolePending(state, ownProps.role.name); const loading = isDeleteRolePending(state, ownProps.role.name);

View File

@@ -21,14 +21,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
import React from "react"; import React, { FC, useState } from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { compose } from "redux"; import { compose } from "redux";
import { withRouter } from "react-router-dom"; import { withRouter } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { History } from "history"; import { History } from "history";
import { Group } from "@scm-manager/ui-types"; import { Group } from "@scm-manager/ui-types";
import { confirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components"; import { ConfirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components";
import { deleteGroup, getDeleteGroupFailure, isDeleteGroupPending } from "../modules/groups"; import { deleteGroup, getDeleteGroupFailure, isDeleteGroupPending } from "../modules/groups";
type Props = WithTranslation & { type Props = WithTranslation & {
@@ -42,48 +42,49 @@ type Props = WithTranslation & {
history: History; history: History;
}; };
export class DeleteGroup extends React.Component<Props> { export const DeleteGroup: FC<Props> = ({ confirmDialog = true, group, history, t, deleteGroup, loading, error }) => {
static defaultProps = { const [showConfirmAlert, setShowConfirmAlert] = useState(false);
confirmDialog: true
const deleteGroupCallback = () => {
deleteGroup(group, groupDeleted);
}; };
deleteGroup = () => { const groupDeleted = () => {
this.props.deleteGroup(this.props.group, this.groupDeleted); history.push("/groups/");
}; };
groupDeleted = () => { const confirmDelete = () => {
this.props.history.push("/groups/"); setShowConfirmAlert(true);
}; };
confirmDelete = () => { const isDeletable = () => {
const { t } = this.props; return group._links.delete;
confirmAlert({ };
title: t("deleteGroup.confirmAlert.title"),
message: t("deleteGroup.confirmAlert.message"), const action = confirmDialog ? confirmDelete : deleteGroupCallback;
buttons: [
if (!isDeletable()) {
return null;
}
if (showConfirmAlert) {
return (
<ConfirmAlert
title={t("deleteGroup.confirmAlert.title")}
message={t("deleteGroup.confirmAlert.message")}
buttons={[
{ {
className: "is-outlined", className: "is-outlined",
label: t("deleteGroup.confirmAlert.submit"), label: t("deleteGroup.confirmAlert.submit"),
onClick: () => this.deleteGroup() onClick: () => deleteGroupCallback()
}, },
{ {
label: t("deleteGroup.confirmAlert.cancel"), label: t("deleteGroup.confirmAlert.cancel"),
onClick: () => null onClick: () => null
} }
] ]}
}); />
}; );
isDeletable = () => {
return this.props.group._links.delete;
};
render() {
const { loading, error, confirmDialog, t } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deleteGroup;
if (!this.isDeletable()) {
return null;
} }
return ( return (
@@ -93,8 +94,7 @@ export class DeleteGroup extends React.Component<Props> {
<Level right={<DeleteButton label={t("deleteGroup.button")} action={action} loading={loading} />} /> <Level right={<DeleteButton label={t("deleteGroup.button")} action={action} loading={loading} />} />
</> </>
); );
} };
}
const mapStateToProps = (state: any, ownProps: Props) => { const mapStateToProps = (state: any, ownProps: Props) => {
const loading = isDeleteGroupPending(state, ownProps.group.name); const loading = isDeleteGroupPending(state, ownProps.group.name);

View File

@@ -21,13 +21,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
import React from "react"; import React, { FC, useState } from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { compose } from "redux"; import { compose } from "redux";
import { RouteComponentProps, withRouter } from "react-router-dom"; import { RouteComponentProps, withRouter } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { Repository } from "@scm-manager/ui-types"; import { Repository } from "@scm-manager/ui-types";
import { confirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components"; import { ConfirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components";
import { deleteRepo, getDeleteRepoFailure, isDeleteRepoPending } from "../modules/repos"; import { deleteRepo, getDeleteRepoFailure, isDeleteRepoPending } from "../modules/repos";
type Props = RouteComponentProps & type Props = RouteComponentProps &
@@ -39,48 +39,50 @@ type Props = RouteComponentProps &
deleteRepo: (p1: Repository, p2: () => void) => void; deleteRepo: (p1: Repository, p2: () => void) => void;
}; };
class DeleteRepo extends React.Component<Props> { const DeleteRepo: FC<Props> = ({ confirmDialog = true, repository, history, deleteRepo, loading, error, t }: Props) => {
static defaultProps = { const [showConfirmAlert, setShowConfirmAlert] = useState(false);
confirmDialog: true
const deleted = () => {
history.push("/repos/");
}; };
deleted = () => { const deleteRepoCallback = () => {
this.props.history.push("/repos/"); deleteRepo(repository, deleted);
}; };
deleteRepo = () => { const confirmDelete = () => {
this.props.deleteRepo(this.props.repository, this.deleted); setShowConfirmAlert(true);
}; };
confirmDelete = () => { const isDeletable = () => {
const { t } = this.props; return repository._links.delete;
confirmAlert({ };
title: t("deleteRepo.confirmAlert.title"),
message: t("deleteRepo.confirmAlert.message"), const action = confirmDialog ? confirmDelete : deleteRepoCallback;
buttons: [
if (!isDeletable()) {
return null;
}
if (showConfirmAlert) {
return (
<ConfirmAlert
title={t("deleteRepo.confirmAlert.title")}
message={t("deleteRepo.confirmAlert.message")}
buttons={[
{ {
className: "is-outlined", className: "is-outlined",
label: t("deleteRepo.confirmAlert.submit"), label: t("deleteRepo.confirmAlert.submit"),
onClick: () => this.deleteRepo() onClick: () => deleteRepoCallback()
}, },
{ {
label: t("deleteRepo.confirmAlert.cancel"), label: t("deleteRepo.confirmAlert.cancel"),
onClick: () => null onClick: () => null
} }
] ]}
}); close={() => setShowConfirmAlert(false)}
}; />
);
isDeletable = () => {
return this.props.repository._links.delete;
};
render() {
const { loading, error, confirmDialog, t } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deleteRepo;
if (!this.isDeletable()) {
return null;
} }
return ( return (
@@ -98,8 +100,7 @@ class DeleteRepo extends React.Component<Props> {
/> />
</> </>
); );
} };
}
const mapStateToProps = (state: any, ownProps: Props) => { const mapStateToProps = (state: any, ownProps: Props) => {
const { namespace, name } = ownProps.repository; const { namespace, name } = ownProps.repository;

View File

@@ -21,10 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
import React from "react"; import React, { FC, useState } from "react";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { Permission } from "@scm-manager/ui-types"; import { Permission } from "@scm-manager/ui-types";
import { confirmAlert } from "@scm-manager/ui-components"; import { ConfirmAlert } from "@scm-manager/ui-components";
type Props = WithTranslation & { type Props = WithTranslation & {
permission: Permission; permission: Permission;
@@ -35,45 +35,54 @@ type Props = WithTranslation & {
loading: boolean; loading: boolean;
}; };
class DeletePermissionButton extends React.Component<Props> { const DeletePermissionButton: FC<Props> = ({
static defaultProps = { confirmDialog = true,
confirmDialog: true permission,
namespace,
t,
deletePermission,
repoName
}) => {
const [showConfirmAlert, setShowConfirmAlert] = useState(false);
const deletePermissionCallback = () => {
deletePermission(permission, namespace, repoName);
}; };
deletePermission = () => { const confirmDelete = () => {
this.props.deletePermission(this.props.permission, this.props.namespace, this.props.repoName); setShowConfirmAlert(true);
}; };
confirmDelete = () => { const isDeletable = () => {
const { t } = this.props; return permission._links.delete;
confirmAlert({ };
title: t("permission.delete-permission-button.confirm-alert.title"),
message: t("permission.delete-permission-button.confirm-alert.message"), const action = confirmDialog ? confirmDelete : deletePermissionCallback;
buttons: [
if (!isDeletable()) {
return null;
}
if (showConfirmAlert) {
return (
<ConfirmAlert
title={t("permission.delete-permission-button.confirm-alert.title")}
message={t("permission.delete-permission-button.confirm-alert.message")}
buttons={[
{ {
className: "is-outlined", className: "is-outlined",
label: t("permission.delete-permission-button.confirm-alert.submit"), label: t("permission.delete-permission-button.confirm-alert.submit"),
onClick: () => this.deletePermission() onClick: () => deletePermissionCallback()
}, },
{ {
label: t("permission.delete-permission-button.confirm-alert.cancel"), label: t("permission.delete-permission-button.confirm-alert.cancel"),
onClick: () => null onClick: () => null
} }
] ]}
}); />
}; );
isDeletable = () => {
return this.props.permission._links.delete;
};
render() {
const { confirmDialog } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deletePermission;
if (!this.isDeletable()) {
return null;
} }
return ( return (
<a className="level-item" onClick={action}> <a className="level-item" onClick={action}>
<span className="icon is-small"> <span className="icon is-small">
@@ -81,7 +90,6 @@ class DeletePermissionButton extends React.Component<Props> {
</span> </span>
</a> </a>
); );
} };
}
export default withTranslation("repos")(DeletePermissionButton); export default withTranslation("repos")(DeletePermissionButton);

View File

@@ -21,14 +21,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
import React from "react"; import React, { FC, useState } from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { compose } from "redux"; import { compose } from "redux";
import { withRouter } from "react-router-dom"; import { withRouter } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { History } from "history"; import { History } from "history";
import { User } from "@scm-manager/ui-types"; import { User } from "@scm-manager/ui-types";
import { confirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components"; import { ConfirmAlert, DeleteButton, ErrorNotification, Level } from "@scm-manager/ui-components";
import { deleteUser, getDeleteUserFailure, isDeleteUserPending } from "../modules/users"; import { deleteUser, getDeleteUserFailure, isDeleteUserPending } from "../modules/users";
type Props = WithTranslation & { type Props = WithTranslation & {
@@ -42,48 +42,49 @@ type Props = WithTranslation & {
history: History; history: History;
}; };
class DeleteUser extends React.Component<Props> { const DeleteUser: FC<Props> = ({ confirmDialog = true, loading, error, t, history, user, deleteUser }) => {
static defaultProps = { const [showConfirmAlert, setShowConfirmAlert] = useState(false);
confirmDialog: true
const userDeleted = () => {
history.push("/users/");
}; };
userDeleted = () => { const deleteUserCallback = () => {
this.props.history.push("/users/"); deleteUser(user, userDeleted);
}; };
deleteUser = () => { const confirmDelete = () => {
this.props.deleteUser(this.props.user, this.userDeleted); setShowConfirmAlert(true);
}; };
confirmDelete = () => { const isDeletable = () => {
const { t } = this.props; return user._links.delete;
confirmAlert({ };
title: t("deleteUser.confirmAlert.title"),
message: t("deleteUser.confirmAlert.message"), const action = confirmDialog ? confirmDelete : deleteUserCallback;
buttons: [
if (!isDeletable()) {
return null;
}
if (showConfirmAlert) {
return (
<ConfirmAlert
title={t("deleteUser.confirmAlert.title")}
message={t("deleteUser.confirmAlert.message")}
buttons={[
{ {
className: "is-outlined", className: "is-outlined",
label: t("deleteUser.confirmAlert.submit"), label: t("deleteUser.confirmAlert.submit"),
onClick: () => this.deleteUser() onClick: () => deleteUserCallback()
}, },
{ {
label: t("deleteUser.confirmAlert.cancel"), label: t("deleteUser.confirmAlert.cancel"),
onClick: () => null onClick: () => null
} }
] ]}
}); />
}; );
isDeletable = () => {
return this.props.user._links.delete;
};
render() {
const { loading, error, confirmDialog, t } = this.props;
const action = confirmDialog ? this.confirmDelete : this.deleteUser;
if (!this.isDeletable()) {
return null;
} }
return ( return (
@@ -93,8 +94,7 @@ class DeleteUser extends React.Component<Props> {
<Level right={<DeleteButton label={t("deleteUser.button")} action={action} loading={loading} />} /> <Level right={<DeleteButton label={t("deleteUser.button")} action={action} loading={loading} />} />
</> </>
); );
} };
}
const mapStateToProps = (state: any, ownProps: Props) => { const mapStateToProps = (state: any, ownProps: Props) => {
const loading = isDeleteUserPending(state, ownProps.user.name); const loading = isDeleteUserPending(state, ownProps.user.name);