safe state in props and show loading and error state

This commit is contained in:
Maren Süwer
2018-08-23 11:00:14 +02:00
parent f81cc096aa
commit 33976605db
2 changed files with 51 additions and 7 deletions

View File

@@ -2,14 +2,30 @@
import React from "react";
import connect from "react-redux/es/connect/connect";
import { translate } from "react-i18next";
import { fetchPermissions } from "../modules/permissions";
import {
fetchPermissions,
getFetchPermissionsFailure,
isFetchPermissionsPending,
getPermissionsOfRepo
} from "../modules/permissions";
import type { History } from "history";
import Loading from "../../components/Loading";
import ErrorPage from "../../components/ErrorPage";
type Props = {
namespace: string,
name: string,
loading: boolean,
error: Error,
permissions: Permissions,
//dispatch functions
fetchPermissions: (namespace: string, name: string) => void
fetchPermissions: (namespace: string, name: string) => void,
// context props
t: string => string,
history: History,
match: any
};
class Permissions extends React.Component<Props> {
@@ -20,15 +36,38 @@ class Permissions extends React.Component<Props> {
}
render() {
const { namespace, name, loading, error, permissions, t } = this.props;
if (error) {
return (
<ErrorPage
title={t("permissions.error-title")}
subtitle={t("permissions.error-subtitle")}
error={error}
/>
);
}
if (loading || !permissions) {
return <Loading />;
}
return <div>Permissions will be shown here!</div>;
}
}
const mapStateToProps = (state, ownProps) => {
// const { namespace, name } = ownProps.match.params;
const namespace = ownProps.namespace;
const name = ownProps.name;
const error = getFetchPermissionsFailure(state);
const loading = isFetchPermissionsPending(state);
const permissions = getPermissionsOfRepo(state, namespace, name);
return {
//namespace,
//name
namespace,
name,
error,
loading,
permissions
};
};
@@ -43,4 +82,4 @@ const mapDispatchToProps = dispatch => {
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("repos")(Permissions));
)(translate("permissions")(Permissions));