diff --git a/inc/class-api.php b/inc/class-api.php index 8d2488b..bd96ab3 100644 --- a/inc/class-api.php +++ b/inc/class-api.php @@ -401,16 +401,15 @@ public function images( \WP_REST_Request $request ) { /** * Get JSON from response. * - * @param array $data Response. + * @param array $data Response. * - * @return array|false + * @return array|false */ private static function process_json_from_response( $data ) { // Find the target item. $target = current( $data ); - if ( false === $target ) { - // Handle the case where the target is not found. + if ( false === $target || !isset($target->content) ) { return false; } @@ -419,7 +418,12 @@ private static function process_json_from_response( $data ) { try { $json_object = json_decode( $json_string, true ); - return $json_object; + + if ( is_array( $json_object ) ) { + return $json_object; + } + + return false; } catch ( \Exception $e ) { // If parsing failed, try to find a JSON array in the string. preg_match( '/\[(.|\n)*\]/', $json_string, $matches ); @@ -428,7 +432,9 @@ private static function process_json_from_response( $data ) { $json_array_string = $matches[0]; $json_object = json_decode( $json_array_string, true ); - return $json_object; + if ( is_array( $json_object ) ) { + return $json_object; + } } } diff --git a/src/parts/ColorPalette.js b/src/parts/ColorPalette.js index d6f90bf..d2ed9b1 100644 --- a/src/parts/ColorPalette.js +++ b/src/parts/ColorPalette.js @@ -129,6 +129,7 @@ const ColorPalette = () => {
{ palette.map( ( color ) => ( onChangeColor( e, color.slug ) } /> diff --git a/src/store.js b/src/store.js index 3c9b6d8..400d695 100644 --- a/src/store.js +++ b/src/store.js @@ -42,8 +42,8 @@ const DEFAULT_STATE = { activeImageKeyword: null, selectedImages: [], homepage: null, - siteTopic: null, - siteDescription: null, + siteTopic: '', + siteDescription: '', isSavimg: false, hasError: false }; diff --git a/src/utils.js b/src/utils.js index 1c62218..1018739 100644 --- a/src/utils.js +++ b/src/utils.js @@ -37,7 +37,7 @@ export const PageControlIcon = ({ isFilled = false }) => { fill="none" className="hover:fill-white" stroke="white" - stroke-width="2" + strokeWidth="2" /> ) } @@ -53,8 +53,8 @@ export const Logo = () => { fill="none" xmlns="http://www.w3.org/2000/svg"> @@ -70,6 +70,39 @@ export const Logo = () => { ); }; +/** + * Sometimes OpenAI requests fail, so we try to redo them if that happens. + */ +const retryApiFetch = async( params = [], maxAttempts = 3, delay = 500 ) => { + const { setError } = dispatch( 'quickwp/data' ); + + let attempts = 0; + + const makeRequest = async() => { + try { + + const response = await apiFetch({ + ...params + }); + + return response; + } catch ( error ) { + + attempts++; + + if ( attempts < maxAttempts ) { + await new Promise( resolve => setTimeout( resolve, delay ) ); + return makeRequest(); + } else { + setError( true ); + throw error; + } + } + }; + + return makeRequest(); +}; + const sendEvent = async( data ) => { const { setRunID, @@ -91,7 +124,7 @@ const getEvent = async( type ) => { const threadID = select( 'quickwp/data' ).getThreadID( type ); const route = 'homepage' !== type ? 'get' : 'templates'; - const response = await apiFetch({ + const response = await retryApiFetch({ path: addQueryArgs( `${ window.quickwp.api }/${ route }`, { 'thread_id': threadID }) @@ -189,7 +222,7 @@ const awaitEvent = async( type, interval = 5000 ) => { export const requestImages = async( query ) => { const { setImages } = dispatch( 'quickwp/data' ); - const response = await apiFetch({ + const response = await retryApiFetch({ path: addQueryArgs( `${ window.quickwp.api }/images`, { query })