Fix Inspect Ledgers: stable checkbox, currency code, clean state
- Checkbox for "own transactions only" now lives outside the data-dependent rendering block — visible whenever the selected account has children, regardless of query results - Selected account stored as ID (number), not object reference, eliminating React re-render reference-equality issues - isGroupAccount derived via useMemo from stable primitives - ownOnly reset moved to onChange handler, not effects - Autocomplete uses isOptionEqualToValue for stable comparison - Currency code (SEK/EUR) shown in ledger header from org API Made-with: Cursor
This commit is contained in:
@@ -43,14 +43,12 @@ const YEARS = Array.from({ length: 15 }, (_, i) => currentYear - i)
|
||||
export default function InspectLedgersPage() {
|
||||
const { activeOrg } = useOrg()
|
||||
const [accounts, setAccounts] = useState<AccountOption[]>([])
|
||||
const [selectedAccount, setSelectedAccount] = useState<AccountOption | null>(null)
|
||||
// Type adapter for autocomplete which uses AccountWithDepth
|
||||
const [selectedAccountId, setSelectedAccountId] = useState<number | null>(null)
|
||||
const [year, setYear] = useState(currentYear)
|
||||
const [month, setMonth] = useState(0)
|
||||
const [data, setData] = useState<LedgerResponse | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [ownOnly, setOwnOnly] = useState(false)
|
||||
const [isGroupAccount, setIsGroupAccount] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg) return
|
||||
@@ -59,23 +57,22 @@ export default function InspectLedgersPage() {
|
||||
.catch(() => {})
|
||||
}, [activeOrg])
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg || !selectedAccount) return
|
||||
setOwnOnly(false)
|
||||
setIsGroupAccount(selectedAccount.hasChildren)
|
||||
}, [activeOrg, selectedAccount])
|
||||
const isGroupAccount = useMemo(() => {
|
||||
if (!selectedAccountId) return false
|
||||
return accounts.some(a => a.parentId === selectedAccountId)
|
||||
}, [selectedAccountId, accounts])
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg || !selectedAccount) return
|
||||
if (!activeOrg || !selectedAccountId) return
|
||||
setLoading(true)
|
||||
const ownParam = ownOnly ? '&ownOnly=true' : ''
|
||||
api.get<LedgerResponse>(
|
||||
`/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccount.id}&year=${year}&month=${month}${ownParam}`
|
||||
`/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccountId}&year=${year}&month=${month}${ownParam}`
|
||||
)
|
||||
.then(setData)
|
||||
.catch(() => setData(null))
|
||||
.finally(() => setLoading(false))
|
||||
}, [activeOrg, selectedAccount, year, month, ownOnly])
|
||||
}, [activeOrg, selectedAccountId, year, month, ownOnly])
|
||||
|
||||
interface AccountWithDepth extends AccountOption {
|
||||
depth: number
|
||||
@@ -123,8 +120,9 @@ export default function InspectLedgersPage() {
|
||||
options={accountsWithDepth}
|
||||
groupBy={a => a.accountType}
|
||||
getOptionLabel={a => a.name}
|
||||
value={selectedAccount ? accountsWithDepth.find(a => a.id === selectedAccount.id) ?? null : null}
|
||||
onChange={(_, v) => setSelectedAccount(v)}
|
||||
value={accountsWithDepth.find(a => a.id === selectedAccountId) ?? null}
|
||||
onChange={(_, v) => { setSelectedAccountId(v?.id ?? null); setOwnOnly(false) }}
|
||||
isOptionEqualToValue={(opt, val) => opt.id === val.id}
|
||||
sx={{ minWidth: 350, flex: 1 }}
|
||||
renderInput={params => <TextField {...params} label="Account" size="small" />}
|
||||
renderOption={(props, option) => (
|
||||
@@ -154,31 +152,36 @@ export default function InspectLedgersPage() {
|
||||
</FormControl>
|
||||
</Paper>
|
||||
|
||||
{!selectedAccount ? (
|
||||
{!selectedAccountId ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">Select an account to inspect its ledger entries.</Typography>
|
||||
</Paper>
|
||||
) : loading ? (
|
||||
<Skeleton height={400} />
|
||||
) : data && data.entries.length > 0 ? (
|
||||
) : (
|
||||
<>
|
||||
{isGroupAccount && (
|
||||
<Box sx={{ mb: 1 }}>
|
||||
<FormControlLabel
|
||||
control={<Checkbox size="small" checked={ownOnly} onChange={e => setOwnOnly(e.target.checked)} />}
|
||||
label={<Typography variant="body2" sx={{ fontSize: 12 }}>
|
||||
Show only this account's own transactions (exclude sub-accounts)
|
||||
</Typography>}
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<Skeleton height={400} />
|
||||
) : data && data.entries.length > 0 ? (
|
||||
<>
|
||||
<Box sx={{ mb: 1, display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 1 }}>
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
{data.entryCount} entries, {data.from} to {data.to}
|
||||
{isGroupAccount && !ownOnly && (
|
||||
{data.isGroup && !ownOnly && (
|
||||
<Typography component="span" sx={{ ml: 1, fontSize: 12, color: 'text.disabled' }}>
|
||||
({data.groupAccountCount} accounts)
|
||||
</Typography>
|
||||
)}
|
||||
</Typography>
|
||||
{isGroupAccount && (
|
||||
<FormControlLabel
|
||||
control={<Checkbox size="small" checked={ownOnly} onChange={e => setOwnOnly(e.target.checked)} />}
|
||||
label={<Typography variant="body2" sx={{ fontSize: 12 }}>
|
||||
Show only "{data.accountName}" own transactions (exclude sub-accounts)
|
||||
</Typography>}
|
||||
/>
|
||||
)}
|
||||
<Typography variant="body2" color="text.secondary" sx={{ fontStyle: 'italic' }}>
|
||||
Amounts in {activeOrg.currencyCode || 'presentation currency'}
|
||||
</Typography>
|
||||
@@ -320,6 +323,8 @@ export default function InspectLedgersPage() {
|
||||
<Typography color="text.secondary">No entries for this period.</Typography>
|
||||
</Paper>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user