-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move onClick handler to a component
- Loading branch information
1 parent
7e80ff8
commit abe9c20
Showing
3 changed files
with
44 additions
and
34 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 |
---|---|---|
@@ -1,43 +1,18 @@ | ||
import { connect } from 'react-redux' | ||
import { Banner } from 'decentraland-ui2' | ||
import { | ||
getBanner, | ||
getBannerAssets, | ||
getError, | ||
isLoading | ||
} from '../../modules/campaign' | ||
import { getAnalytics } from '../../modules/analytics/utils' | ||
import { MapStateProps, OwnProps } from './Banner.types' | ||
import { sleep } from '../../lib/time' | ||
import { Banner } from './Banner' | ||
|
||
const mapState = (state: any, ownProps: OwnProps): MapStateProps => { | ||
const fields = getBanner(state, ownProps.id) ?? null | ||
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
const anchorEvent = (event as unknown) as React.MouseEvent< | ||
HTMLAnchorElement | ||
> | ||
anchorEvent.preventDefault() | ||
|
||
const analytics = getAnalytics() | ||
if (analytics) { | ||
analytics.track('CLICK_BANNER', { | ||
id: fields?.id, | ||
location: ownProps.id | ||
}) | ||
} | ||
// Delay the navigation to allow the analytics to be tracked | ||
sleep(300).then(() => { | ||
window.location.href = (anchorEvent.target as HTMLAnchorElement).href | ||
}) | ||
} | ||
|
||
return { | ||
onClick: handleClick, | ||
fields, | ||
assets: getBannerAssets(state, ownProps.id), | ||
isLoading: isLoading(state), | ||
error: getError(state) | ||
} | ||
} | ||
const mapState = (state: any, ownProps: OwnProps): MapStateProps => ({ | ||
fields: getBanner(state, ownProps.id) ?? null, | ||
assets: getBannerAssets(state, ownProps.id), | ||
isLoading: isLoading(state), | ||
error: getError(state) | ||
}) | ||
|
||
export default connect<MapStateProps, {}, OwnProps>(mapState)(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useCallback } from 'react' | ||
import { Banner as BannerComponent } from 'decentraland-ui2' | ||
import { getAnalytics } from '../../modules/analytics/utils' | ||
import { BannerProps } from './Banner.types' | ||
import { sleep } from '../../lib/time' | ||
|
||
export const Banner: React.FC<BannerProps> = (props: BannerProps) => { | ||
const { fields, id, ...rest } = props | ||
const handleClick = useCallback( | ||
(event: React.MouseEvent<HTMLButtonElement>) => { | ||
const anchorEvent = (event as unknown) as React.MouseEvent< | ||
HTMLAnchorElement | ||
> | ||
anchorEvent.preventDefault() | ||
|
||
const analytics = getAnalytics() | ||
if (analytics) { | ||
analytics.track('CLICK_BANNER', { | ||
id: fields?.id, | ||
location: id | ||
}) | ||
} | ||
// Delay the navigation to allow the analytics to be tracked | ||
sleep(300).then(() => { | ||
window.location.href = (anchorEvent.target as HTMLAnchorElement).href | ||
}) | ||
}, | ||
[fields?.id, id] | ||
) | ||
|
||
return <BannerComponent onClick={handleClick} fields={fields} {...rest} /> | ||
} |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { BannerFields } from '@dcl/schemas' | ||
import { BannerProps as UIBannerProps } from 'decentraland-ui2' | ||
|
||
export type BannerProps = UIBannerProps & { id: string } | ||
export type BannerProps = Omit<UIBannerProps, 'fields'> & { | ||
fields: (BannerFields & { id: string }) | null | ||
} & { id: string } | ||
export type OwnProps = Pick<BannerProps, 'id'> | ||
export type MapStateProps = Pick< | ||
BannerProps, | ||
'fields' | 'assets' | 'isLoading' | 'error' | 'onClick' | ||
'fields' | 'assets' | 'isLoading' | 'error' | ||
> |