Back

100% Statements 5/5
100% Branches 0/0
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24                        5x   5x   5x 1x 1x          
import { useQueryClient } from '@tanstack/react-query';
import { useEffect } from 'react';
import { QUERY_PRODUCT_BY_OWNERSHIP_KEY, QUERY_PRODUCT_KEY } from '../constants';
import { onSwitchWritten } from '../switch-written';
 
/**
 * Refetch the products and their ownership whenever a feature switch is written.
 *
 * A switch on the Features tab changes a product's status, and the Overview cards
 * would otherwise keep the one cached at page load.
 */
export function useRefreshProductsOnSwitch() {
	const queryClient = useQueryClient();
 
	useEffect(
		() =>
			onSwitchWritten( () => {
				queryClient.invalidateQueries( { queryKey: [ QUERY_PRODUCT_KEY ] } );
				queryClient.invalidateQueries( { queryKey: [ QUERY_PRODUCT_BY_OWNERSHIP_KEY ] } );
			} ),
		[ queryClient ]
	);
}