Merge branch 'ads-locale' into 'develop'

RumbleAdProvider: send Accept-Language header with request

See merge request soapbox-pub/soapbox-fe!1715
This commit is contained in:
Alex Gleason 2022-08-09 13:59:12 +00:00
commit 70a84d74ad
1 changed files with 21 additions and 12 deletions

View File

@ -1,3 +1,4 @@
import { getSettings } from 'soapbox/actions/settings';
import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import { normalizeCard } from 'soapbox/normalizers'; import { normalizeCard } from 'soapbox/normalizers';
@ -22,23 +23,31 @@ interface RumbleApiResponse {
const RumbleAdProvider: AdProvider = { const RumbleAdProvider: AdProvider = {
getAds: async(getState) => { getAds: async(getState) => {
const state = getState(); const state = getState();
const settings = getSettings(state);
const soapboxConfig = getSoapboxConfig(state); const soapboxConfig = getSoapboxConfig(state);
const endpoint = soapboxConfig.extensions.getIn(['ads', 'endpoint']) as string | undefined; const endpoint = soapboxConfig.extensions.getIn(['ads', 'endpoint']) as string | undefined;
if (endpoint) { if (endpoint) {
const response = await fetch(endpoint); const response = await fetch(endpoint, {
const data = await response.json() as RumbleApiResponse; headers: {
return data.ads.map(item => ({ 'Accept-Language': settings.get('locale', '*') as string,
impression: item.impression, },
card: normalizeCard({ });
type: item.type === 1 ? 'link' : 'rich',
image: item.asset, if (response.ok) {
url: item.click, const data = await response.json() as RumbleApiResponse;
}), return data.ads.map(item => ({
})); impression: item.impression,
} else { card: normalizeCard({
return []; type: item.type === 1 ? 'link' : 'rich',
image: item.asset,
url: item.click,
}),
}));
}
} }
return [];
}, },
}; };