-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show a banner based on some url param (#5620)
Co-authored-by: Erik Stockmeier <erik.d.stock@gmail.com> Co-authored-by: Daniel Levenson <dleve123@gmail.com>
- Loading branch information
1 parent
5a3cf5c
commit 5e4e705
Showing
8 changed files
with
101 additions
and
35 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
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,14 @@ | ||
describe("flash_message", () => { | ||
it("renders the email confirmed banner when ?flash_message is confirmed", () => { | ||
cy.visit("/?flash_message=confirmed") | ||
cy.get("#main-layout-header").should( | ||
"contain", | ||
"Your email has been confirmed" | ||
) | ||
}) | ||
|
||
it("ignores flash messages that aren't explicitly supported", () => { | ||
cy.visit("/?flash_message=l33thaxor") | ||
cy.get("#main-layout-header").should("not.contain", "l33thaxor") | ||
}) | ||
}) |
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
26 changes: 7 additions & 19 deletions
26
src/v2/Components/FlashBanner.tsx → ...2/Components/FlashBanner/FlashMessage.tsx
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
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
src/v2/Components/FlashBanner/__tests__/FlashBanner.jest.tsx
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,32 @@ | ||
import { mount } from "enzyme" | ||
import "jest-styled-components" | ||
import React from "react" | ||
import { FlashBanner } from "v2/Components/FlashBanner" | ||
|
||
const { location: originalLocation } = window | ||
|
||
describe("FlashBanner", () => { | ||
afterEach(() => { | ||
window.location = originalLocation | ||
}) | ||
|
||
it("renders a banner based on a messageCode prop", () => { | ||
const wrapper = mount(<FlashBanner messageCode="confirmed" />) | ||
expect(wrapper.text()).toContain("Your email has been confirmed.") | ||
}) | ||
|
||
it("returns nothing without complaint if the message code is not supported", () => { | ||
const wrapper = mount(<FlashBanner messageCode="bad_message" />) | ||
expect(wrapper.html()).toBeNull() | ||
}) | ||
|
||
it("can determine a messageCode from the query string", () => { | ||
delete window.location | ||
const fakeLocation: any = { search: "?flash_message=expired_token" } | ||
window.location = fakeLocation | ||
|
||
const wrapper = mount(<FlashBanner />) | ||
|
||
expect(wrapper.text()).toContain("Link expired. Resend verification email.") | ||
}) | ||
}) |
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,43 @@ | ||
import React from "react" | ||
import qs from "qs" | ||
import { Banner, Sans } from "@artsy/palette" | ||
|
||
/** Flash messages triggered by a url `flash_message` query param | ||
* A message can be a string or a React Component that takes no props | ||
* (that is, use relay or context if necessary) | ||
*/ | ||
const messageByQueryParam: { [k: string]: string | React.FC } = { | ||
confirmed: "Your email has been confirmed.", | ||
already_confirmed: "You have already confirmed your email.", | ||
invalid_token: "An error has occurred. Please contact support@artsy.net.", | ||
blank_token: "An error has occurred. Please contact support@artsy.net.", | ||
expired_token: "Link expired. Resend verification email.", | ||
} | ||
|
||
/** | ||
* The component responsible for selecting a flash message key from the `flash_message` url query param | ||
*/ | ||
export const FlashBanner: React.FunctionComponent<{ | ||
messageCode?: string | ||
}> = props => { | ||
const [messageCode, setMessageCode] = React.useState(props.messageCode) | ||
|
||
React.useEffect(() => { | ||
if (!messageCode) { | ||
const query = qs.parse(window.location.search.slice(1)) | ||
if (query["flash_message"]) setMessageCode(query["flash_message"]) | ||
} | ||
}, []) | ||
|
||
if (!messageCode) return null | ||
const Message = messageByQueryParam[messageCode] | ||
if (!Message) return null | ||
|
||
return ( | ||
<Banner dismissable backgroundColor="black100"> | ||
<Sans color="white100" size="3" lineHeight={1} weight="medium"> | ||
{typeof Message === "string" ? Message : <Message />} | ||
</Sans> | ||
</Banner> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.