2021-06-16 11:38:36 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-07-01 06:48:30 +00:00
|
|
|
import { Controller, useForm, useFormContext, useWatch } from "react-hook-form";
|
2021-06-16 11:38:36 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
2021-07-01 06:48:30 +00:00
|
|
|
AlertVariant,
|
2021-06-16 11:38:36 +00:00
|
|
|
Button,
|
2021-08-17 12:50:31 +00:00
|
|
|
Divider,
|
2021-06-16 11:38:36 +00:00
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Select,
|
2021-08-17 12:50:31 +00:00
|
|
|
SelectGroup,
|
2021-06-16 11:38:36 +00:00
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Switch,
|
|
|
|
TextContent,
|
2021-08-17 12:50:31 +00:00
|
|
|
ToolbarItem,
|
2021-06-16 11:38:36 +00:00
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
2021-08-26 08:39:35 +00:00
|
|
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
2021-06-16 11:38:36 +00:00
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
|
|
|
import { FormPanel } from "../components/scroll-form/FormPanel";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2021-07-01 06:48:30 +00:00
|
|
|
import { AddMessageBundleModal } from "./AddMessageBundleModal";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-07-01 16:44:36 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-08-19 13:56:00 +00:00
|
|
|
import { DEFAULT_LOCALE } from "../i18n";
|
2021-06-16 11:38:36 +00:00
|
|
|
|
|
|
|
type LocalizationTabProps = {
|
|
|
|
save: (realm: RealmRepresentation) => void;
|
|
|
|
reset: () => void;
|
|
|
|
refresh: () => void;
|
|
|
|
realm: RealmRepresentation;
|
|
|
|
};
|
|
|
|
|
2021-07-01 06:48:30 +00:00
|
|
|
export type KeyValueType = { key: string; value: string };
|
|
|
|
|
|
|
|
export type BundleForm = {
|
|
|
|
messageBundle: KeyValueType;
|
|
|
|
};
|
|
|
|
|
2021-06-16 11:38:36 +00:00
|
|
|
export const LocalizationTab = ({
|
|
|
|
save,
|
|
|
|
reset,
|
|
|
|
realm,
|
|
|
|
}: LocalizationTabProps) => {
|
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const adminClient = useAdminClient();
|
2021-07-05 11:24:10 +00:00
|
|
|
const [addMessageBundleModalOpen, setAddMessageBundleModalOpen] =
|
|
|
|
useState(false);
|
2021-06-16 11:38:36 +00:00
|
|
|
|
|
|
|
const [supportedLocalesOpen, setSupportedLocalesOpen] = useState(false);
|
|
|
|
const [defaultLocaleOpen, setDefaultLocaleOpen] = useState(false);
|
2021-08-17 12:50:31 +00:00
|
|
|
const [filterDropdownOpen, setFilterDropdownOpen] = useState(false);
|
2021-08-19 13:56:00 +00:00
|
|
|
const [selectMenuLocale, setSelectMenuLocale] = useState(DEFAULT_LOCALE);
|
2021-06-16 11:38:36 +00:00
|
|
|
|
2021-08-17 12:50:31 +00:00
|
|
|
const { getValues, control, handleSubmit, formState } = useFormContext();
|
2021-06-16 11:38:36 +00:00
|
|
|
const [valueSelected, setValueSelected] = useState(false);
|
2021-08-17 12:50:31 +00:00
|
|
|
const [selectMenuValueSelected, setSelectMenuValueSelected] = useState(false);
|
|
|
|
|
2021-06-16 11:38:36 +00:00
|
|
|
const themeTypes = useServerInfo().themes!;
|
2021-07-01 06:48:30 +00:00
|
|
|
const bundleForm = useForm<BundleForm>({ mode: "onChange" });
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-07-01 16:44:36 +00:00
|
|
|
const { realm: currentRealm } = useRealm();
|
2021-06-16 11:38:36 +00:00
|
|
|
|
2021-09-02 20:14:43 +00:00
|
|
|
const watchSupportedLocales = useWatch<string[]>({
|
2021-06-16 11:38:36 +00:00
|
|
|
control,
|
|
|
|
name: "supportedLocales",
|
|
|
|
});
|
|
|
|
|
|
|
|
const internationalizationEnabled = useWatch({
|
|
|
|
control,
|
|
|
|
name: "internationalizationEnabled",
|
2021-09-02 20:14:43 +00:00
|
|
|
defaultValue: false,
|
2021-06-16 11:38:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const loader = async () => {
|
2021-08-12 08:56:25 +00:00
|
|
|
try {
|
2021-06-28 06:02:35 +00:00
|
|
|
const result = await adminClient.realms.getRealmLocalizationTexts({
|
|
|
|
realm: realm.realm!,
|
2021-08-19 13:56:00 +00:00
|
|
|
selectedLocale:
|
|
|
|
selectMenuLocale || getValues("defaultLocale") || DEFAULT_LOCALE,
|
2021-08-17 12:50:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return Object.entries(result);
|
|
|
|
} catch (error) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const tableLoader = async () => {
|
|
|
|
try {
|
|
|
|
const result = await adminClient.realms.getRealmLocalizationTexts({
|
|
|
|
realm: currentRealm,
|
|
|
|
selectedLocale: selectMenuLocale,
|
2021-06-28 06:02:35 +00:00
|
|
|
});
|
2021-08-17 12:50:31 +00:00
|
|
|
|
|
|
|
return Object.entries(result);
|
2021-08-12 08:56:25 +00:00
|
|
|
} catch (error) {
|
2021-08-17 12:50:31 +00:00
|
|
|
return [];
|
2021-06-16 11:38:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-01 06:48:30 +00:00
|
|
|
const handleModalToggle = () => {
|
|
|
|
setAddMessageBundleModalOpen(!addMessageBundleModalOpen);
|
|
|
|
};
|
|
|
|
|
2021-08-17 12:50:31 +00:00
|
|
|
const options = [
|
|
|
|
<SelectGroup label={t("defaultLocale")} key="group1">
|
|
|
|
{watchSupportedLocales
|
2021-09-02 20:14:43 +00:00
|
|
|
?.filter((item) => item === realm?.defaultLocale)
|
2021-08-17 12:50:31 +00:00
|
|
|
.map((locale) => (
|
|
|
|
<SelectOption key={locale} value={locale}>
|
|
|
|
{t(`allSupportedLocales.${locale}`)}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</SelectGroup>,
|
|
|
|
<Divider key="divider" />,
|
|
|
|
<SelectGroup label={t("supportedLocales")} key="group2">
|
|
|
|
{watchSupportedLocales
|
2021-09-02 20:14:43 +00:00
|
|
|
?.filter((item) => item !== realm?.defaultLocale)
|
2021-08-17 12:50:31 +00:00
|
|
|
.map((locale) => (
|
|
|
|
<SelectOption key={locale} value={locale}>
|
|
|
|
{t(`allSupportedLocales.${locale}`)}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</SelectGroup>,
|
|
|
|
];
|
|
|
|
|
|
|
|
const [tableKey, setTableKey] = useState(0);
|
|
|
|
|
|
|
|
const refreshTable = () => {
|
|
|
|
setTableKey(new Date().getTime());
|
|
|
|
};
|
|
|
|
|
2021-07-01 06:48:30 +00:00
|
|
|
const addKeyValue = async (pair: KeyValueType): Promise<void> => {
|
|
|
|
try {
|
|
|
|
adminClient.setConfig({
|
|
|
|
requestConfig: { headers: { "Content-Type": "text/plain" } },
|
|
|
|
});
|
|
|
|
await adminClient.realms.addLocalization(
|
|
|
|
{
|
2021-07-01 16:44:36 +00:00
|
|
|
realm: currentRealm!,
|
2021-08-17 12:50:31 +00:00
|
|
|
selectedLocale:
|
2021-08-19 13:56:00 +00:00
|
|
|
selectMenuLocale || getValues("defaultLocale") || DEFAULT_LOCALE,
|
2021-07-01 06:48:30 +00:00
|
|
|
key: pair.key,
|
|
|
|
},
|
|
|
|
pair.value
|
|
|
|
);
|
|
|
|
|
|
|
|
adminClient.setConfig({
|
2021-07-01 16:44:36 +00:00
|
|
|
realmName: currentRealm!,
|
2021-07-01 06:48:30 +00:00
|
|
|
});
|
2021-08-17 12:50:31 +00:00
|
|
|
refreshTable();
|
2021-07-28 12:01:42 +00:00
|
|
|
addAlert(t("pairCreatedSuccess"), AlertVariant.success);
|
2021-07-01 06:48:30 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("realm-settings:pairCreatedError", error);
|
2021-07-01 06:48:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-16 11:38:36 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-07-01 06:48:30 +00:00
|
|
|
{addMessageBundleModalOpen && (
|
|
|
|
<AddMessageBundleModal
|
|
|
|
handleModalToggle={handleModalToggle}
|
|
|
|
save={(pair: any) => {
|
|
|
|
addKeyValue(pair);
|
|
|
|
handleModalToggle();
|
|
|
|
}}
|
|
|
|
form={bundleForm}
|
|
|
|
/>
|
|
|
|
)}
|
2021-06-16 11:38:36 +00:00
|
|
|
<PageSection variant="light">
|
|
|
|
<FormPanel
|
|
|
|
className="kc-login-screen"
|
|
|
|
title="Login screen customization"
|
|
|
|
>
|
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
onSubmit={handleSubmit(save)}
|
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("internationalization")}
|
|
|
|
fieldId="kc-internationalization"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="internationalizationEnabled"
|
|
|
|
control={control}
|
2021-06-23 11:36:10 +00:00
|
|
|
defaultValue={false}
|
2021-06-16 11:38:36 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id="kc-l-internationalization"
|
|
|
|
label={t("common:enabled")}
|
|
|
|
labelOff={t("common:disabled")}
|
|
|
|
isChecked={internationalizationEnabled}
|
|
|
|
data-testid={
|
|
|
|
value
|
|
|
|
? "internationalization-enabled"
|
|
|
|
: "internationalization-disabled"
|
|
|
|
}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
{internationalizationEnabled && (
|
|
|
|
<>
|
|
|
|
<FormGroup
|
|
|
|
label={t("supportedLocales")}
|
|
|
|
fieldId="kc-l-supported-locales"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="supportedLocales"
|
|
|
|
control={control}
|
|
|
|
render={({ onChange }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-l-supported-locales"
|
|
|
|
onToggle={() => {
|
|
|
|
setSupportedLocalesOpen(!supportedLocalesOpen);
|
|
|
|
}}
|
|
|
|
onSelect={(_, v) => {
|
|
|
|
const option = v as string;
|
|
|
|
if (!watchSupportedLocales) {
|
|
|
|
onChange([option]);
|
|
|
|
} else if (watchSupportedLocales!.includes(option)) {
|
|
|
|
onChange(
|
|
|
|
watchSupportedLocales.filter(
|
|
|
|
(item: string) => item !== option
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
onChange([...watchSupportedLocales, option]);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onClear={() => {
|
|
|
|
onChange([]);
|
|
|
|
}}
|
|
|
|
selections={watchSupportedLocales}
|
|
|
|
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-l-default-locale"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="defaultLocale"
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="kc-default-locale"
|
|
|
|
onToggle={() =>
|
|
|
|
setDefaultLocaleOpen(!defaultLocaleOpen)
|
|
|
|
}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setValueSelected(true);
|
|
|
|
setDefaultLocaleOpen(false);
|
|
|
|
}}
|
|
|
|
selections={
|
|
|
|
valueSelected
|
|
|
|
? t(`allSupportedLocales.${value}`)
|
|
|
|
: realm.defaultLocale !== ""
|
|
|
|
? t(
|
|
|
|
`allSupportedLocales.${
|
2021-08-19 13:56:00 +00:00
|
|
|
realm.defaultLocale || DEFAULT_LOCALE
|
2021-06-16 11:38:36 +00:00
|
|
|
}`
|
|
|
|
)
|
|
|
|
: t("placeholderText")
|
|
|
|
}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("defaultLocale")}
|
|
|
|
isOpen={defaultLocaleOpen}
|
|
|
|
placeholderText={t("placeholderText")}
|
|
|
|
data-testid="select-default-locale"
|
|
|
|
>
|
2021-09-02 20:14:43 +00:00
|
|
|
{watchSupportedLocales?.map(
|
2021-06-16 11:38:36 +00:00
|
|
|
(locale: string, idx: number) => (
|
|
|
|
<SelectOption
|
|
|
|
key={`default-locale-${idx}`}
|
|
|
|
value={locale}
|
|
|
|
>
|
|
|
|
{t(`allSupportedLocales.${locale}`)}
|
|
|
|
</SelectOption>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
2021-08-17 12:50:31 +00:00
|
|
|
isDisabled={!formState.isDirty}
|
2021-06-16 11:38:36 +00:00
|
|
|
type="submit"
|
|
|
|
data-testid="localization-tab-save"
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
<Button variant="link" onClick={reset}>
|
|
|
|
{t("common:revert")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
|
2021-06-10 20:00:14 +00:00
|
|
|
<FormPanel className="kc-message-bundles" title="Edit message bundles">
|
2021-06-16 11:38:36 +00:00
|
|
|
<TextContent className="messageBundleDescription">
|
|
|
|
{t("messageBundleDescription")}
|
|
|
|
</TextContent>
|
|
|
|
<div className="tableBorder">
|
|
|
|
<KeycloakDataTable
|
2021-08-17 12:50:31 +00:00
|
|
|
key={tableKey}
|
|
|
|
isSearching
|
|
|
|
loader={selectMenuValueSelected ? tableLoader : loader}
|
|
|
|
ariaLabelKey="realm-settings:localization"
|
|
|
|
searchTypeComponent={
|
|
|
|
<ToolbarItem>
|
|
|
|
<Select
|
|
|
|
width={180}
|
|
|
|
data-testid="filter-by-locale-select"
|
|
|
|
isOpen={filterDropdownOpen}
|
|
|
|
className="kc-filter-by-locale-select"
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
isDisabled={!formState.isSubmitSuccessful}
|
|
|
|
onToggle={(isExpanded) => setFilterDropdownOpen(isExpanded)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
setSelectMenuLocale(value.toString());
|
|
|
|
setSelectMenuValueSelected(true);
|
|
|
|
refreshTable();
|
|
|
|
setFilterDropdownOpen(false);
|
|
|
|
}}
|
|
|
|
selections={
|
|
|
|
selectMenuValueSelected
|
|
|
|
? t(`allSupportedLocales.${selectMenuLocale}`)
|
|
|
|
: realm.defaultLocale !== ""
|
2021-08-19 13:56:00 +00:00
|
|
|
? t(`allSupportedLocales.${DEFAULT_LOCALE}`)
|
2021-08-17 12:50:31 +00:00
|
|
|
: t("placeholderText")
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{options}
|
|
|
|
</Select>
|
|
|
|
</ToolbarItem>
|
|
|
|
}
|
2021-07-01 06:48:30 +00:00
|
|
|
toolbarItem={
|
|
|
|
<Button
|
|
|
|
data-testid="add-bundle-button"
|
2021-08-17 12:50:31 +00:00
|
|
|
isDisabled={!formState.isSubmitSuccessful}
|
2021-07-01 06:48:30 +00:00
|
|
|
onClick={() => setAddMessageBundleModalOpen(true)}
|
|
|
|
>
|
|
|
|
{t("addMessageBundle")}
|
|
|
|
</Button>
|
|
|
|
}
|
2021-06-16 11:38:36 +00:00
|
|
|
searchPlaceholderKey=" "
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noMessageBundles")}
|
|
|
|
instructions={t("noMessageBundlesInstructions")}
|
2021-07-01 06:48:30 +00:00
|
|
|
onPrimaryAction={handleModalToggle}
|
|
|
|
primaryActionText={t("addMessageBundle")}
|
2021-06-16 11:38:36 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
canSelectAll
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "Key",
|
|
|
|
cellRenderer: (row) => row[0],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Value",
|
|
|
|
cellRenderer: (row) => row[1],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</FormPanel>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|