-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: `newuserPrompt` -> `newUserPrompt` * feat: create initial `banner` component * chore: all `alert` with `banner` * chore: add `dialog-footer` and tests for button labels * chore: remove `alertContext` * chore: abstract the observer logic into its separate file * chore: add conditional pathname logic for banner displaying * chore: new banner tests * chore: one more test * chore: remove `pathname` for `useEffect` and rename private variable * fix: move banner and navigate under polling; improve logic * fix: move `banner` after poller
- Loading branch information
1 parent
54be6e2
commit 073cbd4
Showing
26 changed files
with
405 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Alert, AlertVariant } from "../Alert"; | ||
import { Check, X } from "lucide-react"; | ||
import { useLocation } from "react-router-dom"; | ||
import { Observer } from "@/utils/basic-observable"; | ||
|
||
export type Banner = { | ||
header: string; | ||
body: string; | ||
variant?: AlertVariant; | ||
pathnameToDisplayOn: string; | ||
}; | ||
|
||
class BannerObserver extends Observer<Banner> { | ||
create = (data: Banner) => { | ||
this.publish(data); | ||
this.observed = { ...data }; | ||
}; | ||
|
||
dismiss = () => { | ||
this.publish(null); | ||
this.observed = null; | ||
}; | ||
} | ||
|
||
const bannerState = new BannerObserver(); | ||
|
||
export const banner = (newBanner: Banner) => { | ||
return bannerState.create(newBanner); | ||
}; | ||
|
||
export const Banner = () => { | ||
const [activeBanner, setActiveBanner] = useState<Banner | null>(null); | ||
const { pathname } = useLocation(); | ||
|
||
const onClose = () => { | ||
bannerState.dismiss(); | ||
}; | ||
|
||
useEffect(() => { | ||
const unsubscribe = bannerState.subscribe((banner) => { | ||
if (banner) { | ||
setActiveBanner(banner); | ||
} else { | ||
setActiveBanner(null); | ||
} | ||
}); | ||
|
||
return unsubscribe; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (activeBanner && activeBanner.pathnameToDisplayOn !== pathname) { | ||
onClose(); | ||
} | ||
}, [pathname, activeBanner]); | ||
|
||
if (activeBanner && activeBanner.pathnameToDisplayOn === pathname) { | ||
return ( | ||
<Alert | ||
variant={activeBanner.variant} | ||
className="mt-4 mb-8 flex-row text-sm" | ||
> | ||
<div className="flex items-start justify-between"> | ||
<Check /> | ||
<div className="ml-2 w-full"> | ||
<h3 className="text-lg font-bold" data-testid="banner-header"> | ||
{activeBanner.header} | ||
</h3> | ||
<p data-testid="banner-body">{activeBanner.body}</p> | ||
</div> | ||
<button onClick={onClose} data-testid="banner-close"> | ||
<X size={20} /> | ||
</button> | ||
</div> | ||
</Alert> | ||
); | ||
} | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./banner"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { ReactNode } from "react"; | ||
import { Link, MemoryRouter, Route, Routes } from "react-router-dom"; | ||
import { describe, expect, test } from "vitest"; | ||
import { act, render } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { banner, Banner } from "../Banner"; | ||
|
||
const wrapper = ({ children }: { children: ReactNode }) => ( | ||
<MemoryRouter initialEntries={["/dashboard"]}> | ||
<Routes> | ||
<Route | ||
path="/dashboard" | ||
element={<Link to="/example" id="dashboard-link" />} | ||
/> | ||
<Route | ||
path="/example" | ||
element={<Link to="/dashboard" id="example-link" />} | ||
/> | ||
</Routes> | ||
{children} | ||
</MemoryRouter> | ||
); | ||
|
||
describe("banner", () => { | ||
test("Hidden on initial render", () => { | ||
const { queryByTestId } = render(<Banner />, { wrapper }); | ||
|
||
expect(queryByTestId("banner-header")).not.toBeInTheDocument(); | ||
}); | ||
|
||
test("Check if banner is not rendered on wrong pathnameToDisplayOn", () => { | ||
const { queryByTestId } = render(<Banner />, { wrapper }); | ||
|
||
act(() => { | ||
banner({ | ||
header: "Test header", | ||
body: "Test body", | ||
pathnameToDisplayOn: "/", | ||
}); | ||
}); | ||
|
||
expect(queryByTestId("banner-header")).not.toBeInTheDocument(); | ||
}); | ||
|
||
test("Check if banner is visible on correct pathnameToDisplayOn", () => { | ||
const { getByTestId } = render(<Banner />, { wrapper }); | ||
|
||
act(() => { | ||
banner({ | ||
header: "Test header", | ||
body: "Test body", | ||
pathnameToDisplayOn: "/dashboard", | ||
}); | ||
}); | ||
|
||
expect(getByTestId("banner-header")).toHaveTextContent("Test header"); | ||
}); | ||
|
||
test("Check if banner header and body text match", () => { | ||
const { getByText } = render(<Banner />, { wrapper }); | ||
|
||
act(() => { | ||
banner({ | ||
header: "Test header", | ||
body: "Test body", | ||
pathnameToDisplayOn: "/dashboard", | ||
}); | ||
}); | ||
|
||
expect(getByText("Test header")).toBeInTheDocument(); | ||
expect(getByText("Test body")).toBeInTheDocument(); | ||
}); | ||
|
||
test("Check if banner is closed when clicking the Close button", async () => { | ||
const { getByTestId, queryByTestId } = render(<Banner />, { | ||
wrapper, | ||
}); | ||
const user = userEvent.setup(); | ||
|
||
act(() => { | ||
banner({ | ||
header: "Test header", | ||
body: "Test body", | ||
pathnameToDisplayOn: "/dashboard", | ||
}); | ||
}); | ||
|
||
await user.click(getByTestId("banner-close")); | ||
|
||
expect(queryByTestId("banner-header")).not.toBeInTheDocument(); | ||
}); | ||
|
||
test("Check if banner is closed when navigating away", async () => { | ||
const { container, queryByTestId } = render(<Banner />, { | ||
wrapper, | ||
}); | ||
const user = userEvent.setup(); | ||
|
||
act(() => { | ||
banner({ | ||
header: "Test header", | ||
body: "Test body", | ||
pathnameToDisplayOn: "/dashboard", | ||
}); | ||
}); | ||
|
||
await user.click(container.querySelector("#dashboard-link")); | ||
|
||
expect(queryByTestId("banner-header")).not.toBeInTheDocument(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.