Skip to content

Commit

Permalink
fix: use the same relay env between Home screen and useMutation (#8636
Browse files Browse the repository at this point in the history
)

* refactor: pass correct relay env

* refactor: remove refresh helpers

* refactor: remove unused `contextScreen` prop

* fix: failed tests

* Revert "fix: failed tests"

This reverts commit fd536c8.

* sandbox
  • Loading branch information
dimatretyak authored May 9, 2023
1 parent fe8aa88 commit c868677
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 44 deletions.
2 changes: 0 additions & 2 deletions src/app/Components/Artist/ArtistHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ArtistHeaderFollowArtistMutation } from "__generated__/ArtistHeaderFoll
import { ArtistHeader_artist$data } from "__generated__/ArtistHeader_artist.graphql"
import { FollowButton } from "app/Components/Button/FollowButton"
import { formatLargeNumberOfItems } from "app/utils/formatLargeNumberOfItems"
import { refreshOnArtistFollow } from "app/utils/refreshHelpers"
import { Schema } from "app/utils/track"
import { useState } from "react"
import { commitMutation, createFragmentContainer, graphql, RelayProp } from "react-relay"
Expand Down Expand Up @@ -87,7 +86,6 @@ export const ArtistHeader: React.FC<Props> = ({ artist, relay }) => {
}

