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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 32x 32x 27x 32x 5x 32x 1x 32x 10x 32x 2x 32x 2x 32x 79x 79x 12x 12x 10x 5x 10x 10x | import { useMutation, useQueryClient } from '@tanstack/react-query';
import apiFetch from '@wordpress/api-fetch';
import { privacyStringToInt } from '../utils/library-item';
import { recordVideoPressPrivacyChange } from '../utils/privacy-analytics';
import { LIBRARY_ITEM_QUERY_SEGMENT, LIBRARY_QUERY_KEY } from './use-library';
import type { LibraryItemPrivacy, VideoDetailsPatch } from '../types/library';
type ApiPatch = {
title?: string;
description?: string;
caption?: string;
rating?: string;
display_embed?: boolean;
allow_download?: boolean;
privacy_setting?: 0 | 1 | 2;
};
/**
* Convert a UI VideoDetailsPatch to the shape expected by the wpcom/v2/videopress/meta endpoint.
*
* @param patch - The partial UI-layer patch object with camelCase field names.
* @return A plain object with snake_case API field names, omitting undefined keys.
*/
export function patchToApi( patch: Partial< VideoDetailsPatch > ): ApiPatch {
const out: ApiPatch = {};
if ( patch.title !== undefined ) {
out.title = patch.title;
}
if ( patch.description !== undefined ) {
out.description = patch.description;
}
if ( patch.rating !== undefined ) {
out.rating = patch.rating;
}
if ( patch.privacy !== undefined ) {
out.privacy_setting = privacyStringToInt( patch.privacy );
}
if ( patch.displayEmbed !== undefined ) {
out.display_embed = patch.displayEmbed;
}
if ( patch.allowDownloads !== undefined ) {
out.allow_download = patch.allowDownloads;
}
return out;
}
/**
* Return a mutation that POSTs video meta updates to wpcom/v2/videopress/meta and
* invalidates the library cache on success.
*
* @return A TanStack Query mutation object with a mutateAsync method.
*/
export function useUpdateVideoMeta() {
const client = useQueryClient();
return useMutation<
void,
Error,
{
id: number | string;
patch: Partial< VideoDetailsPatch >;
previousPrivacy?: LibraryItemPrivacy;
}
>( {
mutationFn: async ( { id, patch, previousPrivacy } ) => {
const privacy = patch.privacy;
const response = await apiFetch< { code?: string } >( {
path: '/wpcom/v2/videopress/meta',
method: 'POST',
data: { id: Number( id ), ...patchToApi( patch ) },
} );
if ( response?.code === 'success' ) {
recordVideoPressPrivacyChange( previousPrivacy, privacy, false );
}
},
onSuccess: ( _data, { id } ) => {
client.invalidateQueries( { queryKey: [ LIBRARY_QUERY_KEY ] } );
client.invalidateQueries( {
queryKey: [ LIBRARY_QUERY_KEY, LIBRARY_ITEM_QUERY_SEGMENT, String( id ) ],
} );
},
} );
}
|