From b0f7cade8455234d47eb92a0fe992b15efbbaffa Mon Sep 17 00:00:00 2001 From: Alice Brooks Date: Thu, 19 Sep 2024 11:08:53 +0100 Subject: [PATCH] Tidy up prior to pushing --- .eslintrc.json | 4 +++- src/app.tsx | 12 ++++++------ src/backends/{backend.tsx => backend.d.ts} | 4 ++-- src/backends/suseconnect.tsx | 14 ++++++++++---- src/backends/transactional-update.tsx | 10 ++++++++-- src/components/reboot_dialog.tsx | 2 +- src/components/register_code_form.tsx | 12 ++++++------ src/components/subscription_list.tsx | 12 ++++++------ 8 files changed, 42 insertions(+), 28 deletions(-) rename src/backends/{backend.tsx => backend.d.ts} (94%) diff --git a/.eslintrc.json b/.eslintrc.json index d499b3e..b52354a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -22,7 +22,7 @@ "no-var": "error", "lines-between-class-members": ["error", "always", { "exceptAfterSingleLine": true }], "prefer-promise-reject-errors": ["error", { "allowEmptyReject": true }], - "react/jsx-indent": ["error", 4], + "react/jsx-indent": "off", "semi": ["error", "always", { "omitLastInOneLineBlock": true }], "react-hooks/rules-of-hooks": "error", @@ -36,6 +36,8 @@ "no-console": "off", "quotes": "off", "react/jsx-curly-spacing": "off", + "react/jsx-closing-tag-location": "off", + "react/jsx-handler-names": "off", "react/jsx-indent-props": "off", "react/jsx-no-useless-fragment": "error", "react/prop-types": "off", diff --git a/src/app.tsx b/src/app.tsx index 9fa8278..40359ed 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -58,7 +58,7 @@ export const Application = () => { updateSubscriptions(); }, [updateSubscriptions, backend]); - const registerProduct = async (): Promise<[boolean, string]> => { + const registerProduct = useCallback(async (): Promise<[boolean, string]> => { console.debug("registering", formData); const result = await backend?.register(formData.registrationCode, formData.email, formData.product).then((result) => { if (result[0]) { @@ -76,9 +76,9 @@ export const Application = () => { }); return result || [false, ""]; - }; + }, [backend, Dialogs, formData, updateSubscriptions]); - const deactivateProduct = (subscription: Subscription | Extension): void => { + const deactivateProduct = useCallback((subscription: Subscription | Extension): void => { console.log("deregistering", subscription.identifier); setLoadingSubscriptions(true); backend?.deregister([subscription.identifier, subscription.version, subscription.arch].join("/")) @@ -96,9 +96,9 @@ export const Application = () => { setLoadingSubscriptions(false); updateSubscriptions(); }); - }; + }, [backend, Dialogs, updateSubscriptions]); - const activateProduct = (subscription: Subscription | Extension): void => { + const activateProduct = useCallback((subscription: Subscription | Extension): void => { console.log("activating", subscription.identifier); setLoadingExtensions(true); backend?.register("", "", [subscription.identifier, subscription.version, subscription.arch].join("/")) @@ -113,7 +113,7 @@ export const Application = () => { setLoadingExtensions(false); updateSubscriptions(); }); - }; + }, [backend, Dialogs, updateSubscriptions]); return ( diff --git a/src/backends/backend.tsx b/src/backends/backend.d.ts similarity index 94% rename from src/backends/backend.tsx rename to src/backends/backend.d.ts index eb56dab..5155727 100644 --- a/src/backends/backend.tsx +++ b/src/backends/backend.d.ts @@ -10,7 +10,7 @@ type Extension = { expires_at?: string, } -enum SubscriptionStatus { +declare enum SubscriptionStatus { Active = "Active", Expired = "Expired", Unregistered = "Unregistered", @@ -39,7 +39,7 @@ type CockpitSpawnError = { exit_signal: string | null, } -enum SUSEConnectExitCodes { +declare enum SUSEConnectExitCodes { ZyppBusy = 7, } diff --git a/src/backends/suseconnect.tsx b/src/backends/suseconnect.tsx index f352c06..4b477cd 100644 --- a/src/backends/suseconnect.tsx +++ b/src/backends/suseconnect.tsx @@ -3,7 +3,7 @@ import { Backend, Subscription, CockpitSpawnError, SUSEConnectExitCodes, Extensi export class SuseConnect implements Backend { async getSubscriptions(): Promise { - let result; + let result = null; let retry = true; let tries = 0; @@ -23,7 +23,10 @@ export class SuseConnect implements Backend { }); } - return result || []; + if (result) + return result; + + throw new Error("Unable to get subscriptions"); } async getSubscriptionsStatus(): Promise { @@ -33,7 +36,7 @@ export class SuseConnect implements Backend { } async getExtensions(): Promise { - let result; + let result = null; let retry = true; let tries = 0; @@ -53,7 +56,10 @@ export class SuseConnect implements Backend { }); } - return result || []; + if (result) + return result; + + throw new Error("Unable to get extensions"); } async getAvailableExtensions(): Promise { diff --git a/src/backends/transactional-update.tsx b/src/backends/transactional-update.tsx index 0a335ac..0528c76 100644 --- a/src/backends/transactional-update.tsx +++ b/src/backends/transactional-update.tsx @@ -24,7 +24,10 @@ export class TransactionalUpdate implements Backend { }); } - return result || []; + if (result) + return result; + + throw new Error("Unable to get subscriptions"); } async getSubscriptionsStatus(): Promise { @@ -56,7 +59,10 @@ export class TransactionalUpdate implements Backend { }); } - return result || []; + if (result) + return result; + + throw new Error("Unable to get extensions"); } async getAvailableExtensions(): Promise { diff --git a/src/components/reboot_dialog.tsx b/src/components/reboot_dialog.tsx index 2901178..350ca78 100644 --- a/src/components/reboot_dialog.tsx +++ b/src/components/reboot_dialog.tsx @@ -10,7 +10,7 @@ export const RebootDialog = () => { const Dialogs = useDialogs(); const reboot = () => { - cockpit.spawn(["reboot"], { superuser: "try" }); + cockpit.spawn(["reboot"], { superuser: "require" }); }; return ( diff --git a/src/components/register_code_form.tsx b/src/components/register_code_form.tsx index 08ba80c..f5802eb 100644 --- a/src/components/register_code_form.tsx +++ b/src/components/register_code_form.tsx @@ -1,6 +1,6 @@ import cockpit from 'cockpit'; import { Form, Grid, FormGroup, TextInput, GridItem, ActionGroup, Button } from "@patternfly/react-core"; -import React, { useState } from "react"; +import React, { Dispatch, SetStateAction, useState } from "react"; import { EmptyStatePanel } from 'cockpit-components-empty-state'; const _ = cockpit.gettext; @@ -12,22 +12,22 @@ type RegisterFormData = { }; type Props = { - submitCallback: any, + submitCallback: () => Promise<[boolean, string]>, formData: RegisterFormData, - setFormData: any, + setFormData: Dispatch>, }; const RegisterCodeForm = ({ submitCallback, formData, setFormData }: Props) => { const [submitting, setSubmitting] = useState(false); - const onValueChange = (fieldName: string, value: any) => { + const onValueChange = (fieldName: string, value: string|number) => { setFormData({ ...formData, [fieldName]: value }); }; const submit = () => { setSubmitting(true); - submitCallback().then((success: boolean) => { - if (success) { + submitCallback().then((result: [boolean, string]) => { + if (result[0]) { setFormData({ registrationCode: "", email: "", diff --git a/src/components/subscription_list.tsx b/src/components/subscription_list.tsx index 9dc4471..8351c55 100644 --- a/src/components/subscription_list.tsx +++ b/src/components/subscription_list.tsx @@ -40,20 +40,20 @@ export const SubscriptionList = ({ subscriptions, loading, deactivate, activate {item.expires_at ? - Expires: {format_date(item.expires_at)} - + Expires: {format_date(item.expires_at)} + : ""} {deactivate ? + Deactivate + : ""} {activate ? + Activate + : ""}