File size: 2,799 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
100
101
102
103
import { translate } from 'i18n-calypso';
import { map, truncate } from 'lodash';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { noRetry } from 'calypso/state/data-layer/wpcom-http/pipeline/retry-on-failure/policies';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import { errorNotice } from 'calypso/state/notices/actions';
import { READER_FEED_SEARCH_REQUEST, READER_FEED_REQUEST } from 'calypso/state/reader/action-types';
import { receiveFeedSearch } from 'calypso/state/reader/feed-searches/actions';
import queryKey from 'calypso/state/reader/feed-searches/query-key';
import {
	receiveReaderFeedRequestSuccess,
	receiveReaderFeedRequestFailure,
} from 'calypso/state/reader/feeds/actions';

export function fromApi( apiResponse ) {
	const feeds = map( apiResponse.feeds, ( feed ) => ( {
		...feed,
		feed_URL: feed.subscribe_URL,
	} ) );

	const total = apiResponse.total > 200 ? 200 : apiResponse.total;

	return {
		feeds,
		total,
	};
}

export function requestReadFeedSearch( action ) {
	if ( ! ( action.payload && action.payload.query ) ) {
		return;
	}

	const path = '/read/feed';
	return http(
		{
			path,
			method: 'GET',
			apiVersion: '1.1',
			query: {
				q: action.payload.query,
				offset: action.payload.offset,
				exclude_followed: action.payload.excludeFollowed,
				sort: action.payload.sort,
			},
		},
		action
	);
}

export function receiveReadFeedSearchSuccess( action, data ) {
	const { feeds, total } = data;
	return receiveFeedSearch( queryKey( action.payload ), feeds, total );
}

export function receiveReadFeedSearchError( action ) {
	const errorText = translate( 'Could not get results for query: %(query)s', {
		args: { query: truncate( action.payload.query, { length: 50 } ) },
	} );

	return errorNotice( errorText );
}

export function requestReadFeed( action ) {
	return http(
		{
			apiVersion: '1.1',
			method: 'GET',
			path: `/read/feed/${ encodeURIComponent( action.payload.ID ) }`,
			retryPolicy: noRetry(),
		},
		action
	);
}

export function receiveReadFeedSuccess( action, response ) {
	return receiveReaderFeedRequestSuccess( response );
}

export function receiveReadFeedError( action, response ) {
	return receiveReaderFeedRequestFailure( action.payload.ID, response );
}

registerHandlers( 'state/data-layer/wpcom/read/feed/index.js', {
	[ READER_FEED_SEARCH_REQUEST ]: [
		dispatchRequest( {
			fetch: requestReadFeedSearch,
			onSuccess: receiveReadFeedSearchSuccess,
			onError: receiveReadFeedSearchError,
			fromApi,
		} ),
	],

	[ READER_FEED_REQUEST ]: [
		dispatchRequest( {
			fetch: requestReadFeed,
			onSuccess: receiveReadFeedSuccess,
			onError: receiveReadFeedError,
		} ),
	],
} );