2023-02-01 11:02:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
|
2022-12-19 12:54:01 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { Campaign } from '~/marketing/types';
|
2023-02-01 11:02:03 +00:00
|
|
|
import { STORE_KEY } from '~/marketing/data-multichannel/constants';
|
|
|
|
import {
|
2023-12-05 03:20:05 +00:00
|
|
|
CampaignsPagination,
|
2023-02-01 11:02:03 +00:00
|
|
|
Campaign as APICampaign,
|
|
|
|
ApiFetchError,
|
|
|
|
} from '~/marketing/data-multichannel/types';
|
|
|
|
import { useRegisteredChannels } from '~/marketing/hooks';
|
2022-12-19 12:54:01 +00:00
|
|
|
|
|
|
|
type UseCampaignsType = {
|
|
|
|
loading: boolean;
|
2023-02-01 11:02:03 +00:00
|
|
|
data?: Array< Campaign >;
|
|
|
|
error?: ApiFetchError;
|
2023-02-23 18:13:20 +00:00
|
|
|
meta?: {
|
|
|
|
total?: number;
|
|
|
|
};
|
2022-12-19 12:54:01 +00:00
|
|
|
};
|
|
|
|
|
2023-03-01 19:15:38 +00:00
|
|
|
/**
|
|
|
|
* Custom hook to get campaigns.
|
|
|
|
*
|
2023-08-29 22:40:45 +00:00
|
|
|
* @param page Page number. Default is `1`.
|
|
|
|
* @param perPage Page size, i.e. number of records in one page. Default is `5`.
|
2023-03-01 19:15:38 +00:00
|
|
|
*/
|
2023-03-19 15:17:22 +00:00
|
|
|
export const useCampaigns = ( page = 1, perPage = 5 ): UseCampaignsType => {
|
2023-02-23 18:13:20 +00:00
|
|
|
const { data: channels } = useRegisteredChannels();
|
2022-12-17 13:08:14 +00:00
|
|
|
|
2023-02-23 18:13:20 +00:00
|
|
|
return useSelect(
|
|
|
|
( select ) => {
|
|
|
|
const { hasFinishedResolution, getCampaigns } = select( STORE_KEY );
|
2023-12-05 03:20:05 +00:00
|
|
|
const { campaignsPage, meta } = getCampaigns< CampaignsPagination >(
|
2023-02-23 18:13:20 +00:00
|
|
|
page,
|
|
|
|
perPage
|
2023-02-01 11:02:03 +00:00
|
|
|
);
|
|
|
|
|
2023-02-23 18:13:20 +00:00
|
|
|
const convert = ( campaign: APICampaign ): Campaign => {
|
|
|
|
const channel = channels?.find(
|
|
|
|
( el ) => el.slug === campaign.channel
|
|
|
|
);
|
|
|
|
|
2024-03-18 17:56:08 +00:00
|
|
|
const cost = campaign.cost ? campaign.cost.formatted : '-';
|
|
|
|
const sales = campaign.sales ? campaign.sales.formatted : '-';
|
2023-11-02 08:28:39 +00:00
|
|
|
|
2023-02-23 18:13:20 +00:00
|
|
|
return {
|
|
|
|
id: `${ campaign.channel }|${ campaign.id }`,
|
|
|
|
title: campaign.title,
|
|
|
|
description: '',
|
2023-11-02 14:19:03 +00:00
|
|
|
cost,
|
2024-01-18 17:42:35 +00:00
|
|
|
sales,
|
2023-02-23 18:13:20 +00:00
|
|
|
manageUrl: campaign.manage_url,
|
|
|
|
icon: channel?.icon || '',
|
|
|
|
channelName: channel?.title || '',
|
|
|
|
channelSlug: campaign.channel,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-01 11:02:03 +00:00
|
|
|
return {
|
2023-02-23 18:13:20 +00:00
|
|
|
loading: ! hasFinishedResolution( 'getCampaigns', [
|
|
|
|
page,
|
|
|
|
perPage,
|
|
|
|
] ),
|
2023-12-05 03:20:05 +00:00
|
|
|
data: campaignsPage?.data?.map( convert ),
|
|
|
|
error: campaignsPage?.error,
|
|
|
|
meta,
|
2023-02-01 11:02:03 +00:00
|
|
|
};
|
2023-02-23 18:13:20 +00:00
|
|
|
},
|
2023-12-05 03:20:05 +00:00
|
|
|
[ page, perPage, channels ]
|
2023-02-23 18:13:20 +00:00
|
|
|
);
|
2022-12-18 11:34:16 +00:00
|
|
|
};
|