const successfulFollowChange = () => {
refreshOnArtistFollow()
trackEvent({
action_name: artist.isFollowed
? Schema.ActionNames.ArtistUnfollow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useFeatureFlag } from "app/utils/hooks/useFeatureFlag"
import { SaveArtworkOptions, useSaveArtwork } from "app/utils/mutations/useSaveArtwork"
import { graphql, useFragment } from "react-relay"

interface Options extends Pick<SaveArtworkOptions, "onCompleted" | "onError" | "contextScreen"> {
interface Options extends Pick<SaveArtworkOptions, "onCompleted" | "onError"> {
artworkFragmentRef: useSaveArtworkToArtworkLists_artwork$key
}

Expand Down
3 changes: 1 addition & 2 deletions src/app/Components/ArtworkRail/ArtworkRailCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export const ArtworkRailCard: React.FC<ArtworkRailCardProps> = ({
internalID,
isSaved,
onCompleted: onArtworkSavedOrUnSaved,
contextScreen: trackingContextScreenOwnerType,
})

const displayForRecentlySoldArtwork =
Expand Down Expand Up @@ -397,7 +396,7 @@ const RecentlySoldCardSection: React.FC<
<Text variant="lg-display" numberOfLines={1}>
{priceRealizedDisplay}
</Text>
{performanceDisplay && (
{!!performanceDisplay && (
<Text variant="lg-display" color="green" numberOfLines={1}>
{`+${performanceDisplay}`}
</Text>
Expand Down
9 changes: 4 additions & 5 deletions src/app/Scenes/Home/Home.tests.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { defaultEnvironment } from "app/system/relay/createEnvironment"
import { getMockRelayEnvironment } from "app/system/relay/defaultEnvironment"
import { flushPromiseQueue } from "app/utils/tests/flushPromiseQueue"
import { renderWithHookWrappersTL } from "app/utils/tests/renderWithWrappers"
import { renderWithWrappers } from "app/utils/tests/renderWithWrappers"
import { act } from "react-test-renderer"
import { GraphQLSingularResponse } from "relay-runtime"
import { createMockEnvironment } from "relay-test-utils"
import { HomeQueryRenderer } from "./Home"

jest.mock("app/Components/Home/ArtistRails/ArtistRail", () => ({
Expand All @@ -19,11 +18,11 @@ jest.mock("app/Scenes/Home/Components/SalesRail", () => ({
SalesRailFragmentContainer: jest.fn(() => null),
}))

const mockEnvironment = defaultEnvironment as ReturnType<typeof createMockEnvironment>
const mockEnvironment = getMockRelayEnvironment()

describe(HomeQueryRenderer, () => {
const getWrapper = async () => {
const tree = renderWithHookWrappersTL(<HomeQueryRenderer />, mockEnvironment)
const tree = renderWithWrappers(<HomeQueryRenderer environment={mockEnvironment} />)

mockMostRecentOperation("HomeAboveTheFoldQuery", {
errors: [],
Expand Down
21 changes: 9 additions & 12 deletions src/app/Scenes/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
import { search2QueryDefaultVariables } from "app/Scenes/Search/Search2"
import { ViewingRoomsHomeMainRail } from "app/Scenes/ViewingRoom/Components/ViewingRoomsHomeRail"
import { GlobalStore } from "app/store/GlobalStore"
import { defaultEnvironment } from "app/system/relay/createEnvironment"
import { getRelayEnvironment } from "app/system/relay/defaultEnvironment"
import { AboveTheFoldQueryRenderer } from "app/utils/AboveTheFoldQueryRenderer"
import { useExperimentVariant } from "app/utils/experiments/hooks"
import { maybeReportExperimentVariant } from "app/utils/experiments/reporter"
Expand All @@ -64,7 +64,6 @@ import {
useMemoizedRandom,
} from "app/utils/placeholders"
import { usePrefetch } from "app/utils/queryPrefetching"
import { RefreshEvents, HOME_SCREEN_REFRESH_KEY } from "app/utils/refreshHelpers"
import { ProvideScreenTracking, Schema } from "app/utils/track"
import {
ArtworkActionTrackingProps,
Expand Down Expand Up @@ -94,6 +93,8 @@ import {
import { createRefetchContainer, graphql, RelayRefetchProp } from "react-relay"

import { useTracking } from "react-tracking"
import RelayModernEnvironment from "relay-runtime/lib/store/RelayModernEnvironment"
import { RelayMockEnvironment } from "relay-test-utils/lib/RelayModernMockEnvironment"
import { useContentCards } from "./Components/ContentCards"
import HomeAnalytics from "./homeAnalytics"
import { useHomeModules } from "./useHomeModules"
Expand Down Expand Up @@ -200,14 +201,6 @@ const Home = memo((props: HomeProps) => {

const { isRefreshing, handleRefresh, scrollRefs } = useHandleRefresh(relay, modules)

useEffect(() => {
RefreshEvents.addListener(HOME_SCREEN_REFRESH_KEY, handleRefresh)

return () => {
RefreshEvents.removeListener(HOME_SCREEN_REFRESH_KEY, handleRefresh)
}
}, [])

const renderItem: ListRenderItem<HomeModule> | null | undefined = useCallback(
({ item, index }: { item: HomeModule; index: number }) => {
const trackingProps = extractArtworkActionTrackingProps(item)
Expand Down Expand Up @@ -698,7 +691,11 @@ const messages = {
},
}

export const HomeQueryRenderer: React.FC = () => {
interface HomeQRProps {
environment?: RelayModernEnvironment | RelayMockEnvironment
}

export const HomeQueryRenderer: React.FC<HomeQRProps> = ({ environment }) => {
const { flash_message } = GlobalStore.useAppState(
(state) => state.bottomTabs.sessionState.tabProps.home ?? {}
) as {
Expand Down Expand Up @@ -741,7 +738,7 @@ export const HomeQueryRenderer: React.FC = () => {

return (
<AboveTheFoldQueryRenderer<HomeAboveTheFoldQuery, HomeBelowTheFoldQuery>
environment={defaultEnvironment}
environment={environment || getRelayEnvironment()}
above={{
query: graphql`
query HomeAboveTheFoldQuery($version: String!) {
Expand Down
6 changes: 1 addition & 5 deletions src/app/utils/mutations/useSaveArtwork.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ScreenOwnerType } from "@artsy/cohesion"
import { refreshOnArtworkSave } from "app/utils/refreshHelpers"
import { Schema } from "app/utils/track"
import { useMutation } from "react-relay"
import { graphql } from "relay-runtime"

Expand All @@ -10,7 +8,6 @@ export interface SaveArtworkOptions {
isSaved: boolean | null
onCompleted?: (isSaved: boolean) => void
onError?: () => void
contextScreen?: Schema.OwnerEntityTypes | ScreenOwnerType
}

export const useSaveArtwork = ({
Expand All @@ -19,7 +16,6 @@ export const useSaveArtwork = ({
isSaved,
onCompleted,
onError,
contextScreen,
}: SaveArtworkOptions) => {
const [commit] = useMutation(SaveArtworkMutation)
const nextSavedState = !isSaved
Expand All @@ -41,7 +37,7 @@ export const useSaveArtwork = ({
},
},
onCompleted: () => {
refreshOnArtworkSave(contextScreen)
refreshOnArtworkSave()
onCompleted?.(nextSavedState)
},
onError: () => {
Expand Down
18 changes: 1 addition & 17 deletions src/app/utils/refreshHelpers.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import EventEmitter from "events"
import { PAGE_SIZE } from "app/Components/constants"
import { SaveArtworkOptions } from "app/utils/mutations/useSaveArtwork"
import { Schema } from "app/utils/track"
import { useState } from "react"
import { RefreshControl } from "react-native"

Expand All @@ -11,23 +9,9 @@ RefreshEvents.setMaxListeners(20)
export const FAVORITE_ARTWORKS_REFRESH_KEY = "refreshFavoriteArtworks"
export const MY_COLLECTION_REFRESH_KEY = "refreshMyCollection"
export const MY_COLLECTION_INSIGHTS_REFRESH_KEY = "refreshMyCollectionInsights"
export const HOME_SCREEN_REFRESH_KEY = "refreshHomeScreen"

export const refreshOnArtworkSave = (contextScreen: SaveArtworkOptions["contextScreen"]) => {
export const refreshOnArtworkSave = () => {
refreshFavoriteArtworks()

refreshHomeScreen(contextScreen)
}

export const refreshOnArtistFollow = (contextScreen?: Schema.OwnerEntityTypes) => {
refreshHomeScreen(contextScreen)
}

export const refreshHomeScreen = (contextScreen: SaveArtworkOptions["contextScreen"]) => {
// Only refresh home screen if we are not on the home screen
if (contextScreen === Schema.OwnerEntityTypes.Home) return

RefreshEvents.emit(HOME_SCREEN_REFRESH_KEY)
}

export const refreshMyCollection = () => {
Expand Down

0 comments on commit c868677

Please sign in to comment.