diff --git a/src/clients/ClientDetails.tsx b/src/clients/ClientDetails.tsx new file mode 100644 index 0000000000..36089e1144 --- /dev/null +++ b/src/clients/ClientDetails.tsx @@ -0,0 +1,199 @@ +import React, { useContext, useEffect, useState } from "react"; +import { + AlertVariant, + ButtonVariant, + DropdownItem, + PageSection, + Tab, + Tabs, + TabTitleText, +} from "@patternfly/react-core"; +import { useTranslation } from "react-i18next"; +import { Controller, useForm, useWatch } from "react-hook-form"; +import { useParams } from "react-router-dom"; + +import { ClientSettings } from "./ClientSettings"; +import { useAlerts } from "../components/alert/Alerts"; +import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; +import { useDownloadDialog } from "../components/download-dialog/DownloadDialog"; +import { ViewHeader } from "../components/view-header/ViewHeader"; +import { HttpClientContext } from "../context/http-service/HttpClientContext"; +import { RealmContext } from "../context/realm-context/RealmContext"; +import { Credentials } from "./credentials/Credentials"; +import { ClientRepresentation } from "../realm/models/Realm"; +import { + convertFormValuesToObject, + convertToFormValues, + exportClient, +} from "../util"; +import { + convertToMultiline, + toValue, +} from "../components/multi-line-input/MultiLineInput"; + +export const ClientDetails = () => { + const { t } = useTranslation("clients"); + const httpClient = useContext(HttpClientContext)!; + const { realm } = useContext(RealmContext); + const { addAlert } = useAlerts(); + + const form = useForm(); + const publicClient = useWatch({ + control: form.control, + name: "publicClient", + defaultValue: false, + }); + const { id } = useParams<{ id: string }>(); + + const [activeTab, setActiveTab] = useState(0); + const [name, setName] = useState(""); + const url = `/admin/realms/${realm}/clients/${id}`; + + const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ + titleKey: "clients:clientDeleteConfirmTitle", + messageKey: "clients:clientDeleteConfirm", + continueButtonLabel: "common:delete", + continueButtonVariant: ButtonVariant.danger, + onConfirm: () => { + try { + httpClient.doDelete(`/admin/realms/${realm}/clients/${id}`); + addAlert(t("clientDeletedSuccess"), AlertVariant.success); + } catch (error) { + addAlert(`${t("clientDeleteError")} ${error}`, AlertVariant.danger); + } + }, + }); + + const [toggleDownloadDialog, DownloadDialog] = useDownloadDialog({ + id, + protocol: form.getValues("protocol"), + }); + + const setupForm = (client: ClientRepresentation) => { + form.reset(client); + Object.entries(client).map((entry) => { + if (entry[0] === "redirectUris") { + form.setValue(entry[0], convertToMultiline(entry[1])); + } else if (entry[0] === "attributes") { + convertToFormValues(entry[1], "attributes", form.setValue); + } else { + form.setValue(entry[0], entry[1]); + } + }); + }; + + useEffect(() => { + (async () => { + const fetchedClient = await httpClient.doGet(url); + if (fetchedClient.data) { + setName(fetchedClient.data.clientId); + setupForm(fetchedClient.data); + } + })(); + }, []); + + const save = async () => { + if (await form.trigger()) { + const redirectUris = toValue(form.getValues()["redirectUris"]); + const attributes = form.getValues()["attributes"] + ? convertFormValuesToObject(form.getValues()["attributes"]) + : {}; + + try { + const client = { + ...form.getValues(), + redirectUris, + attributes, + }; + await httpClient.doPut(url, client); + setupForm(client as ClientRepresentation); + addAlert(t("clientSaveSuccess"), AlertVariant.success); + } catch (error) { + addAlert(`${t("clientSaveError")} '${error}'`, AlertVariant.danger); + } + } + }; + + return ( + <> + + + { + const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({ + titleKey: "clients:disableConfirmTitle", + messageKey: "clients:disableConfirm", + continueButtonLabel: "common:disable", + onConfirm: () => { + onChange(!value); + save(); + }, + }); + return ( + <> + + toggleDownloadDialog()} + > + {t("downloadAdapterConfig")} + , + exportClient(form.getValues())} + > + {t("common:export")} + , + toggleDeleteDialog()} + > + {t("common:delete")} + , + ]} + isEnabled={value} + onToggle={(value) => { + if (!value) { + toggleDisableDialog(); + } else { + onChange(value); + save(); + } + }} + /> + + ); + }} + /> + + setActiveTab(key as number)} + isBox + > + {t("settings")}} + > + + + {publicClient && ( + {t("credentials")}} + > + + + )} + + + + ); +}; diff --git a/src/clients/ClientSettings.tsx b/src/clients/ClientSettings.tsx index 3d02a16825..fa8faf6523 100644 --- a/src/clients/ClientSettings.tsx +++ b/src/clients/ClientSettings.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useEffect, useState } from "react"; +import React from "react"; import { useTranslation } from "react-i18next"; import { FormGroup, @@ -6,245 +6,119 @@ import { Form, Switch, TextArea, - PageSection, ActionGroup, Button, - AlertVariant, - ButtonVariant, - DropdownItem, } from "@patternfly/react-core"; -import { useParams } from "react-router-dom"; -import { Controller, useForm } from "react-hook-form"; +import { Controller, UseFormMethods } from "react-hook-form"; import { ScrollForm } from "../components/scroll-form/ScrollForm"; import { ClientDescription } from "./ClientDescription"; import { CapabilityConfig } from "./add/CapabilityConfig"; -import { RealmContext } from "../context/realm-context/RealmContext"; -import { HttpClientContext } from "../context/http-service/HttpClientContext"; -import { ClientRepresentation } from "../realm/models/Realm"; -import { - convertToMultiline, - MultiLineInput, - toValue, -} from "../components/multi-line-input/MultiLineInput"; -import { useAlerts } from "../components/alert/Alerts"; -import { ViewHeader } from "../components/view-header/ViewHeader"; -import { exportClient } from "../util"; -import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; -import { useDownloadDialog } from "../components/download-dialog/DownloadDialog"; +import { MultiLineInput } from "../components/multi-line-input/MultiLineInput"; import { FormAccess } from "../components/form-access/FormAccess"; -export const ClientSettings = () => { +type ClientSettingsProps = { + form: UseFormMethods; + save: () => void; +}; + +export const ClientSettings = ({ form, save }: ClientSettingsProps) => { const { t } = useTranslation("clients"); - const httpClient = useContext(HttpClientContext)!; - const { realm } = useContext(RealmContext); - const { addAlert } = useAlerts(); - - const { id } = useParams<{ id: string }>(); - const [name, setName] = useState(""); - const form = useForm(); - const url = `/admin/realms/${realm}/clients/${id}`; - - useEffect(() => { - (async () => { - const fetchedClient = await httpClient.doGet(url); - if (fetchedClient.data) { - setName(fetchedClient.data.clientId); - Object.entries(fetchedClient.data).map((entry) => { - if (entry[0] !== "redirectUris") { - form.setValue(entry[0], entry[1]); - } else if (entry[1] && entry[1].length > 0) { - form.setValue(entry[0], convertToMultiline(entry[1])); - } - }); - } - })(); - }, []); - - const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ - titleKey: "clients:clientDeleteConfirmTitle", - messageKey: "clients:clientDeleteConfirm", - continueButtonLabel: "common:delete", - continueButtonVariant: ButtonVariant.danger, - onConfirm: () => { - try { - httpClient.doDelete(`/admin/realms/${realm}/clients/${id}`); - addAlert(t("clientDeletedSuccess"), AlertVariant.success); - } catch (error) { - addAlert(`${t("clientDeleteError")} ${error}`, AlertVariant.danger); - } - }, - }); - - const [toggleDownloadDialog, DownloadDialog] = useDownloadDialog({ - id, - protocol: form.getValues("protocol"), - }); - - const save = async () => { - if (await form.trigger()) { - const redirectUris = toValue(form.getValues()["redirectUris"]); - try { - httpClient.doPut(url, { ...form.getValues(), redirectUris }); - addAlert(t("clientSaveSuccess"), AlertVariant.success); - } catch (error) { - addAlert(`${t("clientSaveError")} '${error}'`, AlertVariant.danger); - } - } - }; return ( <> - - - { - const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({ - titleKey: "clients:disableConfirmTitle", - messageKey: "clients:disableConfirm", - continueButtonLabel: "common:disable", - onConfirm: () => { - onChange(!value); - save(); - }, - }); - return ( - <> - - toggleDownloadDialog()} - > - {t("downloadAdapterConfig")} - , - exportClient(form.getValues())} - > - {t("common:export")} - , - toggleDeleteDialog()} - > - {t("common:delete")} - , - ]} - isEnabled={value} - onToggle={(value) => { - if (!value) { - toggleDisableDialog(); - } else { - onChange(value); - save(); - } - }} - /> - - ); - }} - /> - - - -
- - - - - - - - - - - - - - - - ( - - )} - /> - - - ( - - )} - /> - - -