2020-09-04 18:16:11 +00:00
|
|
|
import React, { useState, useContext, useEffect } from "react";
|
2020-09-01 14:51:59 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
2020-09-04 18:16:11 +00:00
|
|
|
import { Realm } from "../../models/Realm";
|
|
|
|
|
2020-08-04 12:59:41 +00:00
|
|
|
import {
|
|
|
|
Dropdown,
|
|
|
|
DropdownToggle,
|
|
|
|
DropdownItem,
|
|
|
|
Button,
|
2020-09-04 18:16:11 +00:00
|
|
|
Divider,
|
2020-09-01 14:51:59 +00:00
|
|
|
} from "@patternfly/react-core";
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2020-09-01 14:51:59 +00:00
|
|
|
import style from "./realm-selector.module.css";
|
2020-09-04 18:16:11 +00:00
|
|
|
import { HttpClientContext } from "../../http-service/HttpClientContext";
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2020-09-04 18:16:11 +00:00
|
|
|
export const RealmSelector = () => {
|
2020-08-04 12:59:41 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
2020-08-27 12:09:36 +00:00
|
|
|
const history = useHistory();
|
2020-09-04 18:16:11 +00:00
|
|
|
const httpClient = useContext(HttpClientContext);
|
|
|
|
const [realms, setRealms] = useState([] as Realm[]);
|
|
|
|
const [currentRealm, setCurrentRealm] = useState("Master");
|
|
|
|
|
|
|
|
const getRealms = async () => {
|
|
|
|
return await httpClient
|
|
|
|
?.doGet("/admin/realms")
|
|
|
|
.then((r) => r.data as Realm[]);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getRealms().then((result) => {
|
|
|
|
setRealms(result) !== undefined ? result : [];
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const dropdownItems = realms.map((r) => (
|
|
|
|
<DropdownItem
|
|
|
|
component="a"
|
|
|
|
href={"/#/realms/" + r.id}
|
|
|
|
key={r.id}
|
|
|
|
onClick={() => setCurrentRealm(r.realm)}
|
|
|
|
>
|
|
|
|
{r.realm.charAt(0).toUpperCase() + r.realm.slice(1)}
|
2020-08-27 12:09:36 +00:00
|
|
|
</DropdownItem>
|
2020-08-04 12:59:41 +00:00
|
|
|
));
|
2020-09-04 18:16:11 +00:00
|
|
|
|
2020-08-04 12:59:41 +00:00
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
id="realm-select"
|
|
|
|
className={style.dropdown}
|
|
|
|
isOpen={open}
|
|
|
|
toggle={
|
|
|
|
<DropdownToggle
|
|
|
|
id="realm-select-toggle"
|
|
|
|
onToggle={() => setOpen(!open)}
|
|
|
|
className={style.toggle}
|
|
|
|
>
|
2020-09-04 18:16:11 +00:00
|
|
|
{currentRealm}
|
2020-08-04 12:59:41 +00:00
|
|
|
</DropdownToggle>
|
|
|
|
}
|
|
|
|
dropdownItems={[
|
|
|
|
...dropdownItems,
|
2020-09-04 18:16:11 +00:00
|
|
|
<Divider key={1} />,
|
2020-08-27 12:09:36 +00:00
|
|
|
<DropdownItem component="div" key="add">
|
2020-09-05 19:37:42 +00:00
|
|
|
<Button isBlock onClick={() => history.push("/add-realm")}>
|
2020-09-04 18:16:11 +00:00
|
|
|
Create Realm
|
|
|
|
</Button>
|
2020-08-04 12:59:41 +00:00
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|