Added code and translation for customapi format data types:

- percentBinary (1-100% of 8-bit number)
- state (boolean value as state)
This commit is contained in:
Kaya Emilia Riegler 2023-09-04 21:08:48 +02:00
parent 61b969cced
commit 9994ab4141
2 changed files with 21 additions and 13 deletions

View File

@ -306,6 +306,10 @@
"7days": "7 Days", "7days": "7 Days",
"30days": "30 Days" "30days": "30 Days"
}, },
"customapi": {
"active": "Active",
"inactive": "Inactive"
},
"gotify": { "gotify": {
"apps": "Applications", "apps": "Applications",
"clients": "Clients", "clients": "Clients",

View File

@ -1,4 +1,4 @@
import { useTranslation } from "next-i18next"; import {useTranslation} from "next-i18next";
import Container from "components/services/widget/container"; import Container from "components/services/widget/container";
import Block from "components/services/widget/block"; import Block from "components/services/widget/block";
@ -30,46 +30,50 @@ function getValue(field, data) {
function formatValue(t, mapping, value) { function formatValue(t, mapping, value) {
switch (mapping?.format) { switch (mapping?.format) {
case 'number': case 'number':
return t("common.number", { value: parseInt(value, 10) }); return t("common.number", {value: parseInt(value, 10)});
case 'float': case 'float':
return t("common.number", { value }); return t("common.number", {value});
case 'percent': case 'percent':
return t("common.percent", { value }); return t("common.percent", {value});
case 'percentBinary':
return t("common.percent", {value: (100 * value / 255).toFixed()});
case 'state':
return t(`customapi.${/true/.test(value) ? "active" : "inactive"}`);
case 'text': case 'text':
default: default:
return value; return value;
} }
} }
export default function Component({ service }) { export default function Component({service}) {
const { t } = useTranslation(); const {t} = useTranslation();
const { widget } = service; const {widget} = service;
const { mappings = [], refreshInterval = 10000 } = widget; const {mappings = [], refreshInterval = 10000} = widget;
const { data: customData, error: customError } = useWidgetAPI(widget, null, { const {data: customData, error: customError} = useWidgetAPI(widget, null, {
refreshInterval: Math.max(1000, refreshInterval), refreshInterval: Math.max(1000, refreshInterval),
}); });
if (customError) { if (customError) {
return <Container service={service} error={customError} />; return <Container service={service} error={customError}/>;
} }
if (!customData) { if (!customData) {
return ( return (
<Container service={service}> <Container service={service}>
{ mappings.slice(0,4).map(item => <Block label={item.label} key={item.field} />) } {mappings.slice(0, 4).map(item => <Block label={item.label} key={item.field}/>)}
</Container> </Container>
); );
} }
return ( return (
<Container service={service}> <Container service={service}>
{ mappings.slice(0,4).map(mapping => <Block {mappings.slice(0, 4).map(mapping => <Block
label={mapping.label} label={mapping.label}
key={mapping.field} key={mapping.field}
value={formatValue(t, mapping, getValue(mapping.field, customData))} value={formatValue(t, mapping, getValue(mapping.field, customData))}
/>) } />)}
</Container> </Container>
); );
} }