2020-12-07 18:37:36 +00:00
|
|
|
import { IFormatter, IFormatterValueType } from "@patternfly/react-table";
|
2020-10-02 13:56:46 +00:00
|
|
|
import FileSaver from "file-saver";
|
2020-12-07 19:23:18 +00:00
|
|
|
import _ from "lodash";
|
2020-11-12 12:55:52 +00:00
|
|
|
import ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
|
|
|
import { ProviderRepresentation } from "keycloak-admin/lib/defs/serverInfoRepesentation";
|
2021-01-13 19:59:45 +00:00
|
|
|
import KeycloakAdminClient from "keycloak-admin";
|
2020-08-31 18:26:25 +00:00
|
|
|
|
2020-11-02 20:15:09 +00:00
|
|
|
export const sortProviders = (providers: {
|
|
|
|
[index: string]: ProviderRepresentation;
|
|
|
|
}) => {
|
|
|
|
return [...new Map(Object.entries(providers).sort(sortProvider)).keys()];
|
|
|
|
};
|
|
|
|
|
|
|
|
const sortProvider = (
|
2020-09-01 14:51:59 +00:00
|
|
|
a: [string, ProviderRepresentation],
|
|
|
|
b: [string, ProviderRepresentation]
|
|
|
|
) => {
|
2020-08-31 18:26:25 +00:00
|
|
|
let s1, s2;
|
2020-10-27 11:25:54 +00:00
|
|
|
if (a[1].order !== b[1].order) {
|
2020-08-31 18:26:25 +00:00
|
|
|
s1 = b[1].order;
|
|
|
|
s2 = a[1].order;
|
2020-09-01 14:51:59 +00:00
|
|
|
} else {
|
2020-08-31 18:26:25 +00:00
|
|
|
s1 = a[0];
|
|
|
|
s2 = b[0];
|
2020-09-01 14:51:59 +00:00
|
|
|
}
|
2020-08-31 18:26:25 +00:00
|
|
|
if (s1 < s2) {
|
|
|
|
return -1;
|
|
|
|
} else if (s1 > s2) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2020-10-02 13:56:46 +00:00
|
|
|
|
|
|
|
export const exportClient = (client: ClientRepresentation): void => {
|
2020-12-07 19:23:18 +00:00
|
|
|
const clientCopy = _.cloneDeep(client);
|
2020-10-02 13:56:46 +00:00
|
|
|
delete clientCopy.id;
|
|
|
|
|
|
|
|
if (clientCopy.protocolMappers) {
|
|
|
|
for (let i = 0; i < clientCopy.protocolMappers.length; i++) {
|
|
|
|
delete clientCopy.protocolMappers[i].id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSaver.saveAs(
|
|
|
|
new Blob([JSON.stringify(clientCopy, null, 2)], {
|
|
|
|
type: "application/json",
|
|
|
|
}),
|
|
|
|
clientCopy.clientId + ".json"
|
|
|
|
);
|
|
|
|
};
|
2020-10-20 20:47:23 +00:00
|
|
|
|
2021-01-15 01:44:16 +00:00
|
|
|
export const toUpperCase = (name: string) =>
|
|
|
|
name.charAt(0).toUpperCase() + name.slice(1);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2020-10-20 20:47:23 +00:00
|
|
|
export const convertToFormValues = (
|
|
|
|
obj: any,
|
|
|
|
prefix: string,
|
|
|
|
setValue: (name: string, value: any) => void
|
|
|
|
) => {
|
|
|
|
return Object.keys(obj).map((key) => {
|
2021-02-28 20:02:31 +00:00
|
|
|
const newKey = key.replace(/\./g, "-");
|
2020-10-20 20:47:23 +00:00
|
|
|
setValue(prefix + "." + newKey, obj[key]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const convertFormValuesToObject = (obj: any) => {
|
|
|
|
const keyValues = Object.keys(obj).map((key) => {
|
2021-02-28 20:02:31 +00:00
|
|
|
const newKey = key.replace(/-/g, ".");
|
2020-10-20 20:47:23 +00:00
|
|
|
return { [newKey]: obj[key] };
|
|
|
|
});
|
|
|
|
return Object.assign({}, ...keyValues);
|
|
|
|
};
|
2020-12-07 18:37:36 +00:00
|
|
|
|
|
|
|
export const emptyFormatter = (): IFormatter => (
|
|
|
|
data?: IFormatterValueType
|
|
|
|
) => {
|
|
|
|
return data ? data : "—";
|
|
|
|
};
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2021-01-20 21:10:25 +00:00
|
|
|
export const boolFormatter = (): IFormatter => (data?: IFormatterValueType) => {
|
|
|
|
const boolVal = data?.toString();
|
|
|
|
|
|
|
|
return (boolVal
|
|
|
|
? boolVal.charAt(0).toUpperCase() + boolVal.slice(1)
|
|
|
|
: undefined) as string;
|
|
|
|
};
|
|
|
|
|
2021-01-13 19:59:45 +00:00
|
|
|
export const getBaseUrl = (adminClient: KeycloakAdminClient) => {
|
|
|
|
return adminClient.keycloak
|
|
|
|
? adminClient.keycloak.authServerUrl!
|
|
|
|
: adminClient.baseUrl + "/";
|
|
|
|
};
|