Skip to content

Commit

Permalink
Check for existing font family using GET /font-families?slug=.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Jan 16, 2024
1 parent ac9f29d commit 6648afc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
* Internal dependencies
*/
import {
fetchGetFontFamilyBySlug,
fetchInstallFontFamily,
fetchUninstallFonts,
fetchFontCollections,
Expand Down Expand Up @@ -203,9 +204,13 @@ function FontLibraryProvider( { children } ) {
setIsInstalling( true );
try {
// Get the ID of the font family post, if it is already installed.
let fontFamilyId = libraryPosts.filter(
( post ) => post.font_family_settings.slug === font.slug
)[ 0 ]?.id;
let fontFamilyId = await fetchGetFontFamilyBySlug( font.slug )
.then( ( response ) => response?.[ 0 ]?.id )
.catch( ( e ) => {
// eslint-disable-next-line no-console
console.error( e );
return null;
} );

// Otherwise, install it.
if ( ! fontFamilyId ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export async function fetchInstallFontFace( fontFamilyId, data ) {
return apiFetch( config );
}

export async function fetchGetFontFamilyBySlug( slug ) {
const config = {
path: `/wp/v2/font-families?slug=${ slug }`,
method: 'GET',
};
return apiFetch( config );
}

export async function fetchUninstallFonts( fonts ) {
const data = {
font_families: fonts,
Expand Down

0 comments on commit 6648afc

Please sign in to comment.