-
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.
refactor: extract SaleMessage component and query renderer
- Loading branch information
1 parent
6f75eb5
commit a4755f2
Showing
6 changed files
with
158 additions
and
162 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 was deleted.
Oops, something went wrong.
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,132 @@ | ||
import { FC, ReactElement } from "react" | ||
import { createFragmentContainer, graphql } from "react-relay" | ||
import { Flex, Text } from "@artsy/palette" | ||
import { SystemQueryRenderer } from "System/Relay/SystemQueryRenderer" | ||
import { useTimer } from "Utils/Hooks/useTimer" | ||
import { SaleMessageQuery } from "__generated__/SaleMessageQuery.graphql" | ||
import { SaleMessage_artwork$data } from "__generated__/SaleMessage_artwork.graphql" | ||
import { Details_artwork$data } from "__generated__/Details_artwork.graphql" | ||
|
||
interface PublicSaleMessageProps { | ||
artwork: Details_artwork$data | ||
} | ||
|
||
interface ActivePartnerOfferTimerProps { | ||
artwork: SaleMessage_artwork$data | ||
} | ||
|
||
interface SaleMessageProps { | ||
artwork: SaleMessage_artwork$data | ||
publicSaleMessage: ReactElement | ||
} | ||
|
||
const PublicSaleMessage: React.FC<PublicSaleMessageProps> = props => { | ||
const { | ||
artwork: { sale, sale_message, sale_artwork }, | ||
} = props | ||
|
||
if (sale?.is_auction && !sale?.is_closed) { | ||
const highestBid_display = sale_artwork?.highest_bid?.display | ||
const openingBid_display = sale_artwork?.opening_bid?.display | ||
|
||
return <>{highestBid_display || openingBid_display || ""}</> | ||
} | ||
|
||
// NBSP is used to prevent un-aligned carousels | ||
return <>{sale_message ?? " "}</> | ||
} | ||
|
||
const ActivePartnerOfferTimer: React.FC<ActivePartnerOfferTimerProps> = ({ | ||
artwork: { collectorSignals }, | ||
}) => { | ||
const SEPARATOR = <> </> | ||
const { endAt } = collectorSignals?.partnerOffer ?? {} | ||
const { time } = useTimer(endAt ?? "") | ||
const { days, hours } = time | ||
|
||
return ( | ||
<Text | ||
variant="sm-display" | ||
lineHeight="22px" | ||
color="blue100" | ||
alignSelf="flex-start" | ||
fontWeight="normal" | ||
> | ||
Exp.{SEPARATOR} | ||
{Number(days)}d{SEPARATOR} | ||
{Number(hours)}h{SEPARATOR} | ||
</Text> | ||
) | ||
} | ||
|
||
const SaleMessage: React.FC<SaleMessageProps> = ({ | ||
artwork, | ||
publicSaleMessage, | ||
}) => { | ||
const partnerOffer = artwork?.collectorSignals?.partnerOffer | ||
const offeredPrice = partnerOffer?.priceWithDiscount?.display | ||
|
||
return partnerOffer ? ( | ||
<Flex flexDirection="row" alignItems="center"> | ||
<Text lineHeight="22px">{offeredPrice}</Text> | ||
| ||
<ActivePartnerOfferTimer artwork={artwork} /> | ||
</Flex> | ||
) : ( | ||
publicSaleMessage | ||
) | ||
} | ||
|
||
const SaleMessageFragmentContainer = createFragmentContainer(SaleMessage, { | ||
artwork: graphql` | ||
fragment SaleMessage_artwork on Artwork { | ||
collectorSignals { | ||
partnerOffer { | ||
endAt | ||
priceWithDiscount { | ||
display | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
}) | ||
|
||
interface SaleMessageQueryRendererProps { | ||
id: string | ||
artwork: Details_artwork$data | ||
} | ||
|
||
export const SaleMessageQueryRenderer: FC<SaleMessageQueryRendererProps> = ({ | ||
id, | ||
artwork, | ||
}) => { | ||
const publicSaleMessage = <PublicSaleMessage artwork={artwork} /> | ||
|
||
return ( | ||
<SystemQueryRenderer<SaleMessageQuery> | ||
lazyLoad | ||
query={graphql` | ||
query SaleMessageQuery($id: String!) { | ||
artwork(id: $id) { | ||
...SaleMessage_artwork | ||
} | ||
} | ||
`} | ||
placeholder={publicSaleMessage} | ||
variables={{ id }} | ||
render={({ error, props }) => { | ||
if (error || !props?.artwork) { | ||
return publicSaleMessage | ||
} | ||
|
||
return ( | ||
<SaleMessageFragmentContainer | ||
artwork={props.artwork} | ||
publicSaleMessage={publicSaleMessage} | ||
/> | ||
) | ||
}} | ||
/> | ||
) | ||
} |
28 changes: 14 additions & 14 deletions
28
...ted__/PartnerOfferedPriceQuery.graphql.ts → ...__generated__/SaleMessageQuery.graphql.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.