From 339f727ffc12acc7dc3913651c967dd2b9f07640 Mon Sep 17 00:00:00 2001 From: Vladimir Sklyar Date: Mon, 6 Sep 2021 15:13:35 +0300 Subject: [PATCH] #15 Handle wp_errors on the backend properly + display them on frontend --- .../js/components/metabox/MetaboxContent.js | 38 +++++++++++-------- inc/logic.php | 15 ++------ src/Fetcher/EntityFetcher.php | 23 ++++++----- src/Fetcher/MetaFetcher.php | 30 ++++++++------- 4 files changed, 56 insertions(+), 50 deletions(-) diff --git a/assets/js/components/metabox/MetaboxContent.js b/assets/js/components/metabox/MetaboxContent.js index 1572515..5cac93f 100644 --- a/assets/js/components/metabox/MetaboxContent.js +++ b/assets/js/components/metabox/MetaboxContent.js @@ -78,23 +78,31 @@ export const MetaboxContent = () => { return null; } - switch(key) { - case "entity": return ( -
-
Section: {data.section_title}
- -
- ); - case "meta": return ( -
-
Section: {data.section_title}
- -
- ); - default: return

Unknown tab: {key}

; - } + return ( + + {key === "entity" && } + {key === "meta" && } + + ); })} ); } + +const TabContent = ({ children, title, error }) => { + return ( +
+
Section: {title}
+ + {error ? ( +
+

{str("error")}: {error}

+
+ ) : ( + <>{children} + )} + +
+ ); +}; diff --git a/inc/logic.php b/inc/logic.php index 18c6872..f9f8ee6 100644 --- a/inc/logic.php +++ b/inc/logic.php @@ -45,6 +45,7 @@ function show_metabox(object $item): void 'fields_not_found' => esc_html__('There are no meta fields for this item.', 'entity-viewer'), 'fields_not_found_for_search_query' => esc_html__('There are no meta fields for this search query.', 'entity-viewer'), 'see_raw_value' => esc_html__('see the raw value for search results', 'entity-viewer'), + 'error' => esc_html__('Error', 'entity-viewer'), ], ], 'metabox' => [ @@ -71,22 +72,12 @@ function show_metabox(object $item): void */ function get_metabox_payload(string $entity_name, int $item_id): array { - // TODO smth with WP_Error ??? $entity_data = EntityFetcher::getData($entity_name, $item_id); $meta_data = MetaFetcher::getData($entity_name, $item_id); return [ - 'entity' => [ - 'tab_title' => $entity_data['tab_title'], - 'section_title' => $entity_data['section_title'], - 'fields' => $entity_data['fields'], - ], - 'meta' => [ - 'tab_title' => $meta_data['tab_title'], - 'section_title' => $meta_data['section_title'], - 'fields' => $meta_data['fields'], - 'has_serialized_values' => $meta_data['has_serialized_values'], // not used in JS for now - ], + 'entity' => $entity_data, + 'meta' => $meta_data, ]; } diff --git a/src/Fetcher/EntityFetcher.php b/src/Fetcher/EntityFetcher.php index 157d5b6..1913d2c 100644 --- a/src/Fetcher/EntityFetcher.php +++ b/src/Fetcher/EntityFetcher.php @@ -8,15 +8,20 @@ class EntityFetcher { - /** - * @return array|WP_Error - */ - public static function getData(string $entity_name, int $item_id) + public static function getData(string $entity_name, int $item_id): array { + $result = [ + 'tab_title' => __('Props', 'entity-viewer'), + 'section_title' => __('Props', 'entity-viewer'), + 'fields' => [], + 'error' => false, + ]; + $data = self::fetchDataFromDB($entity_name, $item_id); if (is_wp_error($data)) { - return $data; + $result['error'] = $data->get_error_code(); + return $result; } $fields = []; @@ -31,11 +36,9 @@ public static function getData(string $entity_name, int $item_id) ++$index; } - return [ - 'tab_title' => __('Props', 'entity-viewer'), - 'section_title' => __('Props', 'entity-viewer'), - 'fields' => $fields, - ]; + $result['fields'] = $fields; + + return $result; } /** diff --git a/src/Fetcher/MetaFetcher.php b/src/Fetcher/MetaFetcher.php index 6ac9a62..de864d1 100644 --- a/src/Fetcher/MetaFetcher.php +++ b/src/Fetcher/MetaFetcher.php @@ -9,31 +9,35 @@ class MetaFetcher { - /** - * @return array|WP_Error - */ - public static function getData(string $entity_name, int $item_id) + public static function getData(string $entity_name, int $item_id): array { - $has_serialized_values = false; - $meta_id_key = get_meta_id_column_for_entity($entity_name); $meta_raw = self::fetchDataFromDB($entity_name, $item_id); + $result = [ + 'tab_title' => __('Meta', 'entity-viewer'), + 'section_title' => __('Meta', 'entity-viewer'), + 'fields' => [], + 'error' => false, + 'has_serialized_values' => false, // not used in JS for now + ]; + if (is_wp_error($meta_raw)) { - return $meta_raw; + $result['error'] = $meta_raw->get_error_code(); + return $result; } + $has_serialized_values = false; + $fields = array_map( construct_meta_data_mapper($meta_id_key, $has_serialized_values), $meta_raw ); - return [ - 'tab_title' => __('Meta', 'entity-viewer'), - 'section_title' => __('Meta', 'entity-viewer'), - 'fields' => $fields, - 'has_serialized_values' => $has_serialized_values, - ]; + $result['fields'] = $fields; + $result['has_serialized_values'] = $has_serialized_values; + + return $result; } /**