Refactor proxy for brevity

pull/442/head
Michael Shamoon 2 years ago
parent df3e2eff11
commit d942e989bd

@ -9,13 +9,13 @@ export default function Component({ service }) {
const { widget } = service; const { widget } = service;
const { data: homebridge, error: homebridgeError } = useWidgetAPI(widget, "info"); const { data: homebridgeData, error: homebridgeError } = useWidgetAPI(widget, "info");
if (homebridgeError || (homebridge && !homebridge.data)) { if (homebridgeError || homebridgeData?.error) {
return <Container error={t("widget.api_error")} />; return <Container error={t("widget.api_error")} />;
} }
if (!homebridge) { if (!homebridgeData) {
return ( return (
<Container service={service}> <Container service={service}>
<Block label="widget.status" /> <Block label="widget.status" />
@ -29,21 +29,21 @@ export default function Component({ service }) {
<Container service={service}> <Container service={service}>
<Block <Block
label="widget.status" label="widget.status"
value={`${homebridge.data.status[0].toUpperCase()}${homebridge.data.status.substr(1)}`} value={`${homebridgeData.status[0].toUpperCase()}${homebridgeData.status.substr(1)}`}
/> />
<Block <Block
label="homebridge.updates" label="homebridge.updates"
value={ value={
(homebridge.data.updateAvailable || homebridge.data.plugins.updatesAvailable) (homebridgeData.updateAvailable || homebridgeData.plugins?.updatesAvailable)
? t("homebridge.update_available") ? t("homebridge.update_available")
: t("homebridge.up_to_date")} : t("homebridge.up_to_date")}
/> />
{homebridge?.data?.childBridges.quantity > 0 && {homebridgeData?.childBridges?.total > 0 &&
<Block <Block
label="homebridge.child_bridges" label="homebridge.child_bridges"
value={t("homebridge.child_bridges_status", { value={t("homebridge.child_bridges_status", {
total: homebridge.data.childBridges.quantity, total: homebridgeData.childBridges.total,
ok: homebridge.data.childBridges.quantityWithOkStatus ok: homebridgeData.childBridges.running
})} })}
/>} />}
</Container> </Container>

@ -62,22 +62,6 @@ async function apiCall(widget, endpoint) {
return { status, contentType, data: JSON.parse(data.toString()), responseHeaders }; return { status, contentType, data: JSON.parse(data.toString()), responseHeaders };
} }
function formatPluginsResponse(plugins) {
const quantity = plugins?.data.filter(p => p.updateAvailable).length;
return {
updatesAvailable: quantity > 0,
quantity,
}
}
function formatChildBridgesResponse(childBridges) {
const quantity = childBridges?.data?.length
return {
quantity,
quantityWithOkStatus: childBridges?.data?.filter(cb => cb.status === "ok").length,
}
}
export default async function homebridgeProxyHandler(req, res) { export default async function homebridgeProxyHandler(req, res) {
const { group, service } = req.query; const { group, service } = req.query;
@ -97,17 +81,20 @@ export default async function homebridgeProxyHandler(req, res) {
await login(widget); await login(widget);
} }
const statusRs = await apiCall(widget, "status/homebridge"); const { data: statusData } = await apiCall(widget, "status/homebridge");
const versionRs = await apiCall(widget, "status/homebridge-version"); const { data: versionData } = await apiCall(widget, "status/homebridge-version");
const childBrigdeRs = await apiCall(widget, "status/homebridge/child-bridges"); const { data: childBridgeData } = await apiCall(widget, "status/homebridge/child-bridges");
const pluginsRs = await apiCall(widget, "plugins"); const { data: pluginsData } = await apiCall(widget, "plugins");
return res.status(200).send({ return res.status(200).send({
data: { status: statusData?.status,
status: statusRs?.data?.status, updateAvailable: versionData?.updateAvailable,
updateAvailable: versionRs?.data?.updateAvailable, plugins: {
plugins: formatPluginsResponse(pluginsRs), updatesAvailable: pluginsData?.filter(p => p.updateAvailable).length,
childBridges: formatChildBridgesResponse(childBrigdeRs), },
} childBridges: {
running: childBridgeData?.filter(cb => cb.status === "ok").length,
total: childBridgeData?.length
}
}); });
} }

Loading…
Cancel
Save