File size: 2,516 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Action } from 'redux';
import { Railcar } from 'calypso/data/marketplace/types';
import { decodeEntities } from 'calypso/lib/formatting';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import { READER_RECOMMENDED_SITES_REQUEST } from 'calypso/state/reader/action-types';
import {
	receiveRecommendedSites,
	RecommendedSitesRequestAction,
} from 'calypso/state/reader/recommended-sites/actions';

const noop = () => {};

export const requestRecommendedSites = ( action: RecommendedSitesRequestAction ): Action => {
	const { seed = 1, number = 10, offset = 0 } = action.payload;

	return http( {
		method: 'GET',
		path: '/read/recommendations/sites',
		query: { number, offset, seed, posts_per_site: 0 },
		apiVersion: '1.2',
		onSuccess: action,
		onFailure: action,
	} );
};

export interface RecommendedSitesBody {
	algorithm: string;
	sites: RecommendedSiteResponse[];
	meta: {
		next_page: string;
	};
}

interface RecommendedSiteResponse {
	blog_id: number;
	blog_title?: string;
	blog_url?: string;
	description: string;
	feed_id: number;
	feed_url: string;
	icon: {
		ico: string;
		img: string;
		media_id: string;
	};
	ID: number;
	name: string;
	railcar: Railcar;
	URL: string;
}

export interface RecommendedSite {
	algorithm: string;
	blogId: number;
	description: string;
	feedId: number;
	feedUrl: string;
	icon: string;
	railcar: Railcar;
	title: string;
	url: string;
}

export const mapResponseToRecommendedSites = ( {
	algorithm,
	sites,
}: RecommendedSitesBody ): RecommendedSite[] =>
	sites.map(
		( site: RecommendedSiteResponse ): RecommendedSite => ( {
			algorithm,
			blogId: site.blog_id,
			description: site.description,
			feedId: site.feed_id,
			feedUrl: site.feed_url,
			icon: site.icon?.img,
			railcar: site.railcar,
			title: decodeEntities( site.blog_title ?? site.name ),
			url: site.blog_url ?? site.URL,
		} )
	);

export const addRecommendedSites = (
	{ payload: { seed, offset } }: RecommendedSitesRequestAction,
	sites: RecommendedSite[]
) => receiveRecommendedSites( { sites, seed, offset } );

registerHandlers( 'state/data-layer/wpcom/read/recommendations/sites/index.js', {
	[ READER_RECOMMENDED_SITES_REQUEST ]: [
		dispatchRequest( {
			fetch: requestRecommendedSites,
			onSuccess: addRecommendedSites,
			onError: noop,
			fromApi: mapResponseToRecommendedSites,
		} ),
	],
} );