Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-655 Don't render promotion in case it's empty #890

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/app/atoms/partners-promotion.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { FC, memo, useCallback } from 'react';
import React, { FC, memo, useCallback, useState } from 'react';

import { useDispatch } from 'react-redux';

import { useAppEnv } from 'app/env';
import { ReactComponent as CloseIcon } from 'app/icons/close.svg';
import { togglePartnersPromotionAction } from 'app/store/partners-promotion/actions';
import { useShouldShowPartnersPromoSelector, usePartnersPromoSelector } from 'app/store/partners-promotion/selectors';
import { isEmptyPromotion } from 'lib/apis/optimal';
import { t } from 'lib/i18n';
import { useConfirm } from 'lib/ui/dialog';

Expand All @@ -30,6 +31,7 @@ export const PartnersPromotion: FC<Props> = memo(({ variant }) => {
const dispatch = useDispatch();
const { popup } = useAppEnv();

const [isImageBroken, setIsImageBroken] = useState(false);
const { data: promo, isLoading, error } = usePartnersPromoSelector();
const shouldShowPartnersPromo = useShouldShowPartnersPromoSelector();

Expand All @@ -45,7 +47,11 @@ export const PartnersPromotion: FC<Props> = memo(({ variant }) => {
}
}, [confirm]);

if (!shouldShowPartnersPromo || Boolean(error)) {
const onImageError = useCallback(() => {
setIsImageBroken(true);
}, []);

if (!shouldShowPartnersPromo || Boolean(error) || isEmptyPromotion(promo) || isImageBroken) {
return null;
}

Expand All @@ -68,7 +74,7 @@ export const PartnersPromotion: FC<Props> = memo(({ variant }) => {
testID={PartnersPromotionSelectors.promoLink}
testIDProperties={{ variant, href: promo.link }}
>
<img className="h-10 w-10 rounded-circle" src={promo.image} alt="Partners promotion" />
<img className="h-10 w-10 rounded-circle" src={promo.image} alt="Partners promotion" onError={onImageError} />
<div className="flex flex-col gap-1">
<div className="flex gap-1">
<span className="text-gray-910 font-medium">{promo.copy.headline}</span>
Expand Down Expand Up @@ -106,7 +112,7 @@ export const PartnersPromotion: FC<Props> = memo(({ variant }) => {
testID={PartnersPromotionSelectors.promoLink}
testIDProperties={{ variant, href: promo.link }}
>
<img src={promo.image} alt="Partners promotion" className="shadow-lg rounded-lg" />
<img src={promo.image} alt="Partners promotion" className="shadow-lg rounded-lg" onError={onImageError} />
</Anchor>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions src/app/store/partners-promotion/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createAction } from '@reduxjs/toolkit';

import { OptimalPromotionInterface, OptimalPromoVariantEnum } from 'lib/apis/optimal';
import { OptimalPromotionType, OptimalPromoVariantEnum } from 'lib/apis/optimal';
import { createActions } from 'lib/store';

export const loadPartnersPromoAction = createActions<OptimalPromoVariantEnum, OptimalPromotionInterface, string>(
export const loadPartnersPromoAction = createActions<OptimalPromoVariantEnum, OptimalPromotionType, string>(
'partnersPromo/LOAD_PARTNERS'
);

Expand Down
4 changes: 2 additions & 2 deletions src/app/store/partners-promotion/state.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OptimalPromotionInterface } from 'lib/apis/optimal';
import { OptimalPromotionType } from 'lib/apis/optimal';
import { LoadableEntityState, createEntity } from 'lib/store';

import { mockPartnersPromotion } from './state.mock';

export interface PartnersPromotionState {
promotion: LoadableEntityState<OptimalPromotionInterface>;
promotion: LoadableEntityState<OptimalPromotionType>;
shouldShowPromotion: boolean;
}

Expand Down
11 changes: 9 additions & 2 deletions src/lib/apis/optimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export enum OptimalPromoVariantEnum {
Token = 'tw-token'
}

export interface OptimalPromotionInterface {
type EmptyPromotion = Record<string, undefined>;
type NormalPromotion = {
body: string;
campaign_type: string;
copy: {
Expand All @@ -28,11 +29,17 @@ export interface OptimalPromotionInterface {
text: string;
view_time_url: string;
view_url: string;
};

export type OptimalPromotionType = EmptyPromotion | NormalPromotion;

export function isEmptyPromotion(promotion: OptimalPromotionType): promotion is EmptyPromotion {
return !('link' in promotion && 'image' in promotion && 'copy' in promotion);
}

export const getOptimalPromotionImage$ = (variant: OptimalPromoVariantEnum) =>
from(
optimalApi.get<OptimalPromotionInterface>('api/v1/decision', {
optimalApi.get<OptimalPromotionType>('api/v1/decision', {
params: {
publisher: 'templewallet', // your-publisher-slug
ad_types: variant,
Expand Down