From c6a310a827e6472df058c1bc0a8d71a86d537f9b Mon Sep 17 00:00:00 2001 From: Eugenia <32821331+jenny-s51@users.noreply.github.com> Date: Fri, 4 Sep 2020 14:16:11 -0400 Subject: [PATCH] add realm selector (#48) * WIP realm selector * realm dropdown is working * address PR feedback * address PR feedback and fix conflicts * fix formatting * fix path * address PR feedback from Stan * update snapshot tests * fix formatting * change minwidth --- public/index.css | 4 + src/PageNav.tsx | 4 +- .../realm-selector/RealmSelector.test.tsx | 33 + .../realm-selector/RealmSelector.tsx | 51 +- .../__snapshots__/RealmSelector.test.tsx.snap | 587 ++++++++++++++++++ .../realm-selector/realm-selector.module.css | 15 + src/models/Realm.ts | 4 + 7 files changed, 684 insertions(+), 14 deletions(-) create mode 100644 src/components/realm-selector/RealmSelector.test.tsx create mode 100644 src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap create mode 100644 src/models/Realm.ts diff --git a/public/index.css b/public/index.css index a854244e74..e7b7625e4d 100644 --- a/public/index.css +++ b/public/index.css @@ -2,4 +2,8 @@ .pf-c-brand { height: 35px; +} + +button.pf-c-button.pf-m-primary.realmSelectButton { + width: 100%; } \ No newline at end of file diff --git a/src/PageNav.tsx b/src/PageNav.tsx index 92fc77a27c..dd95d9fb45 100644 --- a/src/PageNav.tsx +++ b/src/PageNav.tsx @@ -2,13 +2,13 @@ import React from "react"; import { Nav, NavItem, NavList, PageSidebar } from "@patternfly/react-core"; import { RealmSelector } from "./components/realm-selector/RealmSelector"; -export const PageNav = () => { +export const PageNav: React.FunctionComponent = () => { return ( - + Link 1 diff --git a/src/components/realm-selector/RealmSelector.test.tsx b/src/components/realm-selector/RealmSelector.test.tsx new file mode 100644 index 0000000000..f3f8256d61 --- /dev/null +++ b/src/components/realm-selector/RealmSelector.test.tsx @@ -0,0 +1,33 @@ +import React from "react"; +import { Button, AlertVariant } from "@patternfly/react-core"; +import { mount } from "enzyme"; +import EnzymeToJson from "enzyme-to-json"; +import { act } from "react-dom/test-utils"; + +import { RealmSelector } from "./RealmSelector"; + +const WithButton = () => { + const [add, alerts, hide] = useAlerts(); + return ( + <> + + + + ); +}; + +it("renders realm selector", () => { + const tree = mount(); + const button = tree.find("button"); + expect(button).not.toBeNull(); + + act(() => { + button!.simulate("click"); + }); + expect(EnzymeToJson(tree)).toMatchSnapshot(); + + act(() => { + jest.runAllTimers(); + }); + expect(EnzymeToJson(tree)).toMatchSnapshot(); +}); diff --git a/src/components/realm-selector/RealmSelector.tsx b/src/components/realm-selector/RealmSelector.tsx index eed8b12cfa..f7f76e7d41 100644 --- a/src/components/realm-selector/RealmSelector.tsx +++ b/src/components/realm-selector/RealmSelector.tsx @@ -1,27 +1,48 @@ -import React, { useState } from "react"; +import React, { useState, useContext, useEffect } from "react"; import { useHistory } from "react-router-dom"; +import { Realm } from "../../models/Realm"; + import { Dropdown, DropdownToggle, DropdownItem, Button, + Divider, } from "@patternfly/react-core"; import style from "./realm-selector.module.css"; +import { HttpClientContext } from "../../http-service/HttpClientContext"; -type RealmSelectorProps = { - realm: string; - realmList: string[]; -}; - -export const RealmSelector = ({ realm, realmList }: RealmSelectorProps) => { +export const RealmSelector = () => { const [open, setOpen] = useState(false); const history = useHistory(); - const dropdownItems = realmList.map((r) => ( - - {r} + 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) => ( + setCurrentRealm(r.realm)} + > + {r.realm.charAt(0).toUpperCase() + r.realm.slice(1)} )); + return ( { onToggle={() => setOpen(!open)} className={style.toggle} > - {realm} + {currentRealm} } dropdownItems={[ ...dropdownItems, + , - + , ]} /> diff --git a/src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap b/src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap new file mode 100644 index 0000000000..364c4c2284 --- /dev/null +++ b/src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap @@ -0,0 +1,587 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`renders realm selector 1`] = ` + + , + + + , + ] + } + id="realm-select" + isOpen={false} + toggle={ + + Master + + } + > + , + + + , + ] + } + id="realm-select" + isGrouped={false} + isOpen={false} + isPlain={false} + menuAppendTo="inline" + onSelect={[Function]} + position="left" + toggle={ + + Master + + } + > +
+ + + +
, + } + } + > + + + + , + } + } + > + + + + +
+
+
+`; + +exports[`renders realm selector 2`] = ` + + , + + + , + ] + } + id="realm-select" + isOpen={false} + toggle={ + + Master + + } + > + , + + + , + ] + } + id="realm-select" + isGrouped={false} + isOpen={false} + isPlain={false} + menuAppendTo="inline" + onSelect={[Function]} + position="left" + toggle={ + + Master + + } + > +
+ + + +
, + } + } + > + + + + , + } + } + > + + + + +
+
+
+`; diff --git a/src/components/realm-selector/realm-selector.module.css b/src/components/realm-selector/realm-selector.module.css index 6078a5b00d..8ed60c559e 100644 --- a/src/components/realm-selector/realm-selector.module.css +++ b/src/components/realm-selector/realm-selector.module.css @@ -1,11 +1,26 @@ .dropdown { width: 100%; + padding: 32px; border-bottom: 1px solid var(--pf-c-nav__link--before--BorderColor); + + outline: 1px solid var(--pf-global--Color--200); + outline-offset:-32px; +} + +.menu { + /* setting this to 85% for now */ + min-width: 85%; +} + +.pf-c-button.pf-m-primary.realmSelectButton { + width: 100%; } .toggle { color: var(--pf-c-nav__link--m-current--Color); + width: 100%; + background: var(--pf-global--BackgroundColor--dark-100); } .toggle:focus { diff --git a/src/models/Realm.ts b/src/models/Realm.ts new file mode 100644 index 0000000000..53f841b026 --- /dev/null +++ b/src/models/Realm.ts @@ -0,0 +1,4 @@ +export interface Realm { + id: string; + realm: string; +}