Skip to content

Commit

Permalink
Merge pull request artsy#3239 from artsy/type-open-auth
Browse files Browse the repository at this point in the history
Helpers for opening auth modals are strictly typed
  • Loading branch information
eessex committed Mar 9, 2020
2 parents 26b2c4b + 3d68240 commit 4cb981b
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 117 deletions.
1 change: 1 addition & 0 deletions src/Apps/Artist/Components/ArtistHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ const handleOpenAuth = (mediator, artist) => {
entity: artist,
contextModule: Schema.ContextModule.ArtistPage,
intent: AuthModalIntent.FollowArtist,
kind: "artist",
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const handleOpenAuth = (mediator, artist) => {
entity: artist,
contextModule: Schema.ContextModule.RecommendedArtists,
intent: AuthModalIntent.FollowArtist,
kind: "artist",
})
}

Expand Down
1 change: 1 addition & 0 deletions src/Apps/Artist/Routes/Overview/Components/ZeroState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const ZeroState = props => {
entity: artist,
contextModule: AnalyticsSchema.ContextModule.ArtworkFilter,
intent: AuthModalIntent.FollowArtist,
kind: "artist",
})
}

Expand Down
1 change: 1 addition & 0 deletions src/Apps/Artwork/Components/ArtistInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class ArtistInfo extends Component<ArtistInfoProps, ArtistInfoState> {
entity: artist,
contextModule: Schema.ContextModule.ArtworkPage,
intent: AuthModalIntent.FollowArtist,
kind: "artist",
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class ArtworkDetailsAboutTheWorkFromPartner extends React.Component<
entity: partner,
contextModule: Schema.ContextModule.ArtworkPage,
intent: AuthModalIntent.FollowPartner,
kind: "profile",
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class ArtworkSidebarArtists extends React.Component<ArtistsProps> {
entity: artist,
contextModule: Schema.ContextModule.ArtworkPage,
intent: AuthModalIntent.FollowArtist,
kind: "artist",
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const handleOpenAuth = (mediator, artist) => {
entity: artist,
contextModule: AnalyticsSchema.ContextModule.CollectionDescription,
intent: AuthModalIntent.FollowArtist,
kind: "artist",
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/Components/Artwork/Save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ export class SaveButton extends React.Component<SaveProps, SaveState> {
openAuthModal(this.props.mediator, {
contextModule: Schema.ContextModule.ArtworkPage,
entity: {
id: this.props.artwork.id,
id: this.props.artwork.slug,
name: this.props.artwork.title,
},
kind: "artworks",
intent: AuthModalIntent.SaveArtwork,
})
}
Expand Down
20 changes: 17 additions & 3 deletions src/Components/Authentication/FormSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MobileForgotPasswordForm } from "Components/Authentication/Mobile/Forgo
import { MobileLoginForm } from "Components/Authentication/Mobile/LoginForm"
import { MobileSignUpForm } from "Components/Authentication/Mobile/SignUpForm"
import {
AfterSignUpAction,
FormComponentType,
InputValues,
ModalOptions,
Expand Down Expand Up @@ -66,6 +67,7 @@ export class FormSwitcher extends React.Component<FormSwitcherProps, State> {
destination,
redirectTo,
intent,
title,
trigger,
triggerSeconds,
},
Expand All @@ -79,7 +81,7 @@ export class FormSwitcher extends React.Component<FormSwitcherProps, State> {
action: "Auth impression",
type,
context_module: contextModule,
modal_copy: copy,
modal_copy: copy || title,
trigger: trigger || "click",
trigger_seconds: triggerSeconds,
intent,
Expand Down Expand Up @@ -119,6 +121,17 @@ export class FormSwitcher extends React.Component<FormSwitcherProps, State> {
}
}

getAfterSignupAction = (options: ModalOptions): AfterSignUpAction => {
const { afterSignUpAction, action, kind, objectId } = options
return (
afterSignUpAction || {
action,
kind,
objectId,
}
)
}

render() {
const {
error,
Expand All @@ -135,10 +148,11 @@ export class FormSwitcher extends React.Component<FormSwitcherProps, State> {
accepted_terms_of_service: true,
agreed_to_receive_emails: true,
"signup-referer": options.signupReferer,
afterSignUpAction: this.getAfterSignupAction(options),
},
options.redirectTo
options.redirectTo || options["redirect-to"]
? {
"redirect-to": options.redirectTo,
"redirect-to": options.redirectTo || options["redirect-to"],
}
: null,
options.intent
Expand Down
17 changes: 16 additions & 1 deletion src/Components/Authentication/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface FormProps {
showRecaptchaDisclaimer?: boolean
}

interface AfterSignUpAction {
export interface AfterSignUpAction {
action: "save" | "follow" | "editorialSignup"
objectId?: string
kind?: "artist" | "artworks" | "gene" | "profile" | "show"
Expand Down Expand Up @@ -99,6 +99,21 @@ export interface ModalOptions {
* The image rendered with the modal
*/
image?: string
/**
* MOBILE ONLY
* Used to construct afterSignupAction from query params
*/
kind?: AfterSignUpAction["kind"]
/**
* MOBILE ONLY
* Used to construct afterSignupAction from query params
*/
action?: AfterSignUpAction["action"]
/**
* MOBILE ONLY
* Used to construct afterSignupAction from query params
*/
objectId?: AfterSignUpAction["objectId"]
}

export type FormComponentType =
Expand Down
5 changes: 3 additions & 2 deletions src/Components/FollowButton/FollowArtistButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FollowButton } from "./Button"
import { FollowButtonDeprecated } from "./ButtonDeprecated"
import { FollowTrackingData } from "./Typings"

import { ModalOptions, ModalType } from "Components/Authentication/Types"
import {
commitMutation,
createFragmentContainer,
Expand All @@ -25,7 +26,7 @@ interface Props
artist?: FollowArtistButton_artist
tracking?: TrackingProp
trackingData?: FollowTrackingData
onOpenAuthModal?: (type: "register" | "login", config?: object) => void
onOpenAuthModal?: (type: ModalType, config?: ModalOptions) => void

/**
* FIXME: Default is true due to legacy code. If false, use new @artsy/palette
Expand Down Expand Up @@ -86,7 +87,7 @@ export class FollowArtistButton extends React.Component<Props, State> {
intent: "follow artist",
copy: "Sign up to follow artists",
}
onOpenAuthModal("register", config)
onOpenAuthModal(ModalType.signup, config)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Components/FollowButton/FollowGeneButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FollowGeneButtonMutation } from "__generated__/FollowGeneButtonMutation.graphql"
import * as Artsy from "Artsy"
import { ModalOptions, ModalType } from "Components/Authentication/Types"
import { extend } from "lodash"
import React from "react"
import {
Expand All @@ -20,7 +21,7 @@ interface Props
gene?: FollowGeneButton_gene
tracking?: TrackingProp
trackingData?: FollowTrackingData
onOpenAuthModal?: (type: "register" | "login", config?: object) => void
onOpenAuthModal?: (type: ModalType, config?: ModalOptions) => void
}

export class FollowGeneButton extends React.Component<Props> {
Expand Down Expand Up @@ -67,7 +68,7 @@ export class FollowGeneButton extends React.Component<Props> {
this.trackFollow()
} else {
onOpenAuthModal &&
onOpenAuthModal("register", {
onOpenAuthModal(ModalType.signup, {
contextModule: "intext tooltip",
intent: "follow gene",
copy: "Sign up to follow categories",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("FollowArtistButton", () => {
component.find(FollowButtonDeprecated).simulate("click")
const args = testProps.onOpenAuthModal.mock.calls[0]

expect(args[0]).toBe("register")
expect(args[0]).toBe("signup")
expect(args[1].contextModule).toBe("intext tooltip")
expect(args[1].intent).toBe("follow artist")
expect(args[1].copy).toBe("Sign up to follow artists")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("FollowGeneButton", () => {
component.find(FollowButtonDeprecated).simulate("click")
const args = testProps.onOpenAuthModal.mock.calls[0]

expect(args[0]).toBe("register")
expect(args[0]).toBe("signup")
expect(args[1].contextModule).toBe("intext tooltip")
expect(args[1].intent).toBe("follow gene")
expect(args[1].copy).toBe("Sign up to follow categories")
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Publishing/Article.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Theme } from "@artsy/palette"
import React from "react"

import { ModalOptions, ModalType } from "Components/Authentication/Types"
import { BannerWrapper } from "Components/Publishing/Banner/Banner"
import { PixelTracker } from "Components/Publishing/Display/ExternalTrackers"
import { EditorialFeature } from "Components/Publishing/EditorialFeature/EditorialFeature"
Expand Down Expand Up @@ -48,7 +49,7 @@ export interface ArticleProps {
tracking?: TrackingProp
closeViewer?: () => void
viewerIsOpen?: boolean
onOpenAuthModal?: (type: "register" | "login", config: object) => void
onOpenAuthModal?: (type: ModalType, config: ModalOptions) => void
onExpand?: () => void
shouldAdRender?: boolean
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("ArtistToolTip", () => {
component.find(FollowArtistButton).simulate("click")
const args = context.onOpenAuthModal.mock.calls[0]

expect(args[0]).toBe("register")
expect(args[0]).toBe("signup")
expect(args[1].contextModule).toBe("intext tooltip")
expect(args[1].intent).toBe("follow artist")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("GeneTooltip", () => {
component.find(FollowGeneButton).simulate("click")
const args = context.onOpenAuthModal.mock.calls[0]

expect(args[0]).toBe("register")
expect(args[0]).toBe("signup")
expect(args[1].contextModule).toBe("intext tooltip")
expect(args[1].intent).toBe("follow gene")
})
Expand Down
1 change: 1 addition & 0 deletions src/Components/v2/ArtistCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const handleOpenAuth = props => {
entity: props.artist,
contextModule: Schema.ContextModule.ArtworkPage,
intent: AuthModalIntent.FollowArtist,
kind: "artist",
})
}

Expand Down
127 changes: 127 additions & 0 deletions src/Utils/__tests__/openAuthModal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import * as Schema from "Artsy/Analytics/Schema"
import {
AuthModalIntent,
AuthModalOptions,
openAuthModal,
} from "../openAuthModal"

jest.mock("sharify", () => ({ data: jest.fn() }))
const sd = require("sharify").data

const artistArgs: AuthModalOptions = {
contextModule: Schema.ContextModule.ArtistPage,
entity: {
slug: "andy-warhol",
name: "Andy Warhol",
},
kind: "artist",
intent: AuthModalIntent.FollowArtist,
}

const partnerArgs: AuthModalOptions = {
contextModule: Schema.ContextModule.AboutTheWorkPartner,
entity: {
slug: "david-zwirner",
name: "David Zwirner",
},
kind: "profile",
intent: AuthModalIntent.FollowPartner,
}

const artworkArgs: AuthModalOptions = {
contextModule: Schema.ContextModule.ArtworkPage,
entity: {
id: "andy-warhol-skull",
name: "Skull",
},
intent: AuthModalIntent.SaveArtwork,
kind: "artworks",
}

describe("openAuthModal", () => {
let mediator

beforeEach(() => {
mediator = {
trigger: jest.fn(),
}
window.location.assign = jest.fn()
})

describe("desktop", () => {
it("transforms args for following artists", () => {
openAuthModal(mediator, artistArgs)

expect(mediator.trigger).toBeCalledWith("open:auth", {
afterSignUpAction: {
action: "follow",
kind: "artist",
objectId: "andy-warhol",
},
copy: "Sign up to follow Andy Warhol",
intent: "follow artist",
mode: "signup",
})
})

it("transforms args for following partners", () => {
openAuthModal(mediator, partnerArgs)

expect(mediator.trigger).toBeCalledWith("open:auth", {
afterSignUpAction: {
action: "follow",
kind: "profile",
objectId: "david-zwirner",
},
copy: "Sign up to follow David Zwirner",
intent: "follow partner",
mode: "signup",
})
})

it("transforms args for saving artworks", () => {
openAuthModal(mediator, artworkArgs)

expect(mediator.trigger).toBeCalledWith("open:auth", {
afterSignUpAction: {
action: "save",
kind: "artworks",
objectId: "andy-warhol-skull",
},
copy: "Sign up to save artworks",
intent: "save artwork",
mode: "signup",
})
})
})

describe("mobile", () => {
beforeEach(() => {
sd.IS_MOBILE = true
})

it("transforms args for following artists", () => {
openAuthModal(mediator, artistArgs)
expect(window.location.assign).toBeCalledWith(
"/sign_up?redirect-to=http://localhost/&action=follow&contextModule=Artist%20page&copy=Sign%20up%20to%20follow%20Andy%20Warhol&intent=follow%20artist&kind=artist&objectId=andy-warhol"
)
expect(mediator.trigger).not.toBeCalled()
})

it("transforms args for following partners", () => {
openAuthModal(mediator, partnerArgs)
expect(window.location.assign).toBeCalledWith(
"/sign_up?redirect-to=http://localhost/&action=follow&contextModule=About%20the%20Work%20%28Partner%29&copy=Sign%20up%20to%20follow%20David%20Zwirner&intent=follow%20partner&kind=profile&objectId=david-zwirner"
)
expect(mediator.trigger).not.toBeCalled()
})

it("transforms args for saving artworks", () => {
openAuthModal(mediator, artworkArgs)
expect(window.location.assign).toBeCalledWith(
"/sign_up?redirect-to=http://localhost/&action=save&contextModule=Artwork%20page&copy=Sign%20up%20to%20save%20artworks&intent=save%20artwork&kind=artworks&objectId=andy-warhol-skull"
)
expect(mediator.trigger).not.toBeCalled()
})
})
})
Loading

0 comments on commit 4cb981b

Please sign in to comment.