2023-01-17 18:04:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-01-19 17:02:41 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2023-01-18 17:12:50 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2023-01-20 18:14:36 +00:00
|
|
|
import { useCallback } from '@wordpress/element';
|
2023-01-17 18:04:42 +00:00
|
|
|
|
2022-12-23 13:21:52 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-01-20 18:36:39 +00:00
|
|
|
import { RegisteredChannel, SyncStatusType } from '~/marketing/types';
|
2023-01-17 18:04:42 +00:00
|
|
|
import { STORE_KEY } from '~/marketing/data-multichannel/constants';
|
2023-01-18 17:32:38 +00:00
|
|
|
import {
|
|
|
|
ApiFetchError,
|
|
|
|
Channel,
|
2023-01-20 18:14:36 +00:00
|
|
|
ChannelsState,
|
2023-01-18 17:32:38 +00:00
|
|
|
} from '~/marketing/data-multichannel/types';
|
2022-12-23 13:21:52 +00:00
|
|
|
|
|
|
|
type UseRegisteredChannels = {
|
|
|
|
loading: boolean;
|
2023-01-20 18:36:39 +00:00
|
|
|
data?: Array< RegisteredChannel >;
|
2023-01-17 18:04:42 +00:00
|
|
|
error?: ApiFetchError;
|
2023-01-19 17:02:41 +00:00
|
|
|
refetch: () => void;
|
2022-12-23 13:21:52 +00:00
|
|
|
};
|
|
|
|
|
2023-01-20 18:26:19 +00:00
|
|
|
/**
|
|
|
|
* A object that maps the product listings status in
|
|
|
|
* plugins/woocommerce/src/Admin/Marketing/MarketingChannelInterface.php backend
|
|
|
|
* to SyncStatusType frontend.
|
|
|
|
*/
|
2023-01-18 17:32:38 +00:00
|
|
|
const statusMap: Record< string, SyncStatusType > = {
|
|
|
|
'sync-in-progress': 'syncing',
|
2023-01-20 18:26:19 +00:00
|
|
|
'sync-failed': 'failed',
|
|
|
|
synced: 'synced',
|
2023-01-18 17:32:38 +00:00
|
|
|
};
|
2023-01-18 17:12:50 +00:00
|
|
|
|
2023-01-20 18:36:39 +00:00
|
|
|
const convert = ( data: Channel ): RegisteredChannel => {
|
2023-01-18 17:12:50 +00:00
|
|
|
const issueType = data.errors_count >= 1 ? 'error' : 'none';
|
|
|
|
const issueText =
|
|
|
|
data.errors_count >= 1
|
|
|
|
? sprintf(
|
|
|
|
// translators: %d: The number of issues to resolve.
|
|
|
|
__( '%d issues to resolve', 'woocommerce' ),
|
|
|
|
data.errors_count
|
|
|
|
)
|
|
|
|
: __( 'No issues to resolve', 'woocommerce' );
|
|
|
|
|
2022-12-08 15:53:36 +00:00
|
|
|
return {
|
2023-01-17 18:04:42 +00:00
|
|
|
slug: data.slug,
|
|
|
|
title: data.name,
|
|
|
|
description: data.description,
|
|
|
|
icon: data.icon,
|
|
|
|
isSetupCompleted: data.is_setup_completed,
|
|
|
|
setupUrl: data.settings_url,
|
|
|
|
manageUrl: data.settings_url,
|
2023-01-18 17:32:38 +00:00
|
|
|
syncStatus: statusMap[ data.product_listings_status ],
|
2023-01-18 17:12:50 +00:00
|
|
|
issueType,
|
|
|
|
issueText,
|
2022-12-08 15:53:36 +00:00
|
|
|
};
|
|
|
|
};
|
2023-01-17 18:04:42 +00:00
|
|
|
|
|
|
|
export const useRegisteredChannels = (): UseRegisteredChannels => {
|
2023-01-19 17:02:41 +00:00
|
|
|
const { invalidateResolution } = useDispatch( STORE_KEY );
|
|
|
|
|
|
|
|
const refetch = useCallback( () => {
|
|
|
|
invalidateResolution( 'getChannels' );
|
|
|
|
}, [ invalidateResolution ] );
|
|
|
|
|
2023-01-17 18:04:42 +00:00
|
|
|
return useSelect( ( select ) => {
|
|
|
|
const { hasFinishedResolution, getChannels } = select( STORE_KEY );
|
2023-01-20 18:14:36 +00:00
|
|
|
const channels = getChannels< ChannelsState >();
|
2023-01-17 18:04:42 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
loading: ! hasFinishedResolution( 'getChannels' ),
|
2023-01-19 17:02:41 +00:00
|
|
|
data: channels.data?.map( convert ),
|
2023-01-17 18:04:42 +00:00
|
|
|
error: channels.error,
|
2023-01-19 17:02:41 +00:00
|
|
|
refetch,
|
2023-01-17 18:04:42 +00:00
|
|
|
};
|
|
|
|
} );
|
|
|
|
};
|