2026-05-28 22:06:15 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import { toast } from 'sonner';
|
|
|
|
|
import { api } from '@/api';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card';
|
|
|
|
|
import { Toggle } from './adminShared';
|
|
|
|
|
|
|
|
|
|
export default function BankSyncAdminCard() {
|
|
|
|
|
const [config, setConfig] = useState(null);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
|
const [enabled, setEnabled] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
api.bankSyncConfig()
|
|
|
|
|
.then(d => {
|
|
|
|
|
setConfig(d);
|
|
|
|
|
setEnabled(!!d.enabled);
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {})
|
|
|
|
|
.finally(() => setLoading(false));
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleSave = async () => {
|
|
|
|
|
setSaving(true);
|
|
|
|
|
try {
|
|
|
|
|
const result = await api.setBankSyncConfig({ enabled });
|
|
|
|
|
setConfig(result);
|
|
|
|
|
setEnabled(!!result.enabled);
|
|
|
|
|
toast.success(enabled ? 'Bank sync enabled.' : 'Bank sync disabled.');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
toast.error(err.message || 'Failed to update bank sync setting.');
|
|
|
|
|
} finally {
|
|
|
|
|
setSaving(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardContent className="py-8 text-center text-muted-foreground text-sm">
|
|
|
|
|
Loading…
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const changed = enabled !== !!config?.enabled;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader className="pb-4">
|
|
|
|
|
<CardTitle>Bank Sync (SimpleFIN)</CardTitle>
|
|
|
|
|
<p className="text-sm text-muted-foreground mt-1">
|
|
|
|
|
Allow users to connect their own SimpleFIN Bridge account to sync
|
|
|
|
|
read-only bank transactions. Each user manages their own connection
|
|
|
|
|
from the Data page — no bank credentials are stored.
|
|
|
|
|
</p>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent className="space-y-5">
|
|
|
|
|
|
|
|
|
|
{/* Enable toggle */}
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-sm font-medium">Allow users to connect SimpleFIN</p>
|
|
|
|
|
<p className="text-xs text-muted-foreground mt-0.5">
|
|
|
|
|
When enabled, users see a Bank Sync section on their Data page.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<Toggle
|
|
|
|
|
checked={enabled}
|
|
|
|
|
onChange={v => setEnabled(v)}
|
|
|
|
|
label="Enable bank sync"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex justify-end pt-2">
|
2026-05-28 22:18:20 -05:00
|
|
|
<Button onClick={handleSave} disabled={saving || !changed}>
|
2026-05-28 22:06:15 -05:00
|
|
|
{saving ? 'Saving…' : 'Save'}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|