File size: 1,826 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import page from '@automattic/calypso-router';
import { useLaunchpad, updateLaunchpadSettings } from '@automattic/data-stores';
import { useI18n } from '@wordpress/react-i18n';
import { useCallback, useMemo } from 'react';
import { useDispatch, useSelector } from 'calypso/state';
import { successNotice } from 'calypso/state/notices/actions';
import { NoticeOptions } from 'calypso/state/notices/types';
import { getSelectedSiteSlug } from 'calypso/state/ui/selectors';
export const useCompleteLaunchpadTasksWithNotice = ( { notifyIfNothingChanged = true } = {} ) => {
const siteSlug = useSelector( getSelectedSiteSlug );
const dispatch = useDispatch();
const { __ } = useI18n();
const { data, refetch } = useLaunchpad( siteSlug );
const incompleteTasks = useMemo( () => {
return data?.checklist?.filter( ( task ) => ! task.completed ).map( ( task ) => task.id ) ?? [];
}, [ data ] );
const completeTasks = useCallback(
async ( taskSlugs: string[], noticeText: string, noticeSettings: NoticeOptions ) => {
const tasksToComplete = incompleteTasks?.filter( ( taskSlug ) =>
taskSlugs.includes( taskSlug )
);
if ( tasksToComplete.length === 0 ) {
if ( notifyIfNothingChanged ) {
dispatch( successNotice( noticeText, noticeSettings ) );
}
return;
}
dispatch(
successNotice( noticeText, {
...noticeSettings,
button: __( 'Next steps' ),
onClick: () => page( `/home/${ siteSlug }` ),
} )
);
await updateLaunchpadSettings( siteSlug, {
checklist_statuses: tasksToComplete.reduce(
( acc, taskSlug ) => {
acc[ taskSlug ] = true;
return acc;
},
{} as Record< string, boolean >
),
} );
refetch();
},
[ incompleteTasks, siteSlug, __, refetch, dispatch, notifyIfNothingChanged ]
);
return completeTasks;
};
|