fix profile index bugs

This commit is contained in:
Paul Miller
2024-05-09 09:53:57 -05:00
parent 929162ec8a
commit dff7563ce5
2 changed files with 37 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ import {
SubmitHandler SubmitHandler
} from "@modular-forms/solid"; } from "@modular-forms/solid";
import { BudgetPeriod, NwcProfile, TagItem } from "@mutinywallet/mutiny-wasm"; import { BudgetPeriod, NwcProfile, TagItem } from "@mutinywallet/mutiny-wasm";
import { createAsync } from "@solidjs/router";
import { import {
createMemo, createMemo,
createResource, createResource,
@@ -112,7 +113,7 @@ export function NWCEditor(props: {
const formMode = createMemo(() => { const formMode = createMemo(() => {
const mode: "createnwa" | "createnwc" | "editnwc" = nwa() const mode: "createnwa" | "createnwc" | "editnwc" = nwa()
? "createnwa" ? "createnwa"
: props.initialProfileIndex : typeof props.initialProfileIndex === "number"
? "editnwc" ? "editnwc"
: "createnwc"; : "createnwc";
return mode; return mode;
@@ -173,7 +174,7 @@ export function NWCEditor(props: {
NwcProfile | undefined NwcProfile | undefined
> = async (index, _last) => { > = async (index, _last) => {
console.log("fetching nwc profile", index); console.log("fetching nwc profile", index);
if (!index) return undefined; if (typeof index !== "number") return undefined;
try { try {
const profile: NwcProfile | undefined = const profile: NwcProfile | undefined =
@@ -212,7 +213,8 @@ export function NWCEditor(props: {
async function saveConnection(f: BudgetForm) { async function saveConnection(f: BudgetForm) {
let newProfile: NwcProfile | undefined = undefined; 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") { if (!f.auto_approve || f.budget_amount === "0") {
newProfile = await sw.set_nwc_profile_require_approval( newProfile = await sw.set_nwc_profile_require_approval(
f.profileIndex f.profileIndex
@@ -259,6 +261,27 @@ export function NWCEditor(props: {
} }
} }
const planDetails = createAsync(async () => {
try {
const plans = await sw.get_subscription_plans();
if (plans.length) {
return plans[0];
}
} catch (e) {
console.error(e);
}
});
const initialBudget = createMemo(() => {
if (profile()?.tag === "Subscription" && planDetails()) {
return planDetails()?.amount_sat.toString() || "16000";
}
if (profile()?.budget_amount) {
return profile()?.budget_amount?.toString() || "0";
}
return "0";
});
return ( return (
<Switch> <Switch>
<Match when={formMode() === "createnwc"}> <Match when={formMode() === "createnwc"}>
@@ -308,12 +331,13 @@ export function NWCEditor(props: {
initialValues={{ initialValues={{
connection_name: profile()?.name ?? "", connection_name: profile()?.name ?? "",
auto_approve: !profile()?.require_approval, auto_approve: !profile()?.require_approval,
budget_amount: budget_amount: initialBudget(),
profile()?.budget_amount?.toString() ?? "0",
interval: interval:
(profile() profile()?.tag === "Subscription"
?.budget_period as BudgetForm["interval"]) ?? ? "Month"
"Day", : (profile()?.budget_period?.toString() as BudgetForm["interval"]) ||
"Day",
profileIndex: profile()?.index profileIndex: profile()?.index
}} }}
formMode={formMode()} formMode={formMode()}

View File

@@ -223,8 +223,10 @@ function Nwc() {
const [profileToOpen, setProfileToOpen] = createSignal<number>(); const [profileToOpen, setProfileToOpen] = createSignal<number>();
function editProfile(profile: NwcProfile) { function editProfile(profile: NwcProfile) {
setProfileToOpen(profile.index); if (profile && typeof profile.index === "number") {
setDialogOpen(true); setProfileToOpen(profile.index);
setDialogOpen(true);
}
} }
function createProfile() { function createProfile() {
@@ -294,7 +296,7 @@ function Nwc() {
open={dialogOpen()} open={dialogOpen()}
setOpen={handleToggleOpen} setOpen={handleToggleOpen}
title={ title={
profileToOpen() typeof profileToOpen() === "number"
? i18n.t("settings.connections.edit_connection") ? i18n.t("settings.connections.edit_connection")
: i18n.t("settings.connections.add_connection") : i18n.t("settings.connections.add_connection")
} }