Add admin referral commission settings
This commit is contained in:
parent
9e8b20aaa6
commit
dfd8ec50c0
18
src/api/referrals.api.ts
Normal file
18
src/api/referrals.api.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import http from "./http"
|
||||||
|
|
||||||
|
export interface ReferralSettings {
|
||||||
|
commission_percent: string
|
||||||
|
updated_at: string
|
||||||
|
}
|
||||||
|
interface ReferralSettingsResponse { data: ReferralSettings }
|
||||||
|
|
||||||
|
export async function getReferralSettings(): Promise<ReferralSettings> {
|
||||||
|
const response = await http.get<ReferralSettingsResponse>("/admin/referrals/settings")
|
||||||
|
return response.data.data
|
||||||
|
}
|
||||||
|
export async function updateReferralSettings(commissionPercent: number): Promise<ReferralSettings> {
|
||||||
|
const response = await http.put<ReferralSettingsResponse>("/admin/referrals/settings", {
|
||||||
|
commission_percent: commissionPercent,
|
||||||
|
})
|
||||||
|
return response.data.data
|
||||||
|
}
|
||||||
|
|
@ -29,6 +29,7 @@ import { getMyProfile } from "../api/users.api";
|
||||||
import type { TeamMeProfile } from "../types/team.types";
|
import type { TeamMeProfile } from "../types/team.types";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { AppVersionsTab } from "./settings/AppVersionsTab";
|
import { AppVersionsTab } from "./settings/AppVersionsTab";
|
||||||
|
import { ReferralsTab } from "./settings/ReferralsTab";
|
||||||
import { SubscriptionPlansTab } from "./settings/SubscriptionPlansTab";
|
import { SubscriptionPlansTab } from "./settings/SubscriptionPlansTab";
|
||||||
import { ThemeModePreview } from "./settings/components/ThemeModePreview";
|
import { ThemeModePreview } from "./settings/components/ThemeModePreview";
|
||||||
import { useTheme } from "../contexts/ThemeContext";
|
import { useTheme } from "../contexts/ThemeContext";
|
||||||
|
|
@ -36,12 +37,14 @@ import { RolesListPage } from "./role-management/RolesListPage";
|
||||||
import { useSearchParams } from "react-router-dom";
|
import { useSearchParams } from "react-router-dom";
|
||||||
|
|
||||||
type SettingsTab =
|
type SettingsTab =
|
||||||
|
| "referrals"
|
||||||
| "subscription"
|
| "subscription"
|
||||||
| "app-versions"
|
| "app-versions"
|
||||||
| "security"
|
| "security"
|
||||||
| "appearance";
|
| "appearance";
|
||||||
|
|
||||||
const SETTINGS_TABS: SettingsTab[] = [
|
const SETTINGS_TABS: SettingsTab[] = [
|
||||||
|
"referrals",
|
||||||
"subscription",
|
"subscription",
|
||||||
"app-versions",
|
"app-versions",
|
||||||
"security",
|
"security",
|
||||||
|
|
@ -53,6 +56,7 @@ function isSettingsTab(value: string | null): value is SettingsTab {
|
||||||
}
|
}
|
||||||
|
|
||||||
const tabs: { id: SettingsTab; label: string; icon: typeof Shield }[] = [
|
const tabs: { id: SettingsTab; label: string; icon: typeof Shield }[] = [
|
||||||
|
{ id: "referrals", label: "Referrals", icon: CreditCard },
|
||||||
{ id: "subscription", label: "Subscription packages", icon: CreditCard },
|
{ id: "subscription", label: "Subscription packages", icon: CreditCard },
|
||||||
{ id: "app-versions", label: "App versions", icon: Smartphone },
|
{ id: "app-versions", label: "App versions", icon: Smartphone },
|
||||||
{ id: "security", label: "Security", icon: Shield },
|
{ id: "security", label: "Security", icon: Shield },
|
||||||
|
|
@ -446,7 +450,7 @@ export function SettingsPage() {
|
||||||
|
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
<nav className="flex gap-1 overflow-x-auto border-b border-grayScale-100 pb-px">
|
<nav className="flex gap-1 overflow-x-auto border-b border-grayScale-100 pb-px">
|
||||||
{tabs.map((tab) => {
|
{tabs.filter(tab => tab.id !== "referrals" || ["ADMIN", "SUPER_ADMIN"].includes(profile.team_role)).map((tab) => {
|
||||||
const Icon = tab.icon;
|
const Icon = tab.icon;
|
||||||
const active = activeTab === tab.id;
|
const active = activeTab === tab.id;
|
||||||
return (
|
return (
|
||||||
|
|
@ -469,6 +473,7 @@ export function SettingsPage() {
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<main className="min-h-[400px] min-w-0 w-full">
|
<main className="min-h-[400px] min-w-0 w-full">
|
||||||
|
{activeTab === "referrals" && (["ADMIN", "SUPER_ADMIN"].includes(profile.team_role) ? <ReferralsTab /> : <p>Administrator access is required to manage referral commissions.</p>)}
|
||||||
{activeTab === "subscription" && <SubscriptionPlansTab />}
|
{activeTab === "subscription" && <SubscriptionPlansTab />}
|
||||||
{activeTab === "app-versions" && <AppVersionsTab />}
|
{activeTab === "app-versions" && <AppVersionsTab />}
|
||||||
{activeTab === "security" && <SecurityTab memberId={profile.id} />}
|
{activeTab === "security" && <SecurityTab memberId={profile.id} />}
|
||||||
|
|
|
||||||
82
src/pages/settings/ReferralsTab.tsx
Normal file
82
src/pages/settings/ReferralsTab.tsx
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
import { useEffect, useState, type FormEvent } from "react"
|
||||||
|
import { toast } from "sonner"
|
||||||
|
import { getReferralSettings, updateReferralSettings } from "../../api/referrals.api"
|
||||||
|
import { notifyApiError } from "../../lib/apiErrors"
|
||||||
|
import { Button } from "../../components/ui/button"
|
||||||
|
import { Input } from "../../components/ui/input"
|
||||||
|
import { Card, CardContent, CardHeader, CardTitle } from "../../components/ui/card"
|
||||||
|
import { SpinnerIcon } from "../../components/ui/spinner-icon"
|
||||||
|
|
||||||
|
export function ReferralsTab() {
|
||||||
|
const [percent, setPercent] = useState("")
|
||||||
|
const [savedPercent, setSavedPercent] = useState("")
|
||||||
|
const [loading, setLoading] = useState(true)
|
||||||
|
const [saving, setSaving] = useState(false)
|
||||||
|
const [failed, setFailed] = useState(false)
|
||||||
|
const [attempt, setAttempt] = useState(0)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false
|
||||||
|
setLoading(true)
|
||||||
|
setFailed(false)
|
||||||
|
getReferralSettings().then(settings => {
|
||||||
|
if (!cancelled) {
|
||||||
|
setPercent(settings.commission_percent)
|
||||||
|
setSavedPercent(settings.commission_percent)
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
if (!cancelled) {
|
||||||
|
setFailed(true)
|
||||||
|
notifyApiError(error, "Failed to load referral settings.")
|
||||||
|
}
|
||||||
|
}).finally(() => { if (!cancelled) setLoading(false) })
|
||||||
|
return () => { cancelled = true }
|
||||||
|
}, [attempt])
|
||||||
|
|
||||||
|
async function save(event: FormEvent<HTMLFormElement>) {
|
||||||
|
event.preventDefault()
|
||||||
|
const value = Number(percent)
|
||||||
|
if (!percent.trim() || !Number.isFinite(value) || value < 0 || value > 100 ||
|
||||||
|
Math.abs(value * 100 - Math.round(value * 100)) > 0.000001) {
|
||||||
|
toast.error("Enter a percentage from 0 to 100 with up to two decimal places.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setSaving(true)
|
||||||
|
try {
|
||||||
|
const settings = await updateReferralSettings(value)
|
||||||
|
setPercent(settings.commission_percent)
|
||||||
|
setSavedPercent(settings.commission_percent)
|
||||||
|
toast.success("Referral commission updated.")
|
||||||
|
} catch (error) {
|
||||||
|
notifyApiError(error, "Failed to save referral settings.")
|
||||||
|
} finally { setSaving(false) }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) return <div className="flex justify-center p-12" role="status" aria-label="Loading referral settings"><SpinnerIcon className="h-6 w-6" /></div>
|
||||||
|
if (failed) return <div className="space-y-3"><p>Referral settings could not be loaded.</p><Button onClick={() => setAttempt(value => value + 1)}>Retry</Button></div>
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card className="rounded-[6px] border border-grayScale-100">
|
||||||
|
<CardHeader><CardTitle>Referral commissions</CardTitle></CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<form onSubmit={save} className="max-w-xl space-y-5">
|
||||||
|
<p className="text-sm text-grayScale-500">Learners earn a wallet commission on the first successful paid subscription purchased by someone they refer.</p>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label htmlFor="referral-commission" className="text-sm font-medium">Commission percentage (%)</label>
|
||||||
|
<Input id="referral-commission" type="number" min="0" max="100" step="0.01" required
|
||||||
|
value={percent} onChange={event => setPercent(event.target.value)} disabled={saving}
|
||||||
|
aria-describedby="referral-commission-help" />
|
||||||
|
<p id="referral-commission-help" className="text-sm text-grayScale-500">Set 0 to disable new commissions. Changes apply to future qualifying payments. Existing wallet credits keep their original rate.</p>
|
||||||
|
</div>
|
||||||
|
<div className="rounded-md bg-grayScale-50 p-4 text-sm">
|
||||||
|
Current saved rate: <strong>{savedPercent}%</strong>. Credits use the payment currency and are rounded to two decimal places.
|
||||||
|
</div>
|
||||||
|
<Button type="submit" disabled={saving || percent === savedPercent}>
|
||||||
|
{saving && <SpinnerIcon className="mr-2 h-4 w-4" />}
|
||||||
|
{saving ? "Saving..." : "Save commission"}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user