// This file is part of MinIO Console Server
// 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 .
import React, { useEffect, useState } from "react";
import { connect } from "react-redux";
import Grid from "@mui/material/Grid";
import { Button, LinearProgress } from "@mui/material";
import { Theme } from "@mui/material/styles";
import createStyles from "@mui/styles/createStyles";
import withStyles from "@mui/styles/withStyles";
import api from "../../../../common/api";
import { setModalErrorSnackMessage } from "../../../../actions";
import {
fieldBasic,
modalStyleUtils,
} from "../../Common/FormComponents/common/styleLibrary";
import { ErrorResponseHandler } from "../../../../common/types";
import ModalWrapper from "../../Common/ModalWrapper/ModalWrapper";
import CodeMirrorWrapper from "../../Common/FormComponents/CodeMirrorWrapper/CodeMirrorWrapper";
import { EditYamlIcon } from "../../../../icons";
const styles = (theme: Theme) =>
createStyles({
buttonContainer: {
textAlign: "right",
},
errorState: {
color: "#b53b4b",
fontSize: 14,
fontWeight: "bold",
},
codeMirrorContainer: {
marginBottom: 20,
paddingLeft: 15,
"& label": {
marginBottom: ".5rem",
},
"& label + div": {
display: "none",
},
},
...modalStyleUtils,
...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,
}: ITenantYAMLProps) => {
const [addLoading, setAddLoading] = useState(false);
const [loading, setLoading] = useState(false);
const [tenantYaml, setTenantYaml] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const updateTenant = (event: React.FormEvent) => {
event.preventDefault();
if (addLoading) {
return;
}
setAddLoading(true);
setErrorMessage("");
api
.invoke("PUT", `/api/v1/namespaces/${namespace}/tenants/${tenant}/yaml`, {
yaml: tenantYaml,
})
.then((res) => {
setAddLoading(false);
closeModalAndRefresh(true);
setErrorMessage("");
})
.catch((err: ErrorResponseHandler) => {
setAddLoading(false);
setErrorMessage(err.errorMessage);
});
};
// 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: ErrorResponseHandler) => {
setLoading(false);
setModalErrorSnackMessage(err);
});
}, [tenant, namespace, setModalErrorSnackMessage]);
useEffect(() => {}, []);
const validSave = tenantYaml.trim() !== "";
return (
{
closeModalAndRefresh(false);
}}
title={`YAML`}
titleIcon={}
>
{addLoading ||
(loading && (
))}
{errorMessage !== "" && (
{errorMessage}
)}
{!loading && (
)}
);
};
const mapDispatchToProps = {
setModalErrorSnackMessage,
};
const connector = connect(null, mapDispatchToProps);
export default withStyles(styles)(connector(TenantYAML));