2023-01-17 18:04:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { apiFetch } from '@wordpress/data-controls';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-01-19 11:33:51 +00:00
|
|
|
import {
|
2023-01-31 16:52:35 +00:00
|
|
|
receiveRegisteredChannelsSuccess,
|
|
|
|
receiveRegisteredChannelsError,
|
2023-01-19 11:33:51 +00:00
|
|
|
receiveRecommendedChannelsSuccess,
|
|
|
|
receiveRecommendedChannelsError,
|
2023-02-01 11:02:03 +00:00
|
|
|
receiveCampaignsSuccess,
|
|
|
|
receiveCampaignsError,
|
2023-01-19 11:33:51 +00:00
|
|
|
} from './actions';
|
2023-02-01 11:02:03 +00:00
|
|
|
import { RegisteredChannel, RecommendedChannel, Campaign } from './types';
|
2023-01-17 18:04:42 +00:00
|
|
|
import { API_NAMESPACE } from './constants';
|
|
|
|
import { isApiFetchError } from './guards';
|
|
|
|
|
2023-01-31 16:52:35 +00:00
|
|
|
export function* getRegisteredChannels() {
|
2023-01-17 18:04:42 +00:00
|
|
|
try {
|
2023-01-31 16:52:35 +00:00
|
|
|
const data: RegisteredChannel[] = yield apiFetch( {
|
2023-01-17 18:04:42 +00:00
|
|
|
path: `${ API_NAMESPACE }/channels`,
|
|
|
|
} );
|
|
|
|
|
2023-01-31 16:52:35 +00:00
|
|
|
yield receiveRegisteredChannelsSuccess( data );
|
2023-01-17 18:04:42 +00:00
|
|
|
} catch ( error ) {
|
|
|
|
if ( isApiFetchError( error ) ) {
|
2023-01-31 16:52:35 +00:00
|
|
|
yield receiveRegisteredChannelsError( error );
|
2023-01-17 18:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2023-01-19 11:33:51 +00:00
|
|
|
|
|
|
|
export function* getRecommendedChannels() {
|
|
|
|
try {
|
2023-01-20 18:14:36 +00:00
|
|
|
const data: RecommendedChannel[] = yield apiFetch( {
|
2023-01-19 11:33:51 +00:00
|
|
|
path: `${ API_NAMESPACE }/recommendations?category=channels`,
|
|
|
|
} );
|
|
|
|
|
|
|
|
yield receiveRecommendedChannelsSuccess( data );
|
|
|
|
} catch ( error ) {
|
|
|
|
if ( isApiFetchError( error ) ) {
|
|
|
|
yield receiveRecommendedChannelsError( error );
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 11:02:03 +00:00
|
|
|
|
|
|
|
export function* getCampaigns() {
|
2023-02-01 17:03:15 +00:00
|
|
|
/**
|
|
|
|
* Page size.
|
|
|
|
*
|
|
|
|
* We set this to 100 because this is the maximum limit allowed by the API.
|
|
|
|
*
|
|
|
|
* We need this to support pagination in the UI.
|
|
|
|
* Currently API does not return total number of rows,
|
|
|
|
* so we use 100 to get "all" the rows.
|
|
|
|
*/
|
|
|
|
const perPage = 100;
|
|
|
|
|
2023-02-01 11:02:03 +00:00
|
|
|
try {
|
|
|
|
const data: Array< Campaign > = yield apiFetch( {
|
2023-02-01 17:03:15 +00:00
|
|
|
path: `${ API_NAMESPACE }/campaigns?per_page=${ perPage }`,
|
2023-02-01 11:02:03 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
yield receiveCampaignsSuccess( data );
|
|
|
|
} catch ( error ) {
|
|
|
|
if ( isApiFetchError( error ) ) {
|
|
|
|
yield receiveCampaignsError( error );
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|