woocommerce/plugins/woocommerce-admin/client/marketing/data-multichannel/actions.ts

106 lines
2.0 KiB
TypeScript
Raw Normal View History

/**
* Internal dependencies
*/
import { TYPES } from './action-types';
2023-03-02 16:54:21 +00:00
import { isApiFetchError } from './guards';
import {
ApiFetchError,
RegisteredChannel,
RecommendedChannel,
Campaign,
CampaignType,
} from './types';
export const receiveRegisteredChannelsSuccess = (
channels: Array< RegisteredChannel >
) => {
return {
type: TYPES.RECEIVE_REGISTERED_CHANNELS_SUCCESS,
payload: channels,
};
};
export const receiveRegisteredChannelsError = ( error: ApiFetchError ) => {
return {
type: TYPES.RECEIVE_REGISTERED_CHANNELS_ERROR,
payload: error,
error: true,
};
};
2023-01-19 11:33:51 +00:00
export const receiveRecommendedChannelsSuccess = (
2023-01-20 18:14:36 +00:00
channels: Array< RecommendedChannel >
2023-01-19 11:33:51 +00:00
) => {
return {
type: TYPES.RECEIVE_RECOMMENDED_CHANNELS_SUCCESS,
payload: channels,
};
};
export const receiveRecommendedChannelsError = ( error: ApiFetchError ) => {
return {
type: TYPES.RECEIVE_RECOMMENDED_CHANNELS_ERROR,
payload: error,
error: true,
};
};
type CampaignsSuccessResponse = {
2023-02-23 18:10:37 +00:00
payload: Array< Campaign >;
error: false;
2023-02-23 18:10:37 +00:00
meta: {
page: number;
perPage: number;
total?: number;
2023-02-23 18:10:37 +00:00
};
};
type CampaignsFailResponse = {
2023-02-24 10:32:33 +00:00
payload: ApiFetchError;
error: true;
2023-02-24 10:32:33 +00:00
meta: {
page: number;
perPage: number;
total?: number;
};
};
type CampaignsResponse = CampaignsSuccessResponse | CampaignsFailResponse;
2023-03-01 19:15:38 +00:00
/**
* Create a "RECEIVE_CAMPAIGNS" action object.
*/
export const receiveCampaigns = ( response: CampaignsResponse ) => {
return {
type: TYPES.RECEIVE_CAMPAIGNS,
...response,
};
};
export const receiveCampaignTypes = (
2023-03-02 16:54:21 +00:00
data: Array< CampaignType > | ApiFetchError
) => {
2023-03-02 16:54:21 +00:00
if ( isApiFetchError( data ) ) {
return {
type: TYPES.RECEIVE_CAMPAIGN_TYPES,
payload: data,
error: true as const,
};
}
return {
type: TYPES.RECEIVE_CAMPAIGN_TYPES,
2023-03-02 16:54:21 +00:00
payload: data,
error: false as const,
};
};
export type Action = ReturnType<
| typeof receiveRegisteredChannelsSuccess
| typeof receiveRegisteredChannelsError
2023-01-19 11:33:51 +00:00
| typeof receiveRecommendedChannelsSuccess
| typeof receiveRecommendedChannelsError
| typeof receiveCampaigns
| typeof receiveCampaignTypes
>;