Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show disabled NWCs again #1134

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion public/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@
"confirm_delete": "Are you sure you want to delete this connection?",
"budget": "Budget",
"resets_every": "Resets every",
"resubscribe_date": "Resubscribe on"
"resubscribe_date": "Resubscribe on",
"disable_connection": "Disable",
"disabled": "This subscription is disabled. Currently you can't re-enable subscriptions, but we're working to fix that. Check back soon!",
"disable_connection_confirm": "Are you sure you want to disable your subscription? Currently subscriptions can't be re-enabled, we're working to fix that."
},
"emergency_kit": {
"title": "Emergency Kit",
Expand Down
109 changes: 72 additions & 37 deletions src/components/NWCEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
SubmitHandler
} from "@modular-forms/solid";
import { BudgetPeriod, NwcProfile, TagItem } from "@mutinywallet/mutiny-wasm";
import { createAsync } from "@solidjs/router";
import {
createEffect,
createMemo,
createResource,
For,
Match,
ResourceFetcher,
Show,
Suspense,
Switch
} from "solid-js";

Expand Down Expand Up @@ -112,7 +115,7 @@ export function NWCEditor(props: {
const formMode = createMemo(() => {
const mode: "createnwa" | "createnwc" | "editnwc" = nwa()
? "createnwa"
: props.initialProfileIndex
: typeof props.initialProfileIndex === "number"
? "editnwc"
: "createnwc";
return mode;
Expand Down Expand Up @@ -173,7 +176,7 @@ export function NWCEditor(props: {
NwcProfile | undefined
> = async (index, _last) => {
console.log("fetching nwc profile", index);
if (!index) return undefined;
if (typeof index !== "number") return undefined;

try {
const profile: NwcProfile | undefined =
Expand Down Expand Up @@ -212,7 +215,8 @@ export function NWCEditor(props: {

async function saveConnection(f: BudgetForm) {
let newProfile: NwcProfile | undefined = undefined;
if (!f.profileIndex) throw new Error("No profile index!");
if (typeof f.profileIndex !== "number")
throw new Error("No profile index!");
if (!f.auto_approve || f.budget_amount === "0") {
newProfile = await sw.set_nwc_profile_require_approval(
f.profileIndex
Expand Down Expand Up @@ -259,11 +263,33 @@ export function NWCEditor(props: {
}
}

// TODO: refactor nwc editor so we don't need to do this
const initialBudget = createAsync(async () => {
if (profile()?.tag === "Subscription") {
try {
const plans = await sw.get_subscription_plans();
if (plans.length) {
const returnValue = plans[0].amount_sat.toString() || "0";
return returnValue;
} else {
return "0";
}
} catch (e) {
console.error(e);
return "0";
}
}
if (profile()?.budget_amount) {
return profile()?.budget_amount?.toString() || "0";
}
return "0";
});

return (
<Switch>
<Match when={formMode() === "createnwc"}>
<NWCEditorForm
initialValues={{
values={{
connection_name: "",
auto_approve: false,
budget_amount: "0",
Expand All @@ -281,7 +307,7 @@ export function NWCEditor(props: {
>
<Avatar large image_url={image()} />
<NWCEditorForm
initialValues={{
values={{
connection_name: name(),
auto_approve: nwa()?.budget ? true : false,
budget_amount:
Expand All @@ -299,47 +325,52 @@ export function NWCEditor(props: {
</Match>
<Match when={formMode() === "editnwc"}>
{/* FIXME: not getting the contact rn */}
<Show when={profile()}>
<Show when={profile()?.label && contact()?.image_url}>
<Avatar large image_url={contact()?.image_url} />
<pre>{JSON.stringify(contact(), null, 2)}</pre>
<Suspense>
<Show when={profile()}>
<Show when={profile()?.label && contact()?.image_url}>
<Avatar large image_url={contact()?.image_url} />
<pre>{JSON.stringify(contact(), null, 2)}</pre>
</Show>
<Show when={initialBudget()}>
<NWCEditorForm
values={{
connection_name: profile()?.name ?? "",
auto_approve: !profile()?.require_approval,
budget_amount: initialBudget()!,
interval:
profile()?.tag === "Subscription"
? "Month"
: (profile()?.budget_period?.toString() as BudgetForm["interval"]) ||
"Day",

profileIndex: profile()?.index
}}
formMode={formMode()}
budgetMode={
profile()?.tag === "Subscription"
? "fixed"
: "editable"
}
onSave={saveConnection}
/>
</Show>
</Show>
<NWCEditorForm
initialValues={{
connection_name: profile()?.name ?? "",
auto_approve: !profile()?.require_approval,
budget_amount:
profile()?.budget_amount?.toString() ?? "0",
interval:
(profile()
?.budget_period as BudgetForm["interval"]) ??
"Day",
profileIndex: profile()?.index
}}
formMode={formMode()}
budgetMode={
profile()?.tag === "Subscription"
? "fixed"
: "editable"
}
onSave={saveConnection}
/>
</Show>
</Suspense>
</Match>
</Switch>
);
}

function NWCEditorForm(props: {
initialValues: BudgetForm;
values: BudgetForm;
formMode: FormMode;
budgetMode: BudgetMode;
onSave: (f: BudgetForm) => Promise<void>;
}) {
const i18n = useI18n();

const [budgetForm, { Form, Field }] = createForm<BudgetForm>({
initialValues: props.initialValues,
initialValues: props.values,
validate: (values) => {
const errors: Record<string, string> = {};
if (values.auto_approve && values.budget_amount === "0") {
Expand All @@ -351,14 +382,20 @@ function NWCEditorForm(props: {
}
});

createEffect(() => {
if (props.values) {
setValue(budgetForm, "budget_amount", props.values.budget_amount);
}
});

const handleFormSubmit: SubmitHandler<BudgetForm> = async (
f: BudgetForm
) => {
// If this throws the message will be caught by the form
await props.onSave({
...f,
profileIndex: props.initialValues.profileIndex,
nwaString: props.initialValues.nwaString
profileIndex: props.values.profileIndex,
nwaString: props.values.nwaString
});
};

Expand All @@ -373,9 +410,7 @@ function NWCEditorForm(props: {
>
{(field, fieldProps) => (
<TextField
disabled={
props.initialValues?.connection_name !== ""
}
disabled={props.values?.connection_name !== ""}
value={field.value}
{...fieldProps}
name="name"
Expand Down
39 changes: 28 additions & 11 deletions src/routes/settings/Connections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,18 @@ function NwcDetails(props: {
</Show>
</Show>

<Button layout="small" intent="green" onClick={props.onEdit}>
{i18n.t("settings.connections.edit_budget")}
</Button>
<Show
when={props.profile.enabled}
fallback={
<InfoBox accent="red">
{i18n.t("settings.connections.disabled")}
</InfoBox>
}
>
<Button layout="small" intent="green" onClick={props.onEdit}>
{i18n.t("settings.connections.edit_budget")}
</Button>
</Show>

<Show
when={
Expand All @@ -170,16 +179,22 @@ function NwcDetails(props: {
</Button>
</Show>

<Button layout="small" onClick={confirmDelete}>
{i18n.t("settings.connections.delete_connection")}
</Button>
<Show when={props.profile.enabled}>
<Button layout="small" onClick={confirmDelete}>
{props.profile.tag === "Subscription"
? i18n.t("settings.connections.disable_connection")
: i18n.t("settings.connections.delete_connection")}
</Button>
</Show>
<ConfirmDialog
loading={false}
open={confirmOpen()}
onConfirm={deleteProfile}
onCancel={() => setConfirmOpen(false)}
>
{i18n.t("settings.connections.confirm_delete")}
{props.profile.tag === "Subscription"
? i18n.t("settings.connections.disable_connection_confirm")
: i18n.t("settings.connections.confirm_delete")}
</ConfirmDialog>
</VStack>
);
Expand Down Expand Up @@ -223,8 +238,10 @@ function Nwc() {
const [profileToOpen, setProfileToOpen] = createSignal<number>();

function editProfile(profile: NwcProfile) {
setProfileToOpen(profile.index);
setDialogOpen(true);
if (profile && typeof profile.index === "number") {
setProfileToOpen(profile.index);
setDialogOpen(true);
}
}

function createProfile() {
Expand Down Expand Up @@ -277,7 +294,7 @@ function Nwc() {
{(profile) => (
<Collapser
title={profile.name}
activityLight={"on"}
activityLight={profile.enabled ? "on" : "off"}
defaultOpen={profile.index === newConnection()}
>
<NwcDetails
Expand All @@ -294,7 +311,7 @@ function Nwc() {
open={dialogOpen()}
setOpen={handleToggleOpen}
title={
profileToOpen()
typeof profileToOpen() === "number"
? i18n.t("settings.connections.edit_connection")
: i18n.t("settings.connections.add_connection")
}
Expand Down
Loading