// This file is part of MinIO Console Server
// Copyright (c) 2022 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 {
BackLink,
Button,
FormLayout,
Grid,
InputBox,
PageLayout,
SectionTitle,
Switch,
} from "mds";
import { useNavigate } from "react-router-dom";
import { useAppDispatch } from "../../../store";
import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary";
import {
setErrorSnackMessage,
setHelpName,
setServerNeedsRestart,
} from "../../../systemSlice";
import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper";
import HelpMenu from "../HelpMenu";
import { api } from "api";
import { ApiError, HttpResponse, SetIDPResponse } from "api/consoleApi";
import { errorToHandler } from "api/errors";
type AddIDPConfigurationProps = {
classes?: any;
icon: React.ReactNode;
helpBox: React.ReactNode;
header: string;
title: string;
backLink: string;
formFields: object;
};
const AddIDPConfiguration = ({
icon,
helpBox,
header,
backLink,
title,
formFields,
}: AddIDPConfigurationProps) => {
const extraFormFields = {
name: {
required: true,
hasError: (s: string, editMode: boolean) => {
return !s && editMode ? "Config Name is required" : "";
},
label: "Name",
tooltip: "Name for identity provider configuration",
placeholder: "Name",
type: "text",
},
...formFields,
};
const navigate = useNavigate();
const dispatch = useAppDispatch();
const [fields, setFields] = useState({});
const [loadingCreate, setLoadingCreate] = useState(false);
const validSave = () => {
for (const [key, value] of Object.entries(extraFormFields)) {
if (
value.required &&
!(
fields[key] !== undefined &&
fields[key] !== null &&
fields[key] !== ""
)
) {
return false;
}
}
return true;
};
const resetForm = () => {
setFields({});
};
const addRecord = (event: React.FormEvent) => {
setLoadingCreate(true);
event.preventDefault();
const name = fields["name"];
let input = "";
for (const key of Object.keys(formFields)) {
if (fields[key]) {
input += `${key}=${fields[key]} `;
}
}
api.idp
.createConfiguration("openid", { name, input })
.then((res: HttpResponse) => {
navigate(backLink);
dispatch(setServerNeedsRestart(res.data.restart === true));
})
.catch(async (res: HttpResponse) => {
dispatch(setErrorSnackMessage(errorToHandler(res.error)));
})
.finally(() => setLoadingCreate(false));
};
const renderFormField = (key: string, value: any) => {
switch (value.type) {
case "toggle":
return (
setFields({ ...fields, [key]: e.target.checked ? "on" : "off" })
}
description=""
/>
);
default:
return (
) =>
setFields({ ...fields, [key]: e.target.value })
}
placeholder={value.placeholder}
type={value.type}
/>
);
}
};
useEffect(() => {
dispatch(setHelpName("add_idp_config"));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
navigate(backLink)} label={header} />}
actions={}
/>
{title}
);
};
export default AddIDPConfiguration;