From 5752bd7a9f140dbe88be41542d58dfcde0d7847f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 5 Jan 2024 17:56:51 +0100 Subject: [PATCH] Fix CYS updateStorePatters to use the new images endpoint structure (#43285) * Return the correct empty images response * Use the correct structure in the images endpoint response * Add changefile(s) from automation for the following project(s): woocommerce * Throw error when no images and the store is already ai generated --------- Co-authored-by: github-actions --- .../customize-store/design-with-ai/services.ts | 17 +++++++++++++++-- .../changelog/43285-fix-cys-images-endpoint-bug | 4 ++++ .../src/StoreApi/Routes/V1/AI/Images.php | 5 ++++- 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce/changelog/43285-fix-cys-images-endpoint-bug diff --git a/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts b/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts index a00880843f6..b5c2f72cdba 100644 --- a/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts +++ b/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts @@ -238,7 +238,7 @@ export const updateStorePatterns = async ( const { images } = await apiFetch< { ai_content_generated: boolean; - images: Array< unknown >; + images: { images: Array< unknown >; search_term: string }; } >( { path: '/wc/private/ai/images', method: 'POST', @@ -248,7 +248,20 @@ export const updateStorePatterns = async ( }, } ); - if ( ! images.length ) { + const { is_ai_generated } = await apiFetch< { + is_ai_generated: boolean; + } >( { + path: '/wc/private/ai/store-info', + method: 'GET', + } ); + + if ( ! images.images.length ) { + if ( is_ai_generated ) { + throw new Error( + 'AI content not generated: images not available' + ); + } + await resetPatternsAndProducts()(); return; } diff --git a/plugins/woocommerce/changelog/43285-fix-cys-images-endpoint-bug b/plugins/woocommerce/changelog/43285-fix-cys-images-endpoint-bug new file mode 100644 index 00000000000..420ef6dc707 --- /dev/null +++ b/plugins/woocommerce/changelog/43285-fix-cys-images-endpoint-bug @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Use the new AI images endpoint response structure. \ No newline at end of file diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Images.php b/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Images.php index 4c482715882..8ec4a48bbaa 100644 --- a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Images.php +++ b/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Images.php @@ -102,7 +102,10 @@ class Images extends AbstractRoute { $images = ( new Pexels() )->get_images( $ai_connection, $token, $business_description ); if ( is_wp_error( $images ) ) { - $images = []; + $images = array( + 'images' => array(), + 'search_term' => '', + ); } return rest_ensure_response(