-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for Custom charts and cards widgets (#139)
- Loading branch information
1 parent
1f8b083
commit b6ca6a7
Showing
7 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/frontend/src/components/custom-modules/CustomCardModule.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { FC } from "react"; | ||
|
||
interface IProps { | ||
title: string | undefined; | ||
value: string | React.ReactNode | undefined; | ||
} | ||
|
||
const CustomCardModule: FC<IProps> = ({ title, value }) => { | ||
return ( | ||
<div className="flex flex-col justify-center items-center border border-solid border-btnRingVariant300 !rounded-2xl p-5 m-5"> | ||
{title && ( | ||
<h6 className="text-primaryVariant100 fontGroup-support text-center uppercase mb-0.5"> | ||
{title} | ||
</h6> | ||
)} | ||
{value && ( | ||
<div className="whitespace-nowrap prose-p:fontGroup-major prose-p:!text-4xl text-primary mb-0 text-center"> | ||
{value} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default CustomCardModule; |
123 changes: 123 additions & 0 deletions
123
packages/frontend/src/components/custom-modules/CustomChartModule.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { FC, useMemo } from "react"; | ||
import { | ||
ApexAreaChart, | ||
ApexBarChart, | ||
ApexDonutChart, | ||
ApexLineChart, | ||
ApexPieChart, | ||
themeColors, | ||
} from "@alphaday/ui-kit"; | ||
import { TCustomMetaChart } from "src/api/services"; | ||
import { TCustomSeries } from "src/api/types"; | ||
import { getXSeries, getYSeries } from "src/api/utils/customDataUtils"; | ||
|
||
export interface IChartModuleProps { | ||
id: number; | ||
series: TCustomSeries; | ||
meta: TCustomMetaChart | undefined; | ||
name: string; | ||
} | ||
|
||
const CustomChartModule: FC<IChartModuleProps> = ({ | ||
id, | ||
series, | ||
meta, | ||
name, | ||
}) => { | ||
const labels = useMemo(() => { | ||
return getXSeries(series, meta, name); | ||
}, [meta, series, name]); | ||
|
||
const options = { | ||
id, | ||
sparkline: { | ||
enabled: false, | ||
}, | ||
background: "transparent", | ||
redrawOnWindowResize: true, | ||
height: "500px", | ||
labels, | ||
dataLabels: { | ||
enabled: false, | ||
}, | ||
plotOptions: { | ||
pie: { | ||
donut: { | ||
background: "transparent", | ||
}, | ||
}, | ||
}, | ||
stroke: { | ||
colors: undefined, | ||
}, | ||
legend: { | ||
show: true, | ||
fontSize: "11px", | ||
position: "left", | ||
offsetX: 0, | ||
offsetY: 0, | ||
height: 500, | ||
labels: { | ||
colors: [themeColors.primaryVariant100], | ||
useSeriesColors: false, | ||
}, | ||
markers: { | ||
radius: 3, | ||
}, | ||
}, | ||
}; | ||
|
||
const ySeries = useMemo(() => { | ||
return getYSeries(series, meta, name); | ||
}, [meta, series, name]); | ||
|
||
if (meta?.layout.variant === "donut") { | ||
return ( | ||
<ApexDonutChart | ||
options={options} | ||
series={ySeries} | ||
width="400px" | ||
height="500px" | ||
/> | ||
); | ||
} | ||
if (meta?.layout.variant === "pie") { | ||
return ( | ||
<ApexPieChart | ||
options={options} | ||
series={ySeries} | ||
width="400px" | ||
height="500px" | ||
/> | ||
); | ||
} | ||
if (meta?.layout.variant === "bar") { | ||
return ( | ||
<ApexBarChart | ||
options={options} | ||
series={ySeries} | ||
width="400px" | ||
height="500px" | ||
/> | ||
); | ||
} | ||
if (meta?.layout.variant === "area") { | ||
return ( | ||
<ApexAreaChart | ||
options={options} | ||
series={ySeries} | ||
width="400px" | ||
height="500px" | ||
/> | ||
); | ||
} | ||
return ( | ||
<ApexLineChart | ||
options={options} | ||
series={ySeries} | ||
width="400px" | ||
height="500px" | ||
/> | ||
); | ||
}; | ||
export default CustomChartModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/frontend/src/containers/custom-modules/CustomCardContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { FC, useMemo } from "react"; | ||
import { EWidgetData, useGetCustomItemsQuery } from "src/api/services"; | ||
import { customDataAsCardData } from "src/api/utils/customDataUtils"; | ||
import { Logger } from "src/api/utils/logging"; | ||
import CustomCardModule from "src/components/custom-modules/CustomCardModule"; | ||
import { IModuleContainer } from "src/types"; | ||
|
||
const CustomChartContainer: FC<IModuleContainer> = ({ moduleData }) => { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const { name, custom_data, custom_meta, endpoint_url, data_type } = | ||
moduleData.widget; | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
|
||
const { data } = useGetCustomItemsQuery( | ||
{ | ||
endpointUrl: endpoint_url || "", | ||
}, | ||
{ | ||
skip: data_type === EWidgetData.Static, | ||
} | ||
); | ||
|
||
const cardData = useMemo(() => { | ||
let dataToUse; | ||
if (data_type === EWidgetData.Static) { | ||
if (!custom_data) { | ||
Logger.error( | ||
`CustomCardContainer: missing data for widget ${name}` | ||
); | ||
return undefined; | ||
} | ||
dataToUse = custom_data; | ||
} else { | ||
dataToUse = data?.results ?? []; | ||
} | ||
if (dataToUse) { | ||
if (custom_meta != null) { | ||
if (custom_meta.layout_type !== "card") { | ||
Logger.error( | ||
`CustomCardContainer: invalid layout type, expected "card" in widget ${name}` | ||
); | ||
return customDataAsCardData(dataToUse, undefined, name); | ||
} | ||
return customDataAsCardData(dataToUse, custom_meta, name); | ||
} | ||
} | ||
return customDataAsCardData(dataToUse, undefined, name); | ||
}, [custom_data, custom_meta, data?.results, data_type, name]); | ||
|
||
return <CustomCardModule title={cardData?.title} value={cardData?.value} />; | ||
}; | ||
|
||
export default CustomChartContainer; |
51 changes: 51 additions & 0 deletions
51
packages/frontend/src/containers/custom-modules/CustomChartContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { FC, useMemo } from "react"; | ||
import { EWidgetData, useGetCustomItemsQuery } from "src/api/services"; | ||
import { customDataAsSeries } from "src/api/utils/customDataUtils"; | ||
import { Logger } from "src/api/utils/logging"; | ||
import CustomChartModule from "src/components/custom-modules/CustomChartModule"; | ||
import { IModuleContainer } from "src/types"; | ||
|
||
const CustomChartContainer: FC<IModuleContainer> = ({ moduleData }) => { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const { id, name, custom_data, custom_meta, endpoint_url, data_type } = | ||
moduleData.widget; | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
|
||
const { data } = useGetCustomItemsQuery( | ||
{ | ||
endpointUrl: endpoint_url || "", | ||
}, | ||
{ | ||
skip: data_type === EWidgetData.Static, | ||
} | ||
); | ||
|
||
const series = useMemo(() => { | ||
if (data_type === EWidgetData.Static) { | ||
if (!custom_data) { | ||
Logger.error( | ||
`CustomChartContainer: missing data for widget ${name}` | ||
); | ||
return []; | ||
} | ||
return customDataAsSeries(custom_data); | ||
} | ||
return customDataAsSeries(data?.results ?? []); | ||
}, [custom_data, data?.results, data_type, name]); | ||
|
||
const meta = useMemo(() => { | ||
if (custom_meta?.layout_type !== "chart") { | ||
Logger.warn( | ||
`CustomChartContainer: invalid layout type, expected chart in widget ${name}` | ||
); | ||
return undefined; | ||
} | ||
return custom_meta; | ||
}, [custom_meta, name]); | ||
|
||
return ( | ||
<CustomChartModule id={id} series={series} meta={meta} name={name} /> | ||
); | ||
}; | ||
|
||
export default CustomChartContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters