Skip to content

Commit

Permalink
fix budget enable / disable for subscriptions, better disable descrip…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
futurepaul committed May 9, 2024
1 parent dff7563 commit 87b2709
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 56 deletions.
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
107 changes: 59 additions & 48 deletions src/components/NWCEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
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 @@ -261,20 +263,21 @@ export function NWCEditor(props: {
}
}

const planDetails = createAsync(async () => {
try {
const plans = await sw.get_subscription_plans();
if (plans.length) {
return plans[0];
// 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";
}
} 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";
Expand All @@ -286,7 +289,7 @@ export function NWCEditor(props: {
<Switch>
<Match when={formMode() === "createnwc"}>
<NWCEditorForm
initialValues={{
values={{
connection_name: "",
auto_approve: false,
budget_amount: "0",
Expand All @@ -304,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 @@ -322,48 +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: 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>
</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 @@ -375,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 @@ -397,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
29 changes: 22 additions & 7 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

0 comments on commit 87b2709

Please sign in to comment.