Skip to content

Commit

Permalink
refactor(graphCard): ent-4899 align hook components (#927)
Browse files Browse the repository at this point in the history
* graphCardMetric, Metrics, combine in helpers
  • Loading branch information
cdcabrera committed Oct 14, 2022
1 parent 2bdb7be commit e372d75
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,113 @@ exports[`GraphCard Component should allow being disabled: disabled 1`] = `""`;

exports[`GraphCard Component should render a basic component: basic 1`] = `
<Fragment>
<GraphCardMetrics
metricFilters={
[
{
"id": "Core-seconds",
"isStacked": true,
<ContextProvider
value={
{
"settings": {
"isStandalone": false,
"isThreshold": false,
"strokeWidth": 2,
"metric": undefined,
"metrics": [
{
"id": "Core-seconds",
"isStacked": true,
"isStandalone": false,
"isThreshold": false,
"strokeWidth": 2,
},
],
},
]
}
}
useProductGraphConfig={[Function]}
/>
>
<GraphCardChart
t={[Function]}
useGetGraphTally={[Function]}
useGraphCardContext={[Function]}
useMetricsSelector={[Function]}
useProduct={[Function]}
useProductGraphTallyQuery={[Function]}
/>
</ContextProvider>
</Fragment>
`;

exports[`GraphCard Component should setup basic settings: settings 1`] = `
<Fragment>
<GraphCardMetrics
metricFilters={
[
{
"id": "Core-seconds",
"isStacked": true,
<ContextProvider
value={
{
"settings": {
"isStandalone": false,
"isThreshold": false,
"strokeWidth": 2,
"metric": undefined,
"metrics": [
{
"id": "Core-seconds",
"isStacked": true,
"isStandalone": false,
"isThreshold": false,
"strokeWidth": 2,
},
],
},
]
}
}
useProductGraphConfig={[Function]}
/>
<GraphCardMetric
key="graphCard_Sockets"
metricFilter={
>
<GraphCardChart
t={[Function]}
useGetGraphTally={[Function]}
useGraphCardContext={[Function]}
useMetricsSelector={[Function]}
useProduct={[Function]}
useProductGraphTallyQuery={[Function]}
/>
</ContextProvider>
<ContextProvider
key="graphCard_undefined"
value={
{
"id": "Sockets",
"isStacked": true,
"isStandalone": true,
"isThreshold": false,
"strokeWidth": 2,
"settings": {
"isStandalone": true,
"metric": {
"id": "Sockets",
"isStacked": true,
"isStandalone": true,
"isThreshold": false,
"strokeWidth": 2,
},
"metrics": [
{
"id": "Sockets",
"isStacked": true,
"isStandalone": true,
"isThreshold": false,
"strokeWidth": 2,
},
],
"padding": {
"bottom": 75,
"left": 75,
"right": 45,
"top": 45,
},
},
}
}
useProductGraphConfig={[Function]}
/>
>
<GraphCardMetricTotals
t={[Function]}
useMetricsSelector={[Function]}
useProductGraphTallyQuery={[Function]}
>
<GraphCardChart
t={[Function]}
useGetGraphTally={[Function]}
useGraphCardContext={[Function]}
useMetricsSelector={[Function]}
useProduct={[Function]}
useProductGraphTallyQuery={[Function]}
/>
</GraphCardMetricTotals>
</ContextProvider>
</Fragment>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@

exports[`GraphCardHelpers generateChartSettings should return base graph settings: basic filters 1`] = `
{
"groupedFilters": [
{
"dolor": "sit",
"id": "dolorSit",
"isStacked": true,
"groupedFiltersSettings": {
"settings": {
"isStandalone": false,
"isThreshold": false,
"strokeWidth": 2,
"metric": undefined,
"metrics": [
{
"dolor": "sit",
"id": "dolorSit",
"isStacked": true,
"isStandalone": false,
"isThreshold": false,
"strokeWidth": 2,
},
],
},
],
"standaloneFilters": [],
},
"standaloneFiltersSettings": [],
}
`;

exports[`GraphCardHelpers generateChartSettings should return base graph settings: no filters 1`] = `
{
"groupedFilters": [],
"standaloneFilters": [],
"groupedFiltersSettings": undefined,
"standaloneFiltersSettings": [],
}
`;

Expand Down

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions src/components/graphCard/__tests__/graphCardMetric.test.js

This file was deleted.

11 changes: 0 additions & 11 deletions src/components/graphCard/__tests__/graphCardMetrics.test.js

This file was deleted.

27 changes: 20 additions & 7 deletions src/components/graphCard/graphCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React from 'react';
import PropTypes from 'prop-types';
import { useProductGraphConfig } from '../productView/productViewContext';
import { helpers } from '../../common';
import { GraphCardMetrics } from './graphCardMetrics';
import { GraphCardMetric } from './graphCardMetric';
import { GraphCardMetricTotals } from './graphCardMetricTotals';
import { GraphCardChart } from './graphCardChart';
import { GraphCardContext } from './graphCardContext';
import { graphCardHelpers } from './graphCardHelpers';

/**
Expand All @@ -15,18 +16,30 @@ import { graphCardHelpers } from './graphCardHelpers';
* @returns {Node}
*/
const GraphCard = ({ isDisabled, useProductGraphConfig: useAliasProductGraphConfig }) => {
const { filters } = useAliasProductGraphConfig();
const { groupedFilters, standaloneFilters } = graphCardHelpers.generateChartSettings(filters);
const { filters, settings } = useAliasProductGraphConfig();
const { groupedFiltersSettings, standaloneFiltersSettings } = graphCardHelpers.generateChartSettings(
filters,
settings
);

if (isDisabled) {
return null;
}

return (
<React.Fragment>
{(groupedFilters?.length && <GraphCardMetrics metricFilters={groupedFilters} />) || null}
{standaloneFilters.map(metricFilter => (
<GraphCardMetric key={`graphCard_${metricFilter.id}`} metricFilter={metricFilter} />
{(groupedFiltersSettings && (
<GraphCardContext.Provider value={groupedFiltersSettings}>
<GraphCardChart />
</GraphCardContext.Provider>
)) ||
null}
{standaloneFiltersSettings?.map(filtersSettings => (
<GraphCardContext.Provider key={`graphCard_${filtersSettings?.metric?.id}`} value={filtersSettings}>
<GraphCardMetricTotals>
<GraphCardChart />
</GraphCardMetricTotals>
</GraphCardContext.Provider>
))}
</React.Fragment>
);
Expand Down
56 changes: 43 additions & 13 deletions src/components/graphCard/graphCardHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { RHSM_API_QUERY_GRANULARITY_TYPES as GRANULARITY_TYPES } from '../../typ
import { dateHelpers, helpers } from '../../common';

/**
* Update chart/graph filters with base settings with styling.
* Update chart/graph filters with core settings and styling.
*
* @param {Array} filters
* @returns {{standaloneFilters: Array, groupedFilters: Array}}
* @param {object} graphCardSettings
* @returns {{standaloneFilters: Array, groupedFilters: object}}
*/
const generateChartSettings = (filters = []) => {
const standaloneFilters = [];
const groupedFilters = [];
const generateChartSettings = (filters = [], graphCardSettings = {}) => {
const standaloneFiltersSettings = [];
const groupedFiltersSettings = [];

filters.forEach(({ id, isStandalone = false, isThreshold = false, ...settings }) => {
filters.forEach(({ id, isStandalone = false, isThreshold = false, ...filterSettings }) => {
if (!id) {
return;
}
Expand All @@ -33,21 +34,50 @@ const generateChartSettings = (filters = []) => {
}

if (isStandalone) {
standaloneFilters.push({
...baseFilterSettings,
...settings
standaloneFiltersSettings.push({
settings: {
padding: {
bottom: 75,
left: 75,
right: 45,
top: 45
},
...graphCardSettings,
isStandalone: true,
metric: {
...baseFilterSettings,
...filterSettings
},
metrics: [
{
...baseFilterSettings,
...filterSettings
}
]
}
});
} else {
groupedFilters.push({
groupedFiltersSettings.push({
...baseFilterSettings,
...settings
...filterSettings
});
}
});

const updatedGroupedFiltersSettings =
(groupedFiltersSettings.length && {
settings: {
...graphCardSettings,
isStandalone: false,
metric: undefined,
metrics: groupedFiltersSettings
}
}) ||
undefined;

return {
standaloneFilters,
groupedFilters
standaloneFiltersSettings,
groupedFiltersSettings: updatedGroupedFiltersSettings
};
};

Expand Down
Loading

0 comments on commit e372d75

Please sign in to comment.