feat: add feature limit enforcement UI (Phase 4 Batch 1)
Implementiert User-freundliches Limit-Feedback für Daten-Einträge:
- Button wird deaktiviert wenn Limit erreicht
- Hover-Tooltip erklärt warum ("Limit erreicht X/Y")
- Button-Text zeigt "🔒 Limit erreicht"
- Error-Handling für alle API-Fehler
- Usage-Badge wird nach Speichern aktualisiert
Betrifft: Weight, Circumference, Caliper Screens
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
cf522190c6
commit
0f019f87a4
3 files changed
+131
-21
No files matched your search
@@ -4,6 +4,7 @@ import { useNavigate } from 'react-router-dom'
|
||||
import { api } from '../utils/api'
|
||||
import { calcBodyFat, getBfCategory, METHOD_POINTS } from '../utils/calc'
|
||||
import { CALIPER_POINTS, CALIPER_METHODS } from '../utils/guideData'
|
||||
import UsageBadge from '../components/UsageBadge'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
function emptyForm() {
|
||||
@@ -15,7 +16,7 @@ function emptyForm() {
|
||||
}
|
||||
}
|
||||
|
||||
function CaliperForm({ form, setForm, profile, onSave, onCancel, saveLabel='Speichern' }) {
|
||||
function CaliperForm({ form, setForm, profile, onSave, onCancel, saveLabel='Speichern', saving=false, error=null, usage=null }) {
|
||||
const sex = profile?.sex||'m'
|
||||
const age = profile?.dob ? Math.floor((Date.now()-new Date(profile.dob))/(365.25*24*3600*1000)) : 30
|
||||
const weight = form.weight || 80
|
||||
@@ -65,8 +66,21 @@ function CaliperForm({ form, setForm, profile, onSave, onCancel, saveLabel='Spei
|
||||
<input type="text" className="form-input" placeholder="optional" value={form.notes||''} onChange={e=>set('notes',e.target.value)}/>
|
||||
<span className="form-unit"/>
|
||||
</div>
|
||||
{error && (
|
||||
<div style={{padding:'10px',background:'var(--danger-bg)',border:'1px solid var(--danger)',borderRadius:8,fontSize:13,color:'var(--danger)',marginBottom:8}}>
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
<div style={{display:'flex',gap:6,marginTop:8}}>
|
||||
<button className="btn btn-primary" style={{flex:1}} onClick={()=>onSave(bfPct, sex)}>{saveLabel}</button>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
style={{flex:1, cursor: (usage && !usage.allowed) ? 'not-allowed' : 'pointer'}}
|
||||
onClick={()=>onSave(bfPct, sex)}
|
||||
disabled={saving || (usage && !usage.allowed)}
|
||||
title={usage && !usage.allowed ? `Limit erreicht (${usage.used}/${usage.limit}). Kontaktiere den Admin oder warte bis zum nächsten Reset.` : ''}
|
||||
>
|
||||
{(usage && !usage.allowed) ? '🔒 Limit erreicht' : saveLabel}
|
||||
</button>
|
||||
{onCancel && <button className="btn btn-secondary" style={{flex:1}} onClick={onCancel}><X size={13}/> Abbrechen</button>}
|
||||
</div>
|
||||
</div>
|
||||
@@ -78,12 +92,26 @@ export default function CaliperScreen() {
|
||||
const [profile, setProfile] = useState(null)
|
||||
const [form, setForm] = useState(emptyForm())
|
||||
const [editing, setEditing] = useState(null)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [saved, setSaved] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
const [caliperUsage, setCaliperUsage] = useState(null) // Phase 4: Usage badge
|
||||
const nav = useNavigate()
|
||||
|
||||
const load = () => Promise.all([api.listCaliper(), api.getProfile()])
|
||||
.then(([e,p])=>{ setEntries(e); setProfile(p) })
|
||||
useEffect(()=>{ load() },[])
|
||||
|
||||
const loadUsage = () => {
|
||||
api.getFeatureUsage().then(features => {
|
||||
const caliperFeature = features.find(f => f.feature_id === 'caliper_entries')
|
||||
setCaliperUsage(caliperFeature)
|
||||
}).catch(err => console.error('Failed to load usage:', err))
|
||||
}
|
||||
|
||||
useEffect(()=>{
|
||||
load()
|
||||
loadUsage()
|
||||
},[])
|
||||
|
||||
const buildPayload = (f, bfPct, sex) => {
|
||||
const weight = profile?.weight || null
|
||||
@@ -97,11 +125,23 @@ export default function CaliperScreen() {
|
||||
}
|
||||
|
||||
const handleSave = async (bfPct, sex) => {
|
||||
const payload = buildPayload(form, bfPct, sex)
|
||||
await api.upsertCaliper(payload)
|
||||
setSaved(true); await load()
|
||||
setTimeout(()=>setSaved(false),2000)
|
||||
setForm(emptyForm())
|
||||
setSaving(true)
|
||||
setError(null)
|
||||
try {
|
||||
const payload = buildPayload(form, bfPct, sex)
|
||||
await api.upsertCaliper(payload)
|
||||
setSaved(true)
|
||||
await load()
|
||||
await loadUsage() // Reload usage after save
|
||||
setTimeout(()=>setSaved(false),2000)
|
||||
setForm(emptyForm())
|
||||
} catch (err) {
|
||||
console.error('Save failed:', err)
|
||||
setError(err.message || 'Fehler beim Speichern')
|
||||
setTimeout(()=>setError(null), 5000)
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleUpdate = async (bfPct, sex) => {
|
||||
@@ -125,9 +165,13 @@ export default function CaliperScreen() {
|
||||
</div>
|
||||
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Neue Messung</div>
|
||||
<div className="card-title badge-container-right">
|
||||
<span>Neue Messung</span>
|
||||
{caliperUsage && <UsageBadge {...caliperUsage} />}
|
||||
</div>
|
||||
<CaliperForm form={form} setForm={setForm} profile={profile}
|
||||
onSave={handleSave} saveLabel={saved?'✓ Gespeichert!':'Speichern'}/>
|
||||
onSave={handleSave} saveLabel={saved?'✓ Gespeichert!':'Speichern'}
|
||||
saving={saving} error={error} usage={caliperUsage}/>
|
||||
</div>
|
||||
|
||||
<div className="section-gap">
|
||||
|
||||
Reference in new issue
Block a user