2021-04-30 19:08:12 +00:00
|
|
|
import React, { useState } from "react";
|
2021-04-29 17:39:19 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-04-30 19:08:12 +00:00
|
|
|
import { Controller, useFormContext, useWatch } from "react-hook-form";
|
2021-04-29 17:39:19 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
Button,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Switch,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
2021-05-04 17:58:18 +00:00
|
|
|
import type RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation";
|
2021-04-29 17:39:19 +00:00
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
|
|
|
|
2021-04-30 19:08:12 +00:00
|
|
|
type RealmSettingsThemesTabProps = {
|
|
|
|
save: (realm: RealmRepresentation) => void;
|
|
|
|
reset: () => void;
|
2021-06-16 11:38:36 +00:00
|
|
|
realm: RealmRepresentation;
|
2021-04-30 19:08:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const RealmSettingsThemesTab = ({
|
|
|
|
save,
|
|
|
|
reset,
|
2021-06-16 11:38:36 +00:00
|
|
|
realm,
|
2021-04-30 19:08:12 +00:00
|
|
|
}: RealmSettingsThemesTabProps) => {
|
2021-04-29 17:39:19 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
|
|
|
|
const [loginThemeOpen, setLoginThemeOpen] = useState(false);
|
|
|
|
const [accountThemeOpen, setAccountThemeOpen] = useState(false);
|
|
|
|
const [adminConsoleThemeOpen, setAdminConsoleThemeOpen] = useState(false);
|
|
|
|
const [emailThemeOpen, setEmailThemeOpen] = useState(false);
|
|
|
|
|
|
|
|
const [supportedLocalesOpen, setSupportedLocalesOpen] = useState(false);
|
|
|
|
const [defaultLocaleOpen, setDefaultLocaleOpen] = useState(false);
|
|
|
|
|
2021-04-30 19:08:12 +00:00
|
|
|
const { control, handleSubmit } = useFormContext();
|
2021-04-29 17:39:19 +00:00
|
|
|
|
|
|
|
const themeTypes = useServerInfo().themes!;
|
|
|
|
|
2021-04-30 19:08:12 +00:00
|
|
|
const watchSupportedLocales = useWatch({
|
|
|
|
control,
|
|
|
|
name: "supportedLocales",
|
|
|
|
defaultValue: themeTypes?.account![0].locales,
|
|
|
|
});
|
2021-04-29 17:39:19 +00:00
|
|
|
|
2021-04-30 19:08:12 +00:00
|
|
|
const internationalizationEnabled = useWatch({
|
|
|
|
control,
|
|
|
|
name: "internationalizationEnabled",
|
2021-06-16 11:38:36 +00:00
|
|
|
defaultValue: realm?.internationalizationEnabled,
|
2021-04-30 19:08:12 +00:00
|
|
|
});
|
2021-04-29 17:39:19 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
onSubmit={handleSubmit(save)}
|
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("loginTheme")}
|
|
|
|
fieldId="kc-login-theme"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:loginTheme"
|
|
|
|
forLabel={t("loginTheme")}
|
|
|
|
forID="kc-login-theme"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="loginTheme"
|
|
|
|
control={control}
|
2021-04-30 19:08:12 +00:00
|
|
|
defaultValue=""
|
2021-04-29 17:39:19 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-login-theme"
|
|
|
|
onToggle={() => setLoginThemeOpen(!loginThemeOpen)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setLoginThemeOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("loginTheme")}
|
|
|
|
isOpen={loginThemeOpen}
|
|
|
|
placeholderText="Select a theme"
|
|
|
|
data-testid="select-login-theme"
|
|
|
|
>
|
|
|
|
{themeTypes.login.map((theme, idx) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={theme.name === value}
|
|
|
|
key={`login-theme-${idx}`}
|
|
|
|
value={theme.name}
|
|
|
|
>
|
|
|
|
{t(`${theme.name}`)}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("accountTheme")}
|
|
|
|
fieldId="kc-account-theme"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:accountTheme"
|
|
|
|
forLabel={t("accountTheme")}
|
|
|
|
forID="kc-account-theme"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="accountTheme"
|
|
|
|
control={control}
|
2021-04-30 19:08:12 +00:00
|
|
|
defaultValue=""
|
2021-04-29 17:39:19 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-account-theme"
|
|
|
|
onToggle={() => setAccountThemeOpen(!accountThemeOpen)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setAccountThemeOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("accountTheme")}
|
|
|
|
isOpen={accountThemeOpen}
|
|
|
|
placeholderText="Select a theme"
|
|
|
|
data-testid="select-account-theme"
|
|
|
|
>
|
|
|
|
{themeTypes.account.map((theme, idx) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={theme.name === value}
|
|
|
|
key={`account-theme-${idx}`}
|
|
|
|
value={theme.name}
|
|
|
|
>
|
|
|
|
{t(`${theme.name}`)}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("adminTheme")}
|
|
|
|
fieldId="kc-admin-console-theme"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:adminConsoleTheme"
|
|
|
|
forLabel={t("adminTheme")}
|
|
|
|
forID="kc-admin-console-theme"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="adminTheme"
|
|
|
|
control={control}
|
2021-04-30 19:08:12 +00:00
|
|
|
defaultValue=""
|
2021-04-29 17:39:19 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-admin-console-theme"
|
|
|
|
onToggle={() =>
|
|
|
|
setAdminConsoleThemeOpen(!adminConsoleThemeOpen)
|
|
|
|
}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setAdminConsoleThemeOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("adminConsoleTheme")}
|
|
|
|
isOpen={adminConsoleThemeOpen}
|
|
|
|
placeholderText="Select a theme"
|
|
|
|
data-testid="select-admin-theme"
|
|
|
|
>
|
|
|
|
{themeTypes.admin.map((theme, idx) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={theme.name === value}
|
|
|
|
key={`admin-theme-${idx}`}
|
|
|
|
value={theme.name}
|
|
|
|
>
|
|
|
|
{t(`${theme.name}`)}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("emailTheme")}
|
|
|
|
fieldId="kc-email-theme"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="realm-settings-help:emailTheme"
|
|
|
|
forLabel={t("emailTheme")}
|
|
|
|
forID="kc-email-theme"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="emailTheme"
|
|
|
|
control={control}
|
2021-04-30 19:08:12 +00:00
|
|
|
defaultValue=""
|
2021-04-29 17:39:19 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-email-theme"
|
|
|
|
onToggle={() => setEmailThemeOpen(!emailThemeOpen)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setEmailThemeOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("emailTheme")}
|
|
|
|
isOpen={emailThemeOpen}
|
|
|
|
placeholderText="Select a theme"
|
|
|
|
data-testid="select-email-theme"
|
|
|
|
>
|
|
|
|
{themeTypes.email.map((theme, idx) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={theme.name === value}
|
|
|
|
key={`email-theme-${idx}`}
|
|
|
|
value={theme.name}
|
|
|
|
>
|
|
|
|
{t(`${theme.name}`)}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("internationalization")}
|
|
|
|
fieldId="kc-internationalization"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="internationalizationEnabled"
|
|
|
|
control={control}
|
2021-06-23 11:36:10 +00:00
|
|
|
defaultValue={false}
|
2021-04-29 17:39:19 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
2021-06-16 11:38:36 +00:00
|
|
|
id="kc-t-internationalization"
|
2021-04-29 17:39:19 +00:00
|
|
|
label={t("common:enabled")}
|
|
|
|
labelOff={t("common:disabled")}
|
|
|
|
isChecked={value}
|
|
|
|
data-testid={
|
|
|
|
value
|
|
|
|
? "internationalization-enabled"
|
|
|
|
: "internationalization-disabled"
|
|
|
|
}
|
2021-04-30 19:08:12 +00:00
|
|
|
onChange={onChange}
|
2021-04-29 17:39:19 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
{internationalizationEnabled && (
|
|
|
|
<>
|
|
|
|
<FormGroup
|
|
|
|
label={t("supportedLocales")}
|
2021-06-16 11:38:36 +00:00
|
|
|
fieldId="kc-t-supported-locales"
|
2021-04-29 17:39:19 +00:00
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="supportedLocales"
|
|
|
|
control={control}
|
2021-04-30 19:08:12 +00:00
|
|
|
defaultValue={themeTypes?.account![0].locales}
|
2021-04-29 17:39:19 +00:00
|
|
|
render={({ value, onChange }) => (
|
|
|
|
<Select
|
2021-06-16 11:38:36 +00:00
|
|
|
toggleId="kc-t-supported-locales"
|
2021-06-01 12:14:56 +00:00
|
|
|
onToggle={() => {
|
|
|
|
setSupportedLocalesOpen(!supportedLocalesOpen);
|
|
|
|
}}
|
2021-04-29 17:39:19 +00:00
|
|
|
onSelect={(_, v) => {
|
|
|
|
const option = v as string;
|
|
|
|
if (!value) {
|
|
|
|
onChange([option]);
|
|
|
|
} else if (value!.includes(option)) {
|
|
|
|
onChange(
|
|
|
|
value.filter((item: string) => item !== option)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
onChange([...value, option]);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onClear={() => {
|
2021-04-30 19:08:12 +00:00
|
|
|
onChange([]);
|
2021-04-29 17:39:19 +00:00
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.typeaheadMulti}
|
|
|
|
aria-label={t("supportedLocales")}
|
|
|
|
isOpen={supportedLocalesOpen}
|
|
|
|
placeholderText={"Select locales"}
|
|
|
|
>
|
|
|
|
{themeTypes?.login![0].locales.map(
|
|
|
|
(locale: string, idx: number) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={true}
|
|
|
|
key={`locale-${idx}`}
|
|
|
|
value={locale}
|
|
|
|
>
|
|
|
|
{t(`allSupportedLocales.${locale}`)}
|
|
|
|
</SelectOption>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("defaultLocale")} fieldId="kc-default-locale">
|
|
|
|
<Controller
|
|
|
|
name="defaultLocale"
|
|
|
|
control={control}
|
2021-04-30 19:08:12 +00:00
|
|
|
defaultValue=""
|
2021-04-29 17:39:19 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
2021-06-16 11:38:36 +00:00
|
|
|
toggleId="kc-t-default-locale"
|
2021-04-29 17:39:19 +00:00
|
|
|
onToggle={() => setDefaultLocaleOpen(!defaultLocaleOpen)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setDefaultLocaleOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value && t(`allSupportedLocales.${value}`)}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("defaultLocale")}
|
|
|
|
isOpen={defaultLocaleOpen}
|
|
|
|
placeholderText="Select one"
|
|
|
|
data-testid="select-default-locale"
|
|
|
|
>
|
2021-04-30 19:08:12 +00:00
|
|
|
{watchSupportedLocales.map(
|
|
|
|
(locale: string, idx: number) => (
|
|
|
|
<SelectOption
|
|
|
|
key={`default-locale-${idx}`}
|
|
|
|
value={locale}
|
|
|
|
>
|
|
|
|
{t(`allSupportedLocales.${locale}`)}
|
|
|
|
</SelectOption>
|
|
|
|
)
|
|
|
|
)}
|
2021-04-29 17:39:19 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="themes-tab-save"
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
2021-04-30 19:08:12 +00:00
|
|
|
<Button variant="link" onClick={reset}>
|
2021-04-29 17:39:19 +00:00
|
|
|
{t("common:revert")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|