Skip to content

Commit f024e09

Browse files
committed
Fix canLoadMore logic
1 parent df5f788 commit f024e09

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

packages/connect-react/src/components/RemoteOptionsContainer.tsx

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
3434
setPage,
3535
] = useState<number>(0);
3636

37+
const [
38+
canLoadMore,
39+
setCanLoadMore,
40+
] = useState<boolean>(true);
41+
3742
const [
3843
context,
3944
setContext,
@@ -46,6 +51,7 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
4651
page: 0,
4752
prevContext: {},
4853
data: [],
54+
values: new Set(),
4955
})
5056

5157
const configuredPropsUpTo: Record<string, unknown> = {};
@@ -85,10 +91,6 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
8591
})
8692
}
8793

88-
const canLoadMore = () => {
89-
return Object.keys(pageable.prevContext || {}).length > 0
90-
}
91-
9294
// TODO handle error!
9395
const {
9496
isFetching, refetch,
@@ -132,22 +134,41 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
132134
}
133135
_options = options;
134136
}
135-
const data = [
136-
...pageable.data,
137-
..._options,
138-
]
139-
setPageable({
140-
page: page + 1,
141-
prevContext: res.context,
142-
data,
143-
})
137+
138+
const newOptions = []
139+
const allValues = new Set(pageable.values)
140+
for (const o of _options || []) {
141+
const value = typeof o === "string"
142+
? o
143+
: o.value
144+
if (allValues.has(value)) {
145+
continue
146+
}
147+
allValues.add(value)
148+
newOptions.push(o)
149+
}
150+
let data = pageable.data
151+
if (newOptions.length) {
152+
data = [
153+
...pageable.data,
154+
...newOptions,
155+
]
156+
setPageable({
157+
page: page + 1,
158+
prevContext: res.context,
159+
data,
160+
values: allValues,
161+
})
162+
} else {
163+
setCanLoadMore(false)
164+
}
144165
return data;
145166
},
146167
enabled: !!queryEnabled,
147168
});
148169

149170
const showLoadMoreButton = () => {
150-
return !isFetching && !error && canLoadMore()
171+
return !isFetching && !error && canLoadMore
151172
}
152173

153174
// TODO show error in different spot!

0 commit comments

Comments
 (0)