Amitkasar's picture
Refactor backend: split routes, add services & tests
ef865f2
Raw History Blame Contribute Delete
12.2 kB
import React, { useState, useEffect } from 'react';
import { Settings, Save, Trash2, Key, User, Lock, Plus, X, RefreshCw, Server, Database } from 'lucide-react';
import { useAppContext } from './AppContext';
const ProfilePage = () => {
const {
activeSpecId, activeSpecName, storedCredentials, saveProfile, addToast,
} = useAppContext();
const [profileForm, setProfileForm] = useState({ name: '', email: '', password: '', token: '' });
const [extraKvs, setExtraKvs] = useState([]);
const [isSaving, setIsSaving] = useState(false);
// Sync stored credentials into local form
useEffect(() => {
if (!storedCredentials) return;
setProfileForm({
name: storedCredentials.name || '',
email: storedCredentials.email || storedCredentials.username || '',
password: '', // Don't prefill password
token: storedCredentials.access_token || storedCredentials.token || ''
});
const standardKeys = new Set(['name', 'email', 'username', 'password', 'access_token', 'token', 'refresh_token', 'id_token']);
const kvs = Object.entries(storedCredentials)
.filter(([k, v]) => !standardKeys.has(k) && k !== '_clear')
.map(([k, v]) => ({ key: k, val: String(v) }));
setExtraKvs(kvs);
}, [storedCredentials]);
const handleSave = async () => {
if (!activeSpecId) return;
setIsSaving(true);
try {
const payload = {
name: profileForm.name,
username: profileForm.email,
extra: {}
};
if (profileForm.password) payload.password = profileForm.password;
if (profileForm.token) payload.extra.token = profileForm.token;
extraKvs.forEach(kv => {
if (kv.key && kv.val) payload.extra[kv.key] = kv.val;
});
await saveProfile(payload);
} catch (err) {
console.error("Save failed", err);
addToast('error', err.message || "Failed to save profile.");
} finally {
setIsSaving(false);
}
};
if (!activeSpecId) {
return (
<div className="flex-1 flex flex-col items-center justify-center p-8 text-center bg-[var(--bg-primary)] animate-in fade-in duration-500">
<div className="p-8 bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded-3xl shadow-2xl max-w-md w-full animate-in zoom-in duration-300">
<div className="w-16 h-16 bg-[var(--bg-tertiary)] border border-[var(--border-subtle)] rounded-2xl flex items-center justify-center mx-auto mb-6">
<Server size={32} className="text-[var(--text-secondary)] opacity-50" />
</div>
<h2 className="text-2xl font-semibold text-[var(--text-primary)] mb-2">No Collection Selected</h2>
<p className="text-[var(--text-secondary)] text-sm mb-8 leading-relaxed">
Test user profiles are isolated per collection. Please select an API specification first to manage its credentials.
</p>
<button
onClick={() => window.dispatchEvent(new CustomEvent('nav-change', { detail: 'collections' }))}
className="w-full py-3 bg-[var(--accent-gradient)] hover:opacity-90 text-white text-sm font-bold rounded-xl transition shadow-lg flex items-center justify-center space-x-2"
>
<Database size={16} />
<span>Browse Collections</span>
</button>
</div>
</div>
);
}
return (
<div className="flex-1 flex flex-col bg-[var(--bg-primary)] overflow-hidden animate-in fade-in slide-in-from-bottom-2 duration-300">
{/* Header */}
<div className="h-14 border-b border-[var(--border-subtle)] flex items-center justify-between px-6 bg-[var(--bg-secondary)]">
<div className="flex items-center space-x-4">
<h1 className="text-lg font-medium text-[var(--text-primary)] flex items-center space-x-2">
<Settings size={20} className="text-[var(--accent-primary)]" />
<span>Test User Profile</span>
</h1>
<div className="h-6 w-px bg-[var(--border-subtle)]" />
<div className="text-xs text-[var(--text-secondary)]">
Current Spec: <span className="text-[var(--accent-primary)] font-mono">{activeSpecName}</span>
</div>
</div>
<button
onClick={handleSave}
disabled={isSaving}
className="bg-[var(--accent-gradient)] hover:opacity-90 text-white px-6 py-1.5 text-xs font-semibold rounded-lg flex items-center space-x-2 transition shadow-lg disabled:opacity-50"
>
{isSaving ? <RefreshCw size={14} className="animate-spin" /> : <Save size={14} />}
<span>Save Profile</span>
</button>
</div>
<div className="flex-1 overflow-y-auto p-8">
<div className="max-w-3xl mx-auto space-y-8">
{/* Core Credentials */}
<section className="space-y-4">
<div className="flex items-center space-x-3 mb-6">
<div className="p-2 bg-[var(--accent-primary)]/10 border border-[var(--accent-primary)]/20 rounded-lg">
<Lock size={18} className="text-[var(--accent-primary)]" />
</div>
<h3 className="text-sm font-semibold text-[var(--text-primary)]">Core Authentication</h3>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 bg-[var(--bg-secondary)] p-6 rounded-2xl border border-[var(--border-subtle)] shadow-sm">
<div className="space-y-2">
<label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
Full Name
</label>
<input
type="text"
className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors"
value={profileForm.name}
onChange={e => setProfileForm({...profileForm, name: e.target.value})}
placeholder="e.g. John Doe"
/>
</div>
<div className="space-y-2">
<label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
Email / Username
</label>
<input
type="text"
className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors"
value={profileForm.email}
onChange={e => setProfileForm({...profileForm, email: e.target.value})}
placeholder="e.g. john.doe@example.com"
/>
</div>
<div className="space-y-2">
<label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
Password
</label>
<input
type="password"
className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors"
value={profileForm.password}
onChange={e => setProfileForm({...profileForm, password: e.target.value})}
placeholder="Leave blank to keep current"
/>
</div>
<div className="space-y-2">
<label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
Static Access Token
</label>
<input
type="text"
className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors"
value={profileForm.token}
onChange={e => setProfileForm({...profileForm, token: e.target.value})}
placeholder="Skip login flow with a manual token"
/>
</div>
</div>
</section>
{/* Extra Variables */}
<section className="space-y-4">
<div className="flex items-center justify-between mb-4 px-1">
<div className="flex items-center space-x-3">
<div className="p-2 bg-[var(--accent-secondary)]/10 border border-[var(--accent-secondary)]/20 rounded-lg">
<Key size={18} className="text-[var(--accent-secondary)]" />
</div>
<h3 className="text-sm font-semibold text-[var(--text-primary)]">Custom Fields & Context</h3>
</div>
<button
onClick={() => setExtraKvs([...extraKvs, {key: '', val: ''}])}
className="text-xs bg-[var(--bg-tertiary)] hover:bg-[var(--bg-secondary)] text-[var(--text-primary)] px-3 py-1.5 rounded-lg border border-[var(--border-subtle)] flex items-center gap-1.5 transition shadow-sm"
>
<Plus size={14} />
<span>Add Variables</span>
</button>
</div>
{extraKvs.length === 0 ? (
<div className="py-16 bg-[var(--bg-secondary)] border-2 border-dashed border-[var(--border-subtle)] rounded-2xl text-center shadow-inner">
<p className="text-[var(--text-secondary)] text-sm italic">No custom variables defined. Use these for OTPs, User IDs, or custom test data pointers.</p>
</div>
) : (
<div className="grid grid-cols-1 gap-3">
{extraKvs.map((kv, i) => (
<div key={i} className="flex space-x-3 group animate-in slide-in-from-left-2 duration-300">
<div className="flex-1 bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded-xl overflow-hidden shadow-sm flex items-center group-hover:border-[var(--text-secondary)]/30 transition-colors">
<div className="bg-[var(--bg-primary)] border-r border-[var(--border-subtle)] px-3 py-2 flex items-center justify-center">
<span className="text-[10px] text-[var(--text-secondary)] font-bold uppercase tracking-tighter w-4 text-center">KV</span>
</div>
<input
type="text"
placeholder="Key"
className="w-1/3 bg-transparent border-r border-[var(--border-subtle)] text-[var(--accent-primary)] px-4 py-2.5 text-xs outline-none focus:bg-[var(--bg-tertiary)] transition-colors font-mono"
value={kv.key}
onChange={e => { const n = [...extraKvs]; n[i].key = e.target.value; setExtraKvs(n); }}
/>
<input
type="text"
placeholder="Value"
className="flex-1 bg-transparent text-[var(--accent-secondary)] px-4 py-2.5 text-xs outline-none focus:bg-[var(--bg-tertiary)] transition-colors font-mono"
value={kv.val}
onChange={e => { const n = [...extraKvs]; n[i].val = e.target.value; setExtraKvs(n); }}
/>
</div>
<button
onClick={() => setExtraKvs(extraKvs.filter((_, idx) => idx !== i))}
className="text-[var(--text-secondary)] hover:text-red-400 p-2 opacity-0 group-hover:opacity-100 transition-all self-center"
>
<Trash2 size={16} />
</button>
</div>
))}
</div>
)}
</section>
</div>
</div>
</div>
);
};
export default ProfilePage;