Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
* External dependencies
*/
import { Modal, __experimentalVStack as VStack } from '@wordpress/components'; // eslint-disable-line @wordpress/no-unsafe-wp-apis
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { INTEGRATIONS_STORE } from '../../../store/integrations';
import CSVExport from '../../inbox/export-responses/csv';
import GoogleDriveExport from '../../inbox/export-responses/google-drive';
import type { SelectIntegrations } from '../../../store/integrations';
import type { Integration } from '../../../types';

type ExportResponsesModalProps = {
onRequestClose: () => void;
Expand All @@ -20,6 +24,16 @@ const ExportResponsesModal = ( {
onExport,
autoConnectGdrive,
}: ExportResponsesModalProps ) => {
const { integrations } = useSelect( ( select: SelectIntegrations ) => {
const store = select( INTEGRATIONS_STORE );
return {
integrations: store.getIntegrations() || [],
};
}, [] ) as { integrations: Integration[] };

const isGoogleDriveEnabled = integrations.some(
integration => integration.id === 'google-drive'
);
return (
<Modal
title={ __( 'Export responses', 'jetpack-forms' ) }
Expand All @@ -28,7 +42,9 @@ const ExportResponsesModal = ( {
>
<VStack spacing={ 8 }>
<CSVExport onExport={ onExport } />
<GoogleDriveExport onExport={ onExport } autoConnect={ autoConnectGdrive } />
{ isGoogleDriveEnabled && (
<GoogleDriveExport onExport={ onExport } autoConnect={ autoConnectGdrive } />
) }
</VStack>
</Modal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ import { isSimpleSite } from '@automattic/jetpack-script-data';
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import requestExternalAccess from '@automattic/request-external-access';
import { Button, Path, Spinner, SVG } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useCallback, useRef, useState } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';
import clsx from 'clsx';
/**
* Internal dependencies
*/
import { config } from '../..';
import { useIntegrationStatus } from '../../../blocks/contact-form/components/jetpack-integrations-modal/hooks/use-integration-status';
import { INTEGRATIONS_STORE } from '../../../store/integrations';
import { PARTIAL_RESPONSES_PATH } from '../../../util/get-preferred-responses-view';
/**
* Internal dependencies
*/
import type { SelectIntegrations, IntegrationsDispatch } from '../../../store/integrations';
import type { Integration } from '../../../types';

const GoogleDriveExport = ( { onExport, autoConnect = false } ) => {
const [ isExporting, setIsExporting ] = useState( false );
const { integration, refreshStatus } = useIntegrationStatus( 'google-drive' );
const { integration } = useSelect( ( select: SelectIntegrations ) => {
const store = select( INTEGRATIONS_STORE );
const list = store.getIntegrations() || [];
return { integration: list.find( ( i: Integration ) => i.id === 'google-drive' ) };
}, [] ) as { integration?: Integration };
const { refreshIntegrations } = useDispatch( INTEGRATIONS_STORE ) as IntegrationsDispatch;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file had an extra check for if Google was enabled using the useIntegrationStatus hook. Going forward, we'll want to use the new integrations store for single integrations checks like this too. The store allows for immediate checks without an extra endpoint call. You just check if a particular integration slug exists in the list of integrations. I've updated this file to do that. It also now uses the refreshIntegrations() method from the store.

const isConnectedToGoogleDrive = !! integration?.isConnected;
const { tracks } = useAnalytics();
const autoConnectOpened = useRef( false );
Expand Down Expand Up @@ -58,12 +69,12 @@ const GoogleDriveExport = ( { onExport, autoConnect = false } ) => {
setIsTogglingConnection( true );
requestExternalAccess( integration?.settingsUrl, ( { keyring_id: keyringId } ) => {
if ( keyringId ) {
refreshStatus();
refreshIntegrations();
} else {
setIsTogglingConnection( false );
}
} );
}, [ tracks, integration?.settingsUrl, refreshStatus ] );
}, [ tracks, integration?.settingsUrl, refreshIntegrations ] );

if ( isOfflineMode ) {
return null;
Expand Down
Loading