Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/pages/accounting/closing-entries/CreateClosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
Expand All @@ -18,7 +19,7 @@ import { getConfiguration } from '@/lib/fineract-openapi'
import {
AccountingClosureApi,
OfficesApi,
type GetOfficesResponse,
type OfficeData,
} from '@/fineract-api'

// API clients
Expand All @@ -29,7 +30,7 @@ const CreateClosure = () => {
const navigate = useNavigate()

// Offices list + form state
const [offices, setOffices] = useState<GetOfficesResponse[]>([])
const [offices, setOffices] = useState<OfficeData[]>([])

const [formData, setFormData] = useState({
officeId: '',
Expand All @@ -42,7 +43,7 @@ const CreateClosure = () => {
const fetchOffices = async () => {
try {
const response = await officesApi.retrieveOffices()
setOffices(response.data || [])
setOffices(response.data as OfficeData[])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Retain the fallback to prevent potential runtime errors.

The removal of || [] reduces defensive coding. If response.data is undefined or null (e.g., unexpected API response structure), the state will be set to a non-array value, causing a runtime error at line 117 when calling offices.map(...).

Additionally, prefer type narrowing over type assertion (as) for better type safety.

🛡️ Proposed fix to restore the fallback
-        setOffices(response.data as OfficeData[])
+        setOffices((response.data as OfficeData[]) || [])
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
setOffices(response.data as OfficeData[])
setOffices((response.data as OfficeData[]) || [])
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/pages/accounting/closing-entries/CreateClosure.tsx` at line 46, Restore a
defensive fallback when setting offices: in the place where setOffices is called
(currently using setOffices(response.data as OfficeData[])), check that
response.data is an array (or otherwise truthy) and set an empty array fallback
to avoid setting non-array values that will break offices.map; prefer
type-narrowing (e.g., validate Array.isArray(response.data) or use response.data
?? []) instead of a raw type assertion so offices remains a safe OfficeData[]
for subsequent use (e.g., in offices.map).

} catch (err) {
console.error('Failed to fetch offices', err)
}
Expand Down