From 37f31520f905a6450e756c2f8003474f92a51880 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 18 Jan 2024 11:52:26 +0000 Subject: [PATCH 1/4] Wrap error messages in sprintf --- .../global-styles/font-library-modal/context.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/context.js b/packages/edit-site/src/components/global-styles/font-library-modal/context.js index c653d0a8b0362..4c305ef8e7e60 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/context.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/context.js @@ -9,7 +9,7 @@ import { useEntityRecords, store as coreStore, } from '@wordpress/core-data'; -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies @@ -265,8 +265,11 @@ function FontLibraryProvider( { children } ) { alreadyInstalledFontFaces.length === 0 ) { throw new Error( - __( 'No font faces were installed. ' ) + + sprintf( + /* translators: %s: Specific error message returned from server. */ + __( 'No font faces were installed. %s' ), detailedErrorMessage + ) ); } @@ -289,9 +292,13 @@ function FontLibraryProvider( { children } ) { if ( unsucessfullyInstalledFontFaces.length > 0 ) { throw new Error( - __( - 'Some font faces were installed. There were some errors. ' - ) + detailedErrorMessage + sprintf( + /* translators: %s: Specific error message returned from server. */ + __( + 'Some font faces were installed. There were some errors. %s' + ), + detailedErrorMessage + ) ); } } finally { From 1d01919c0563e94f490c8e0d053b6db4f763f113 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 18 Jan 2024 12:00:43 +0000 Subject: [PATCH 2/4] Use await rather than then --- .../font-library-modal/resolvers.js | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js index baaa698599553..a6a56f2ccc728 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js @@ -13,13 +13,12 @@ export async function fetchInstallFontFamily( data ) { method: 'POST', body: data, }; - return apiFetch( config ).then( ( response ) => { - return { - id: response.id, - ...response.font_face_settings, - fontFace: [], - }; - } ); + const response = await apiFetch( config ); + return { + id: response.id, + ...response.font_family_settings, + fontFace: [], + }; } export async function fetchInstallFontFace( fontFamilyId, data ) { @@ -28,12 +27,11 @@ export async function fetchInstallFontFace( fontFamilyId, data ) { method: 'POST', body: data, }; - return apiFetch( config ).then( ( response ) => { - return { - id: response.id, - ...response.font_face_settings, - }; - } ); + const response = await apiFetch( config ); + return { + id: response.id, + ...response.font_face_settings, + }; } export async function fetchGetFontFamilyBySlug( slug ) { @@ -41,20 +39,19 @@ export async function fetchGetFontFamilyBySlug( slug ) { path: `/wp/v2/font-families?slug=${ slug }&_embed=true`, method: 'GET', }; - return apiFetch( config ).then( ( response ) => { - if ( ! response || response.length === 0 ) { - return null; - } - const fontFamilyPost = response[ 0 ]; - return { - id: fontFamilyPost.id, - ...fontFamilyPost.font_family_settings, - fontFace: - fontFamilyPost?._embedded?.font_faces.map( - ( face ) => face.font_face_settings - ) || [], - }; - } ); + const response = await apiFetch( config ); + if ( ! response || response.length === 0 ) { + return null; + } + const fontFamilyPost = response[ 0 ]; + return { + id: fontFamilyPost.id, + ...fontFamilyPost.font_family_settings, + fontFace: + fontFamilyPost?._embedded?.font_faces.map( + ( face ) => face.font_face_settings + ) || [], + }; } export async function fetchUninstallFontFamily( fontFamilyId ) { @@ -62,7 +59,7 @@ export async function fetchUninstallFontFamily( fontFamilyId ) { path: `/wp/v2/font-families/${ fontFamilyId }?force=true`, method: 'DELETE', }; - return apiFetch( config ); + return await apiFetch( config ); } export async function fetchFontCollections() { @@ -70,7 +67,7 @@ export async function fetchFontCollections() { path: '/wp/v2/font-collections', method: 'GET', }; - return apiFetch( config ); + return await apiFetch( config ); } export async function fetchFontCollection( id ) { @@ -78,5 +75,5 @@ export async function fetchFontCollection( id ) { path: `/wp/v2/font-collections/${ id }`, method: 'GET', }; - return apiFetch( config ); + return await apiFetch( config ); } From fa073653b6cda19afc0e6e8a7b5fb2035ffb8b9b Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 18 Jan 2024 12:02:27 +0000 Subject: [PATCH 3/4] Add variables for API URLs --- .../global-styles/font-library-modal/resolvers.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js index a6a56f2ccc728..bb387ca3510d6 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js @@ -7,9 +7,12 @@ */ import apiFetch from '@wordpress/api-fetch'; +const FONT_FAMILIES_URL = '/wp/v2/font-families'; +const FONT_COLLECTIONS_URL = '/wp/v2/font-collections'; + export async function fetchInstallFontFamily( data ) { const config = { - path: '/wp/v2/font-families', + path: FONT_FAMILIES_URL, method: 'POST', body: data, }; @@ -23,7 +26,7 @@ export async function fetchInstallFontFamily( data ) { export async function fetchInstallFontFace( fontFamilyId, data ) { const config = { - path: `/wp/v2/font-families/${ fontFamilyId }/font-faces`, + path: `${ FONT_FAMILIES_URL }/${ fontFamilyId }/font-faces`, method: 'POST', body: data, }; @@ -36,7 +39,7 @@ export async function fetchInstallFontFace( fontFamilyId, data ) { export async function fetchGetFontFamilyBySlug( slug ) { const config = { - path: `/wp/v2/font-families?slug=${ slug }&_embed=true`, + path: `${ FONT_FAMILIES_URL }s?slug=${ slug }&_embed=true`, method: 'GET', }; const response = await apiFetch( config ); @@ -56,7 +59,7 @@ export async function fetchGetFontFamilyBySlug( slug ) { export async function fetchUninstallFontFamily( fontFamilyId ) { const config = { - path: `/wp/v2/font-families/${ fontFamilyId }?force=true`, + path: `${ FONT_FAMILIES_URL }/${ fontFamilyId }?force=true`, method: 'DELETE', }; return await apiFetch( config ); @@ -64,7 +67,7 @@ export async function fetchUninstallFontFamily( fontFamilyId ) { export async function fetchFontCollections() { const config = { - path: '/wp/v2/font-collections', + path: FONT_COLLECTIONS_URL, method: 'GET', }; return await apiFetch( config ); @@ -72,7 +75,7 @@ export async function fetchFontCollections() { export async function fetchFontCollection( id ) { const config = { - path: `/wp/v2/font-collections/${ id }`, + path: `${ FONT_COLLECTIONS_URL }/${ id }`, method: 'GET', }; return await apiFetch( config ); From 153e1f844b9980cdb2cc0e1f00f58a81eee3196e Mon Sep 17 00:00:00 2001 From: Sarah Norris <1645628+mikachan@users.noreply.github.com> Date: Thu, 18 Jan 2024 13:59:50 +0000 Subject: [PATCH 4/4] Update packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js Co-authored-by: Jeff Ong --- .../components/global-styles/font-library-modal/resolvers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js index bb387ca3510d6..6dcfe85118be2 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js @@ -39,7 +39,7 @@ export async function fetchInstallFontFace( fontFamilyId, data ) { export async function fetchGetFontFamilyBySlug( slug ) { const config = { - path: `${ FONT_FAMILIES_URL }s?slug=${ slug }&_embed=true`, + path: `${ FONT_FAMILIES_URL }?slug=${ slug }&_embed=true`, method: 'GET', }; const response = await apiFetch( config );