Skip to content

Commit e4edb74

Browse files
committed
fix: Add type conversion for branch_id comparison to fix 'Unknown Branch' issue
1 parent f0bbe8a commit e4edb74

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/context/BranchContext.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export const BranchProvider = ({ children }) => {
5757
const data = await response.json();
5858

5959
if (data.success && data.data && data.data.branches) {
60+
console.log('🏢 Fetched branches:', data.data.branches);
61+
console.log('🏢 First branch branch_id type:', typeof data.data.branches[0]?.branch_id);
6062
setBranches(data.data.branches);
6163
} else {
6264
setBranches([]);
@@ -96,12 +98,28 @@ export const BranchProvider = ({ children }) => {
9698
isLocked: isLocked
9799
});
98100

101+
// Find the selected branch with proper type comparison
102+
const selectedBranch = Array.isArray(branches) ?
103+
branches.find(b => {
104+
// Compare as numbers (convert both sides)
105+
const branchId = typeof b.branch_id === 'string' ? parseInt(b.branch_id, 10) : b.branch_id;
106+
const selected = typeof selectedBranchId === 'string' ?
107+
(selectedBranchId === 'All' ? 'All' : parseInt(selectedBranchId, 10)) :
108+
selectedBranchId;
109+
110+
console.log(`🔍 Comparing branch ${b.branch_name}: branchId=${branchId} (${typeof branchId}) vs selected=${selected} (${typeof selected})`);
111+
return branchId === selected;
112+
}) || null
113+
: null;
114+
115+
console.log('🎯 Selected branch:', selectedBranch);
116+
99117
const value = {
100118
selectedBranchId,
101119
setSelectedBranchId: handleSetSelectedBranchId,
102120
branches,
103121
loading,
104-
selectedBranch: Array.isArray(branches) ? branches.find(b => b.branch_id === selectedBranchId) || null : null,
122+
selectedBranch: selectedBranch,
105123
isLocked: isLocked
106124
};
107125

0 commit comments

Comments
 (0)