Skip to content

Commit

Permalink
Jetpack Manage: Implement a modal for the "Get Score" action for Boost (
Browse files Browse the repository at this point in the history
#82239)

* Update LicenseLightbox to render the extraContent

* Create a new component to display the license info modal for reusability

* Create use-request-boost-score-mutation.ts

* Create use-install-plugin-mutation.ts

* Create use-install-boost.tsx

* Create BoostLicenseInfoModal and use it to show boost license info modal

* Fix test cases

* Address PR comments

* Remove useRequestBoostScore as it is no longer required

* Add notice with a link to Jetpack Boost

* Address PR comments
  • Loading branch information
yashwin authored and pull[bot] committed Jan 30, 2024
1 parent 6c49c61 commit 2411956
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { useQueryClient } from '@tanstack/react-query';
import { useTranslate } from 'i18n-calypso';
import { useEffect, useCallback } from 'react';
import { useDispatch } from 'calypso/state';
import useInstallPluginMutation from 'calypso/state/jetpack-agency-dashboard/hooks/use-install-plugin-mutation';
import { successNotice, errorNotice } from 'calypso/state/notices/actions';
import type { Site } from '../types';

export default function useInstallBoost(
siteId: number,
siteUrl: string,
queryKey: any[]
): {
installBoost: () => void;
status: string;
} {
const translate = useTranslate();
const dispatch = useDispatch();
const queryClient = useQueryClient();

const handleUpdateSites = useCallback( async () => {
// Cancel any current refetches, so they don't overwrite our update
await queryClient.cancelQueries( queryKey );

// Update to the new value
queryClient.setQueryData( queryKey, ( oldSites: any ) => {
return {
...oldSites,
sites: oldSites?.sites.map( ( site: Site ) => {
if ( site.blog_id === siteId ) {
return {
...site,
has_pending_boost_one_time_score: true,
};
}
return site;
} ),
};
} );
}, [ queryClient, queryKey, siteId ] );

const { mutate: installPlugin, status } = useInstallPluginMutation( {
retry: false,
} );

const installBoost = () => {
installPlugin( {
site_id: siteId,
plugin_slug: 'jetpack_boost',
} );
};

useEffect( () => {
if ( status === 'success' ) {
dispatch(
successNotice(
translate(
'Jetpack Boost was successfully added to {{em}}%(siteUrl)s{{/em}}. Please allow a few minutes for performance score calculation.',
{
args: { siteUrl },
comment: '%(siteUrl)s is the site url. Eg: example.com',
components: {
em: <em />,
},
}
)
)
);
handleUpdateSites();
}
if ( status === 'error' ) {
dispatch(
errorNotice( translate( 'Something went wrong while installing Boost. Please try again.' ) )
);
}
}, [ dispatch, handleUpdateSites, status, siteUrl, translate ] );

return {
installBoost,
status,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useMobileBreakpoint } from '@automattic/viewport-react';
import { useTranslate } from 'i18n-calypso';
import page from 'page';
import { useContext, useMemo } from 'react';
import LicenseLightbox from 'calypso/jetpack-cloud/sections/partner-portal/license-lightbox';
import { addQueryArgs } from 'calypso/lib/url';
import useJetpackAgencyDashboardRecordTrackEvent from '../../hooks/use-jetpack-agency-dashboard-record-track-event';
import SitesOverviewContext from '../context';
import DashboardDataContext from '../dashboard-data-context';
import { DASHBOARD_PRODUCT_SLUGS_BY_TYPE } from '../lib/constants';

interface Props {
label?: string;
currentLicenseInfo: string | null;
onClose?: () => void;
siteId?: number;
extraAsideContent?: JSX.Element;
isDisabled?: boolean;
onCtaClick?: () => void;
}

export default function LicenseInfoModal( {
label,
currentLicenseInfo,
onClose,
siteId,
extraAsideContent,
isDisabled,
onCtaClick,
}: Props ) {
const isMobile = useMobileBreakpoint();
const translate = useTranslate();

const { hideLicenseInfo } = useContext( SitesOverviewContext );

const { products } = useContext( DashboardDataContext );
const recordEvent = useJetpackAgencyDashboardRecordTrackEvent( null, ! isMobile );

const currentLicenseProductSlug = currentLicenseInfo
? DASHBOARD_PRODUCT_SLUGS_BY_TYPE[ currentLicenseInfo ]
: null;

const currentLicenseProduct = useMemo( () => {
return currentLicenseProductSlug && products
? products.find( ( product ) => product.slug === currentLicenseProductSlug )
: null;
}, [ currentLicenseProductSlug, products ] );

const onHideLicenseInfo = () => {
hideLicenseInfo();
onClose?.();
};

const onIssueLicense = () => {
if ( ! currentLicenseProductSlug ) {
return;
}
recordEvent( 'issue_license_info', {
product: currentLicenseProductSlug,
} );
onCtaClick?.();
onHideLicenseInfo();
page(
addQueryArgs(
{
product_slug: currentLicenseProductSlug,
source: 'dashboard',
site_id: siteId,
},
'/partner-portal/issue-license/'
)
);
};

return (
currentLicenseProduct && (
<LicenseLightbox
product={ currentLicenseProduct }
ctaLabel={ label ?? translate( 'Issue License' ) }
isDisabled={ isDisabled }
onActivate={ onIssueLicense }
onClose={ onHideLicenseInfo }
extraAsideContent={ extraAsideContent }
/>
)
);
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { Card } from '@automattic/components';
import { useMobileBreakpoint } from '@automattic/viewport-react';
import { useTranslate } from 'i18n-calypso';
import page from 'page';
import { useContext, forwardRef, useMemo } from 'react';
import { useContext, forwardRef } from 'react';
import Pagination from 'calypso/components/pagination';
import LicenseLightbox from 'calypso/jetpack-cloud/sections/partner-portal/license-lightbox';
import TextPlaceholder from 'calypso/jetpack-cloud/sections/partner-portal/text-placeholder';
import { addQueryArgs } from 'calypso/lib/url';
import EditButton from '../../dashboard-bulk-actions/edit-button';
import useJetpackAgencyDashboardRecordTrackEvent from '../../hooks/use-jetpack-agency-dashboard-record-track-event';
import DashboardDataContext from '../../sites-overview/dashboard-data-context';
import SitesOverviewContext from '../context';
import useDefaultSiteColumns from '../hooks/use-default-site-columns';
import { DASHBOARD_PRODUCT_SLUGS_BY_TYPE } from '../lib/constants';
import LicenseInfoModal from '../license-info-modal';
import SiteBulkSelect from '../site-bulk-select';
import SiteCard from '../site-card';
import SiteSort from '../site-sort';
import SiteTable from '../site-table';
import { Site } from '../types';
import useFormattedSites from './hooks/use-formatted-sites';

import './style.scss';

const addPageArgs = ( pageNumber: number ) => {
Expand All @@ -39,13 +37,9 @@ interface Props {
const SiteContent = ( { data, isLoading, currentPage, isFavoritesTab }: Props, ref: any ) => {
const isMobile = useMobileBreakpoint();

const translate = useTranslate();

const { isBulkManagementActive, currentLicenseInfo, hideLicenseInfo } =
useContext( SitesOverviewContext );
const { isBulkManagementActive, currentLicenseInfo } = useContext( SitesOverviewContext );

const { products, isLargeScreen } = useContext( DashboardDataContext );
const recordEvent = useJetpackAgencyDashboardRecordTrackEvent( null, ! isMobile );
const { isLargeScreen } = useContext( DashboardDataContext );

const sites = useFormattedSites( data?.sites ?? [] );

Expand All @@ -56,38 +50,6 @@ const SiteContent = ( { data, isLoading, currentPage, isFavoritesTab }: Props, r
const siteColumns = useDefaultSiteColumns();
const firstColumn = siteColumns[ 0 ];

const currentLicenseProductSlug = currentLicenseInfo
? DASHBOARD_PRODUCT_SLUGS_BY_TYPE[ currentLicenseInfo ]
: null;

const currentLicenseProduct = useMemo( () => {
return currentLicenseProductSlug && products
? products.find( ( product ) => product.slug === currentLicenseProductSlug )
: null;
}, [ currentLicenseProductSlug, products ] );

const onIssueLicense = () => {
if ( currentLicenseProductSlug ) {
recordEvent( 'issue_license_info', {
product: currentLicenseProductSlug,
} );
hideLicenseInfo();
page(
addQueryArgs(
{
product_slug: currentLicenseProductSlug,
source: 'dashboard',
},
'/partner-portal/issue-license/'
)
);
}
};

const onHideLicenseInfo = () => {
hideLicenseInfo();
};

return (
<>
{ isLargeScreen ? (
Expand Down Expand Up @@ -126,7 +88,6 @@ const SiteContent = ( { data, isLoading, currentPage, isFavoritesTab }: Props, r
</div>
</div>
) }

{ data && data?.total > 0 && (
<Pagination
compact={ isMobile }
Expand All @@ -136,15 +97,7 @@ const SiteContent = ( { data, isLoading, currentPage, isFavoritesTab }: Props, r
pageClick={ handlePageClick }
/>
) }

{ currentLicenseProduct && (
<LicenseLightbox
product={ currentLicenseProduct }
ctaLabel={ translate( 'Issue License' ) }
onActivate={ onIssueLicense }
onClose={ onHideLicenseInfo }
/>
) }
{ currentLicenseInfo && <LicenseInfoModal currentLicenseInfo={ currentLicenseInfo } /> }
</>
);
};
Expand Down
Loading

0 comments on commit 2411956

Please sign in to comment.