Skip to content

Commit

Permalink
#15 Handle wp_errors on the backend properly + display them on frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
versusbassz committed Sep 6, 2021
1 parent 93c7c63 commit 339f727
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 50 deletions.
38 changes: 23 additions & 15 deletions assets/js/components/metabox/MetaboxContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,31 @@ export const MetaboxContent = () => {
return null;
}

switch(key) {
case "entity": return (
<div className="vsm-tab-content" key={key}>
<div className="vsm-tab-content__title">Section: {data.section_title}</div>
<EntityPropsTable search={search} fieldsData={data} />
</div>
);
case "meta": return (
<div className="vsm-tab-content" key={key}>
<div className="vsm-tab-content__title">Section: {data.section_title}</div>
<MetaTable search={search} fieldsData={data} />
</div>
);
default: return <p key={key}>Unknown tab: {key}</p>;
}
return (
<TabContent key={key} title={data.section_title} error={data.error}>
{key === "entity" && <EntityPropsTable search={search} fieldsData={data} />}
{key === "meta" && <MetaTable search={search} fieldsData={data} />}
</TabContent>
);
})}
</div>
</>
);
}

const TabContent = ({ children, title, error }) => {
return (
<div className="vsm-tab-content">
<div className="vsm-tab-content__title">Section: {title}</div>

{error ? (
<div className="notice notice-error inline">
<p>{str("error")}: {error}</p>
</div>
) : (
<>{children}</>
)}

</div>
);
};
15 changes: 3 additions & 12 deletions inc/logic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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,
];
}

Expand Down
23 changes: 13 additions & 10 deletions src/Fetcher/EntityFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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;
}

/**
Expand Down
30 changes: 17 additions & 13 deletions src/Fetcher/MetaFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 339f727

Please sign in to comment.