Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
userXinos committed Aug 16, 2023
1 parent fc2a933 commit b0bc292
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/components/widgets/openmediavault/updates.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import useSWR from "swr";
import { FaCheck } from "react-icons/fa";
import { FaDownload } from "react-icons/fa";
import { useTranslation } from "next-i18next";

import Resource from "../widget/resource";
import Error from "../widget/error";

const params = new URLSearchParams({ method: "apt.enumerateUpgraded" }).toString();
const useApi = () => useSWR(`/api/widgets/openmediavault?${params}`);

export default function Uptime({ expanded }) {
const { t, i18n } = useTranslation();
const params = { lang: i18n.language, method: "apt.enumerateUpgraded" };
const { data, error } = useSWR(`/api/widgets/openmediavault?${new URLSearchParams(params).toString()}`);
const { t } = useTranslation();
const { data, error } = useApi();

if (error || data?.error) {
return <Error />;
Expand All @@ -20,7 +22,7 @@ export default function Uptime({ expanded }) {

return (
<Resource
icon={FaCheck}
icon={FaDownload}
value={t("openmediavault.updatesAvailable")}
expandedValue={t("openmediavault.packageCount", { value: data.response.length })}
expanded={expanded}
Expand Down
1 change: 0 additions & 1 deletion src/pages/api/widgets/openmediavault.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getPrivateWidgetOptions } from "utils/config/widget-helpers";

export default async function handler(req, res) {
const { index, method } = req.query;

const [{ options }] = await getPrivateWidgetOptions("openmediavault", index);
const widget = {
type: "openmediavault",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const items = [
export default function Component({ service }) {
const { data, error } = useWidgetAPI(service.widget);

if (error) {
return <Container service={service} error={error} />;
if (error || data?.error) {
return <Container service={service} error={error || data?.error} />;
}

const itemsWithData = items.map((item) => ({
...item,
number: data?.response?.data ? item.getNumber(data?.response?.data) : null,
number: data?.response?.data ? item.getNumber(data.response.data) : null,
}));

return (
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/openmediavault/methods/services_get_status.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const items = [
export default function Component({ service }) {
const { data, error } = useWidgetAPI(service.widget);

if (error) {
return <Container service={service} error={error} />;
if (error || data?.error) {
return <Container service={service} error={error || data?.error} />;
}

const itemsWithData = items.map((item) => ({
...item,
number: data?.response?.data ? item.getNumber(data?.response?.data) : null,
number: data?.response?.data ? item.getNumber(data.response.data) : null,
}));

return (
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/openmediavault/methods/smart_get_list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const items = [
export default function Component({ service }) {
const { data, error } = useWidgetAPI(service.widget);

if (error) {
return <Container service={service} error={error} />;
if (error || data?.error) {
return <Container service={service} error={error || data?.error} />;
}

const itemsWithData = items.map((item) => ({
...item,
number: data?.response?.output ? item.getNumber(JSON.parse(data?.response?.output || "{}")?.data) : null,
number: data?.response?.output ? item.getNumber(JSON.parse(data.response.output).data) : null,
}));

return (
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/openmediavault/methods/zfs_get_stats.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export default function Component({ service }) {
}
}, [data]);

if (error) {
if (error || data?.error) {
return (
<Container>
<Error error={error} />
<Error error={error || data?.error} />
</Container>
);
}
Expand Down

0 comments on commit b0bc292

Please sign in to comment.