Skip to content

Commit

Permalink
Handle internal navigation for promotions
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementPasteau committed Oct 3, 2024
1 parent 524ca4d commit 5dd471b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
6 changes: 3 additions & 3 deletions newIDE/app/src/AnnouncementsFeed/AnnouncementFormatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type I18n as I18nType } from '@lingui/core';
import { type Route, type RouteArguments } from '../MainFrame/RouterContext';
import { selectMessageByLocale } from '../Utils/i18n/MessageByLocale';

const getAdapatedMessageAndRouteNavigationParams = (
const getAdaptedMessageAndRouteNavigationParams = (
message: string
): {|
message: string,
Expand Down Expand Up @@ -81,12 +81,12 @@ export const getAnnouncementContent = (
message: adaptedDesktopMessage,
routeNavigationParams: desktopRouteNavigationParams,
isClickableContent: isDesktopClickableContent,
} = getAdapatedMessageAndRouteNavigationParams(message);
} = getAdaptedMessageAndRouteNavigationParams(message);
const {
message: adaptedMobileMessage,
routeNavigationParams: mobileRouteNavigationParams,
isClickableContent: isMobileClickableContent,
} = getAdapatedMessageAndRouteNavigationParams(mobileMessage);
} = getAdaptedMessageAndRouteNavigationParams(mobileMessage);

return {
title,
Expand Down
9 changes: 1 addition & 8 deletions newIDE/app/src/AnnouncementsFeed/AnnouncementsFeedContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,7 @@ export const AnnouncementsFeedStateProvider = ({
listAllPromotions(),
]);

// Logic to remove once promotions are displayed to enough users.
// For now, we filter out promotions from the announcements.
const filteredAnnouncements = fetchedAnnouncements.filter(
announcement =>
!fetchedPromotions.find(promotion => promotion.id === announcement.id)
);

setAnnouncements(filteredAnnouncements);
setAnnouncements(fetchedAnnouncements);
setPromotions(fetchedPromotions);
} catch (error) {
console.error(`Unable to load the announcements from the api:`, error);
Expand Down
31 changes: 30 additions & 1 deletion newIDE/app/src/Promotions/PromotionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ import { type Promotion } from '../Utils/GDevelopServices/Announcement';
import { type Route, type RouteArguments } from '../MainFrame/RouterContext';
import Window from '../Utils/Window';

const getRouteNavigationParamsFromLink = (
link: string
): {| route: Route, params: RouteArguments |} | null => {
if (link.startsWith('https://editor.gdevelop.io')) {
const urlParams = new URLSearchParams(link.replace(/.*\?/, ''));
// $FlowFixMe - Assume that the arguments are always valid.
const route: ?Route = urlParams.get('initial-dialog');
const otherParams = {};
urlParams.forEach((value, key) => {
if (key !== 'initial-dialog') otherParams[key] = value;
});
if (route) {
return { route, params: otherParams };
}

return null;
}

return null;
};

export const getOnClick = ({
promotion,
navigateToRoute,
Expand All @@ -24,7 +45,15 @@ export const getOnClick = ({

const linkUrl = promotion.linkUrl;
if (linkUrl) {
return () => Window.openExternalURL(linkUrl);
const routeNavigationParams = getRouteNavigationParamsFromLink(linkUrl);

return routeNavigationParams
? () =>
navigateToRoute(
routeNavigationParams.route,
routeNavigationParams.params
)
: () => Window.openExternalURL(linkUrl);
}

return undefined;
Expand Down
20 changes: 11 additions & 9 deletions newIDE/app/src/Utils/GDevelopServices/Announcement.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ export type Announcement = {
buttonLabelByLocale?: MessageByLocale,
};

export type Promotion = {
id: string,
imageUrl: string,
mobileImageUrl: string,
display: 'all' | 'non-native-mobile',
type: 'game-template' | 'asset-pack' | 'game',
linkUrl?: string,
productId?: string,
};
export interface Promotion {
id: string;
imageUrl: string;
mobileImageUrl: string;
display: 'all' | 'non-native-mobile' | 'native-mobile';
type: 'game-template' | 'asset-pack' | 'game' | 'other';
linkUrl?: string;
productId?: string;
fromDate?: number;
toDate?: number;
}

export const listAllAnnouncements = async (): Promise<Array<Announcement>> => {
const response = await axios.get(
Expand Down

0 comments on commit 5dd471b

Please sign in to comment.