import { useEffect, useState } from "react"; import { Button, Divider, Input, List, Radio, RadioGroup, Typography } from "@mui/joy"; import * as api from "../helpers/api"; import { generateDialog } from "./Dialog"; import Icon from "./Icon"; import toastHelper from "./Toast"; interface Props extends DialogProps { identityProvider?: IdentityProvider; confirmCallback?: () => void; } const CreateIdentityProviderDialog: React.FC = (props: Props) => { const { confirmCallback, destroy, identityProvider } = props; const [basicInfo, setBasicInfo] = useState({ name: "", identifierFilter: "", }); const [type, setType] = useState("OAUTH2"); const [oauth2Config, setOAuth2Config] = useState({ clientId: "", clientSecret: "", authUrl: "", tokenUrl: "", userInfoUrl: "", scopes: [], fieldMapping: { identifier: "", displayName: "", email: "", }, }); const [oauth2Scopes, setOAuth2Scopes] = useState(""); const isCreating = identityProvider === undefined; useEffect(() => { if (identityProvider) { setBasicInfo({ name: identityProvider.name, identifierFilter: identityProvider.identifierFilter, }); setType(identityProvider.type); if (identityProvider.type === "OAUTH2") { setOAuth2Config(identityProvider.config.oauth2Config); setOAuth2Scopes(identityProvider.config.oauth2Config.scopes.join(" ")); } } }, []); const handleCloseBtnClick = () => { destroy(); }; const allowConfirmAction = () => { if (basicInfo.name === "") { return false; } if (type === "OAUTH2") { if ( oauth2Config.clientId === "" || oauth2Config.clientSecret === "" || oauth2Config.authUrl === "" || oauth2Config.tokenUrl === "" || oauth2Config.userInfoUrl === "" || oauth2Scopes === "" || oauth2Config.fieldMapping.identifier === "" ) { return false; } } return true; }; const handleConfirmBtnClick = async () => { try { if (isCreating) { await api.createIdentityProvider({ ...basicInfo, type: type, config: { oauth2Config: { ...oauth2Config, scopes: oauth2Scopes.split(" "), }, }, }); } else { await api.patchIdentityProvider({ id: identityProvider?.id, type: type, ...basicInfo, config: { oauth2Config: { ...oauth2Config, scopes: oauth2Scopes.split(" "), }, }, }); } } catch (error: any) { console.error(error); toastHelper.error(error.response.data.message); } destroy(); if (confirmCallback) { confirmCallback(); } }; const setPartialOAuth2Config = (state: Partial) => { setOAuth2Config({ ...oauth2Config, ...state, }); }; return ( <>

{isCreating ? "Create SSO" : "Update SSO"}

Type Name* setBasicInfo({ ...basicInfo, name: e.target.value, }) } fullWidth /> Identifier filter setBasicInfo({ ...basicInfo, identifierFilter: e.target.value, }) } fullWidth /> {type === "OAUTH2" && ( <> Client ID* setPartialOAuth2Config({ clientId: e.target.value })} fullWidth /> Client secret* setPartialOAuth2Config({ clientSecret: e.target.value })} fullWidth /> Authorization endpoint* setPartialOAuth2Config({ authUrl: e.target.value })} fullWidth /> Token endpoint* setPartialOAuth2Config({ tokenUrl: e.target.value })} fullWidth /> User info endpoint* setPartialOAuth2Config({ userInfoUrl: e.target.value })} fullWidth /> Scopes* setOAuth2Scopes(e.target.value)} fullWidth /> Identifier* setPartialOAuth2Config({ fieldMapping: { ...oauth2Config.fieldMapping, identifier: e.target.value } })} fullWidth /> Display name setPartialOAuth2Config({ fieldMapping: { ...oauth2Config.fieldMapping, displayName: e.target.value } })} fullWidth /> Email setPartialOAuth2Config({ fieldMapping: { ...oauth2Config.fieldMapping, email: e.target.value } })} fullWidth /> )}
); }; function showCreateIdentityProviderDialog(identityProvider?: IdentityProvider, confirmCallback?: () => void) { generateDialog( { className: "create-identity-provider-dialog", dialogName: "create-identity-provider-dialog", }, CreateIdentityProviderDialog, { identityProvider, confirmCallback } ); } export default showCreateIdentityProviderDialog;