Fixed billing ui
This commit is contained in:
+82
-73
@@ -2253,53 +2253,59 @@ function App() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const saveWorkspaceSettings = async () => {
|
||||||
|
const response = await apiFetch('/workspace', authToken, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
...workspaceForm,
|
||||||
|
billingPlan: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingPlan,
|
||||||
|
billingInterval: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingInterval,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(await readErrorMessage(response, 'Unable to save flock settings.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = (await readJsonSafely<{ workspace?: Workspace }>(response)) ?? {};
|
||||||
|
|
||||||
|
if (!data.workspace) {
|
||||||
|
throw new Error('Unable to save flock settings.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const savedWorkspace = data.workspace;
|
||||||
|
|
||||||
|
setWorkspace(savedWorkspace);
|
||||||
|
setAuthSession((current) =>
|
||||||
|
current
|
||||||
|
? {
|
||||||
|
...current,
|
||||||
|
activeWorkspace: savedWorkspace,
|
||||||
|
workspaces: current.workspaces.map((entry) =>
|
||||||
|
entry.workspace.id === savedWorkspace.id ? { ...entry, workspace: savedWorkspace } : entry,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
: current,
|
||||||
|
);
|
||||||
|
setWorkspaceForm({
|
||||||
|
name: savedWorkspace.name,
|
||||||
|
workspaceType: savedWorkspace.workspaceType,
|
||||||
|
billingEmail: savedWorkspace.billingEmail ?? '',
|
||||||
|
billingPlan: isHouseholdPlan(savedWorkspace.billingPlan) ? savedWorkspace.billingPlan : 'household_basic',
|
||||||
|
billingInterval: savedWorkspace.billingInterval,
|
||||||
|
});
|
||||||
|
|
||||||
|
return savedWorkspace;
|
||||||
|
};
|
||||||
|
|
||||||
const handleWorkspaceSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
const handleWorkspaceSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
setError('');
|
setError('');
|
||||||
setSavingWorkspace(true);
|
setSavingWorkspace(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await apiFetch('/workspace', authToken, {
|
await saveWorkspaceSettings();
|
||||||
method: 'PUT',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
...workspaceForm,
|
|
||||||
billingPlan: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingPlan,
|
|
||||||
billingInterval: workspaceForm.workspaceType === 'rescue' ? undefined : workspaceForm.billingInterval,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(await readErrorMessage(response, 'Unable to save flock settings.'));
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = (await readJsonSafely<{ workspace?: Workspace }>(response)) ?? {};
|
|
||||||
|
|
||||||
if (!data.workspace) {
|
|
||||||
throw new Error('Unable to save flock settings.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const savedWorkspace = data.workspace;
|
|
||||||
|
|
||||||
setWorkspace(savedWorkspace);
|
|
||||||
setAuthSession((current) =>
|
|
||||||
current
|
|
||||||
? {
|
|
||||||
...current,
|
|
||||||
activeWorkspace: savedWorkspace,
|
|
||||||
workspaces: current.workspaces.map((entry) =>
|
|
||||||
entry.workspace.id === savedWorkspace.id ? { ...entry, workspace: savedWorkspace } : entry,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
: current,
|
|
||||||
);
|
|
||||||
setWorkspaceForm({
|
|
||||||
name: savedWorkspace.name,
|
|
||||||
workspaceType: savedWorkspace.workspaceType,
|
|
||||||
billingEmail: savedWorkspace.billingEmail ?? '',
|
|
||||||
billingPlan: isHouseholdPlan(savedWorkspace.billingPlan) ? savedWorkspace.billingPlan : 'household_basic',
|
|
||||||
billingInterval: savedWorkspace.billingInterval,
|
|
||||||
});
|
|
||||||
} catch (workspaceError) {
|
} catch (workspaceError) {
|
||||||
setError(workspaceError instanceof Error ? workspaceError.message : 'Unable to save flock settings.');
|
setError(workspaceError instanceof Error ? workspaceError.message : 'Unable to save flock settings.');
|
||||||
} finally {
|
} finally {
|
||||||
@@ -2406,12 +2412,17 @@ function App() {
|
|||||||
|
|
||||||
setError('');
|
setError('');
|
||||||
setBillingRedirecting(true);
|
setBillingRedirecting(true);
|
||||||
|
setSavingWorkspace(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const savedWorkspace = await saveWorkspaceSettings();
|
||||||
|
const billingPlan = isHouseholdPlan(savedWorkspace.billingPlan) ? savedWorkspace.billingPlan : workspaceForm.billingPlan;
|
||||||
|
const billingInterval = savedWorkspace.billingInterval;
|
||||||
|
|
||||||
const response = await apiFetch('/billing/checkout-session', authToken, {
|
const response = await apiFetch('/billing/checkout-session', authToken, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ billingPlan: workspace.billingPlan, billingInterval: workspace.billingInterval }),
|
body: JSON.stringify({ billingPlan, billingInterval }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -2428,6 +2439,7 @@ function App() {
|
|||||||
} catch (billingError) {
|
} catch (billingError) {
|
||||||
setError(billingError instanceof Error ? billingError.message : 'Unable to start Stripe checkout.');
|
setError(billingError instanceof Error ? billingError.message : 'Unable to start Stripe checkout.');
|
||||||
setBillingRedirecting(false);
|
setBillingRedirecting(false);
|
||||||
|
setSavingWorkspace(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -3410,7 +3422,7 @@ function App() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{workspace?.workspaceType !== 'rescue' ? (
|
{workspace?.workspaceType !== 'rescue' ? (
|
||||||
<form className="form-panel" onSubmit={handleWorkspaceSubmit}>
|
<div className="form-panel">
|
||||||
<label>
|
<label>
|
||||||
Household plan
|
Household plan
|
||||||
<select
|
<select
|
||||||
@@ -3451,10 +3463,33 @@ function App() {
|
|||||||
placeholder="Optional for billing and account management"
|
placeholder="Optional for billing and account management"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<button className="primary-button" type="submit" disabled={savingWorkspace}>
|
<div className="billing-inline-action">
|
||||||
{savingWorkspace ? 'Saving billing...' : 'Save billing settings'}
|
<div>
|
||||||
</button>
|
<strong>{workspace?.stripeSubscriptionId ? 'Manage household billing' : 'Start household subscription'}</strong>
|
||||||
</form>
|
<span>
|
||||||
|
{workspace?.stripeSubscriptionId
|
||||||
|
? 'Open Stripe to update payment methods, invoices, cancellation, or plan changes for this flock.'
|
||||||
|
: 'Continue to Stripe with the selected plan and frequency. Billing is tracked separately for each household flock.'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
{activeMembership?.role === 'owner' || activeMembership?.role === 'assistant' ? (
|
||||||
|
<button
|
||||||
|
className="primary-button"
|
||||||
|
type="button"
|
||||||
|
onClick={workspace?.stripeSubscriptionId ? handleOpenBillingPortal : handleStartBillingCheckout}
|
||||||
|
disabled={billingRedirecting || savingWorkspace || !workspace}
|
||||||
|
>
|
||||||
|
{billingRedirecting || savingWorkspace
|
||||||
|
? 'Opening Stripe...'
|
||||||
|
: workspace?.stripeSubscriptionId
|
||||||
|
? 'Manage billing'
|
||||||
|
: 'Start subscription'}
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<small className="muted">Ask a flock owner or assistant to manage billing for this flock.</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
<div className="summary-grid">
|
<div className="summary-grid">
|
||||||
<article className="summary-card">
|
<article className="summary-card">
|
||||||
@@ -3508,32 +3543,6 @@ function App() {
|
|||||||
: 'Current bird count in this flock.'}
|
: 'Current bird count in this flock.'}
|
||||||
</span>
|
</span>
|
||||||
</article>
|
</article>
|
||||||
{workspace?.workspaceType !== 'rescue' ? (
|
|
||||||
<article className="summary-card">
|
|
||||||
<strong>{workspace?.stripeSubscriptionId ? 'Manage household billing' : 'Start household subscription'}</strong>
|
|
||||||
<span>
|
|
||||||
{workspace?.stripeSubscriptionId
|
|
||||||
? 'Open Stripe to update payment methods, invoices, cancellation, or plan changes for this flock.'
|
|
||||||
: 'Start Stripe Checkout for this flock. Billing is tracked separately for each household flock.'}
|
|
||||||
</span>
|
|
||||||
{activeMembership?.role === 'owner' || activeMembership?.role === 'assistant' ? (
|
|
||||||
<button
|
|
||||||
className="secondary-button"
|
|
||||||
type="button"
|
|
||||||
onClick={workspace?.stripeSubscriptionId ? handleOpenBillingPortal : handleStartBillingCheckout}
|
|
||||||
disabled={billingRedirecting || !workspace}
|
|
||||||
>
|
|
||||||
{billingRedirecting
|
|
||||||
? 'Opening Stripe...'
|
|
||||||
: workspace?.stripeSubscriptionId
|
|
||||||
? 'Manage billing'
|
|
||||||
: 'Start subscription'}
|
|
||||||
</button>
|
|
||||||
) : (
|
|
||||||
<small className="muted">Ask a flock owner or assistant to manage billing for this flock.</small>
|
|
||||||
)}
|
|
||||||
</article>
|
|
||||||
) : null}
|
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
|
|||||||
@@ -492,6 +492,25 @@ textarea {
|
|||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.billing-inline-action {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 1rem;
|
||||||
|
padding-top: 0.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.billing-inline-action div {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.billing-inline-action span {
|
||||||
|
color: var(--muted);
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
|
||||||
.overview-alert-actions {
|
.overview-alert-actions {
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: end;
|
justify-content: end;
|
||||||
|
|||||||
Reference in New Issue
Block a user