Files
openmaxio-object-browser/portal-ui/src/screens/Console/Tenants/TenantDetails/TenantYAML.tsx

182 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-05-10 15:27:52 -07:00
// This file is part of MinIO Kubernetes Cloud
// Copyright (c) 2021 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { useEffect, useState } from "react";
import { connect } from "react-redux";
import Grid from "@material-ui/core/Grid";
import { Button, LinearProgress } from "@material-ui/core";
import { createStyles, Theme, withStyles } from "@material-ui/core/styles";
import api from "../../../../common/api";
import {
setErrorSnackMessage,
setModalErrorSnackMessage,
} from "../../../../actions";
import {
fieldBasic,
modalBasic,
} from "../../Common/FormComponents/common/styleLibrary";
import ModalWrapper from "../../Common/ModalWrapper/ModalWrapper";
import CodeMirrorWrapper from "../../Common/FormComponents/CodeMirrorWrapper/CodeMirrorWrapper";
const styles = (theme: Theme) =>
createStyles({
jsonPolicyEditor: {
minHeight: 400,
width: "100%",
},
buttonContainer: {
textAlign: "right",
},
errorState: {
color: "#b53b4b",
fontSize: 14,
fontWeight: "bold",
},
2021-05-10 15:27:52 -07:00
...modalBasic,
...fieldBasic,
});
interface ITenantYAML {
yaml: string;
}
interface ITenantYAMLProps {
classes: any;
open: boolean;
closeModalAndRefresh: (refresh: boolean) => void;
tenant: string;
namespace: string;
setModalErrorSnackMessage: typeof setModalErrorSnackMessage;
}
const TenantYAML = ({
classes,
open,
closeModalAndRefresh,
tenant,
namespace,
setModalErrorSnackMessage,
2021-05-10 15:27:52 -07:00
}: ITenantYAMLProps) => {
const [addLoading, setAddLoading] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const [tenantYaml, setTenantYaml] = useState<string>("");
const [errorMessage, setErrorMessage] = useState<string>("");
2021-05-10 15:27:52 -07:00
const updateTenant = (event: React.FormEvent) => {
event.preventDefault();
if (addLoading) {
return;
}
setAddLoading(true);
setErrorMessage("");
2021-05-10 15:27:52 -07:00
api
.invoke("PUT", `/api/v1/namespaces/${namespace}/tenants/${tenant}/yaml`, {
yaml: tenantYaml,
})
.then((res) => {
setAddLoading(false);
closeModalAndRefresh(true);
setErrorMessage("");
2021-05-10 15:27:52 -07:00
})
.catch((err) => {
setAddLoading(false);
console.log(err);
setErrorMessage(err);
2021-05-10 15:27:52 -07:00
});
};
// check the permissions for creating bucket
useEffect(() => {
api
.invoke("GET", `/api/v1/namespaces/${namespace}/tenants/${tenant}/yaml`)
.then((res: ITenantYAML) => {
setLoading(false);
setTenantYaml(res.yaml);
})
.catch((err: any) => {
setLoading(false);
setModalErrorSnackMessage(err);
2021-05-10 15:27:52 -07:00
});
}, [tenant, namespace]);
useEffect(() => {}, []);
const validSave = tenantYaml.trim() !== "";
return (
<ModalWrapper
modalOpen={open}
onClose={() => {
closeModalAndRefresh(false);
}}
title={`YAML`}
>
{loading && <LinearProgress />}
{errorMessage !== "" && (
<div className={classes.errorState}>{errorMessage}</div>
)}
{!loading && (
<form
noValidate
autoComplete="off"
onSubmit={(e: React.FormEvent<HTMLFormElement>) => {
updateTenant(e);
}}
>
<Grid container>
<Grid item xs={12} className={classes.formScrollable}>
<Grid item xs={12}>
<br />
</Grid>
<CodeMirrorWrapper
label={`Tenant Specification`}
value={tenantYaml}
mode={"yaml"}
onBeforeChange={(editor, data, value) => {
setTenantYaml(value);
}}
/>
2021-05-10 15:27:52 -07:00
</Grid>
<Grid item xs={12} className={classes.buttonContainer}>
<Button
type="submit"
variant="contained"
color="primary"
disabled={addLoading || !validSave}
>
Save
</Button>
2021-05-10 15:27:52 -07:00
</Grid>
{addLoading && (
<Grid item xs={12}>
<LinearProgress />
</Grid>
)}
</Grid>
</form>
)}
2021-05-10 15:27:52 -07:00
</ModalWrapper>
);
};
const mapDispatchToProps = {
setModalErrorSnackMessage,
};
const connector = connect(null, mapDispatchToProps);
export default withStyles(styles)(connector(TenantYAML));