handling error and loading state

This commit is contained in:
Maren Süwer
2018-08-28 14:29:45 +02:00
parent 6fdc23996c
commit 2c23900061
3 changed files with 75 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
//@flow //@flow
import React from "react"; import React from "react";
import classNames from "classnames";
export type SelectItem = { export type SelectItem = {
value: string, value: string,
@@ -10,7 +11,8 @@ type Props = {
label?: string, label?: string,
options: SelectItem[], options: SelectItem[],
value?: SelectItem, value?: SelectItem,
onChange: string => void onChange: string => void,
loading?: boolean
}; };
class Select extends React.Component<Props> { class Select extends React.Component<Props> {
@@ -37,12 +39,15 @@ class Select extends React.Component<Props> {
}; };
render() { render() {
const { options, value } = this.props; const { options, value, loading } = this.props;
const loadingClass = loading ? "is-loading" : "";
return ( return (
<div className="field"> <div className="field">
{this.renderLabel()} {this.renderLabel()}
<div className="control select"> <div className={classNames(
"control select",
loadingClass
)}>
<select <select
ref={input => { ref={input => {
this.field = input; this.field = input;

View File

@@ -5,10 +5,16 @@ import { Checkbox } from "../../components/forms/index";
import { DeleteButton } from "../../components/buttons/index"; import { DeleteButton } from "../../components/buttons/index";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import { Select } from "../../components/forms/index"; import { Select } from "../../components/forms/index";
import { modifyPermission } from "../modules/permissions"; import {
modifyPermission,
isModifyPermissionPending,
getModifyPermissionFailure,
modifyPermissionReset
} from "../modules/permissions";
import connect from "react-redux/es/connect/connect"; import connect from "react-redux/es/connect/connect";
import { withRouter } from "react-router-dom"; import { withRouter } from "react-router-dom";
import type { History } from "history"; import type { History } from "history";
import ErrorNotification from "../../components/ErrorNotification";
type Props = { type Props = {
submitForm: Permission => void, submitForm: Permission => void,
@@ -18,7 +24,10 @@ type Props = {
namespace: string, namespace: string,
name: string, name: string,
match: any, match: any,
history: History history: History,
loading: boolean,
error: Error,
permissionReset: (string, string, string) => void
}; };
type State = { type State = {
@@ -41,6 +50,11 @@ class SinglePermission extends React.Component<Props, State> {
componentDidMount() { componentDidMount() {
const { permission } = this.props; const { permission } = this.props;
this.props.permissionReset(
this.props.namespace,
this.props.name,
permission.name
);
if (permission) { if (permission) {
this.setState({ this.setState({
permission: { permission: {
@@ -55,9 +69,8 @@ class SinglePermission extends React.Component<Props, State> {
render() { render() {
const { permission } = this.state; const { permission } = this.state;
const { t } = this.props; const { t, loading, error } = this.props;
const types = ["READ", "OWNER", "GROUP"]; const types = ["READ", "OWNER", "GROUP"];
const deleteButton = this.props.permission._links.delete ? ( const deleteButton = this.props.permission._links.delete ? (
<DeleteButton label={t("edit-permission.delete-button")} /> <DeleteButton label={t("edit-permission.delete-button")} />
) : null; ) : null;
@@ -68,12 +81,17 @@ class SinglePermission extends React.Component<Props, State> {
onChange={this.handleTypeChange} onChange={this.handleTypeChange}
value={permission.type ? permission.type : ""} value={permission.type ? permission.type : ""}
options={this.createSelectOptions(types)} options={this.createSelectOptions(types)}
loading={loading}
/> />
</td> </td>
) : ( ) : (
<td>{permission.type}</td> <td>{permission.type}</td>
); );
const errorNotification = error ? (
<ErrorNotification error={error} />
) : null;
return ( return (
<tr> <tr>
<td>{permission.name}</td> <td>{permission.name}</td>
@@ -81,7 +99,7 @@ class SinglePermission extends React.Component<Props, State> {
<Checkbox checked={permission ? permission.groupPermission : false} /> <Checkbox checked={permission ? permission.groupPermission : false} />
</td> </td>
{typeSelector} {typeSelector}
<td>{deleteButton}</td> <td>{deleteButton}</td> {errorNotification}
</tr> </tr>
); );
} }
@@ -116,7 +134,23 @@ class SinglePermission extends React.Component<Props, State> {
} }
} }
const mapStateToProps = (state, ownProps) => {}; const mapStateToProps = (state, ownProps) => {
const permission = ownProps.permission;
const loading = isModifyPermissionPending(
state,
ownProps.namespace,
ownProps.name,
permission.name
);
const error = getModifyPermissionFailure(
state,
ownProps.namespace,
ownProps.name,
permission.name
);
return { loading, error };
};
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
@@ -126,6 +160,13 @@ const mapDispatchToProps = dispatch => {
name: string name: string
) => { ) => {
dispatch(modifyPermission(permission, namespace, name)); dispatch(modifyPermission(permission, namespace, name));
},
permissionReset: (
namespace: string,
name: string,
permissionname: string
) => {
dispatch(modifyPermissionReset(namespace, name, permissionname));
} }
}; };
}; };

View File

@@ -26,6 +26,9 @@ export const MODIFY_PERMISSION_SUCCESS = `${MODIFY_PERMISSION}_${
export const MODIFY_PERMISSION_FAILURE = `${MODIFY_PERMISSION}_${ export const MODIFY_PERMISSION_FAILURE = `${MODIFY_PERMISSION}_${
types.FAILURE_SUFFIX types.FAILURE_SUFFIX
}`; }`;
export const MODIFY_PERMISSION_RESET = `${MODIFY_PERMISSION}_${
types.RESET_SUFFIX
}`;
const REPOS_URL = "repositories"; const REPOS_URL = "repositories";
const PERMISSIONS_URL = "permissions"; const PERMISSIONS_URL = "permissions";
const CONTENT_TYPE = "application/vnd.scmm-permission+json"; const CONTENT_TYPE = "application/vnd.scmm-permission+json";
@@ -98,15 +101,11 @@ export function modifyPermission(
callback?: () => void callback?: () => void
) { ) {
return function(dispatch: any) { return function(dispatch: any) {
dispatch( dispatch(modifyPermissionPending(permission, namespace, name));
modifyPermissionPending(permission, namespace, name)
);
return apiClient return apiClient
.put(permission._links.update.href, permission, CONTENT_TYPE) .put(permission._links.update.href, permission, CONTENT_TYPE)
.then(() => { .then(() => {
dispatch( dispatch(modifyPermissionSuccess(permission, namespace, name));
modifyPermissionSuccess(permission, namespace, name)
);
if (callback) { if (callback) {
callback(); callback();
} }
@@ -142,7 +141,7 @@ export function modifyPermissionSuccess(
payload: { payload: {
permission permission
}, },
itemId: namespace + "/" + name itemId: namespace + "/" + name + "/" + permission.name
}; };
} }
@@ -163,22 +162,27 @@ function newPermissions(
oldPermissions: PermissionCollection, oldPermissions: PermissionCollection,
newPermission: Permission newPermission: Permission
) { ) {
console.log("oldPermissions:");
console.log(oldPermissions);
console.log("new Permission:");
console.log(newPermission);
for (let i = 0; i < oldPermissions.length; i++) { for (let i = 0; i < oldPermissions.length; i++) {
console.log("permissionname:");
console.log("an der stelle i: " + oldPermissions[i].name);
if (oldPermissions[i].name === newPermission.name) { if (oldPermissions[i].name === newPermission.name) {
oldPermissions.splice(i, 1, newPermission); oldPermissions.splice(i, 1, newPermission);
console.log("new Permissions");
console.log(oldPermissions);
return oldPermissions; return oldPermissions;
} }
} }
} }
// reset
export function modifyPermissionReset(
namespace: string,
name: string,
permissionname: string
) {
return {
type: MODIFY_PERMISSION_RESET,
itemId: namespace + "/" + name + "/" + permissionname
};
}
// reducer // reducer
export default function reducer( export default function reducer(
state: Object = {}, state: Object = {},