Skip to content

Commit d3925e6

Browse files
committed
Fetch more data when click on show more on nav search bar
1 parent 93c2650 commit d3925e6

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

community-dashboard/app/Base/configs/apollo.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const GRAPHQL_ENDPOINT = process.env.REACT_APP_GRAPHQL_ENDPOINT as string;
44

55
const link = new HttpLink({
66
uri: GRAPHQL_ENDPOINT,
7-
credentials: 'include',
7+
credentials: 'omit',
88
}) as unknown as ApolloLinkFromClient;
99

1010
/*
@@ -39,8 +39,9 @@ const apolloOptions: ApolloClientOptions<NormalizedCacheObject> = {
3939
errorPolicy: 'all',
4040
},
4141
watchQuery: {
42-
fetchPolicy: 'cache-and-network',
43-
nextFetchPolicy: 'cache-and-network',
42+
// NOTE: setting nextFetchPolicy to cache-and-network is risky
43+
fetchPolicy: 'network-only',
44+
nextFetchPolicy: 'cache-only',
4445
errorPolicy: 'all',
4546
},
4647
},

community-dashboard/app/components/ItemSelectInput/index.tsx

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,33 @@ export type SearchItemType = {
3030
isArchived?: boolean,
3131
};
3232

33+
const LIMIT = 5;
34+
3335
const USERS = gql`
34-
query UserOptions($search: String) {
35-
users(filters: { search: $search }, pagination: { limit: 5, offset: 0 }) {
36+
query UserOptions($search: String, $offset: Int!, $limit: Int!) {
37+
users(filters: { search: $search }, pagination: { limit: $limit, offset: $offset }) {
3638
items {
3739
userId
3840
username
3941
}
4042
count
43+
offset
44+
limit
4145
}
4246
}
4347
`;
4448

4549
const USER_GROUPS = gql`
46-
query UserGroupOptions($search: String) {
47-
userGroups(filters: { search: $search }, pagination: { limit: 5, offset: 0 }) {
50+
query UserGroupOptions($search: String, $offset: Int!, $limit: Int!) {
51+
userGroups(filters: { search: $search }, pagination: { limit: $limit, offset: $offset }) {
4852
items {
4953
isArchived
5054
userGroupId
5155
name
5256
}
5357
count
58+
offset
59+
limit
5460
}
5561
}
5662
`;
@@ -135,17 +141,19 @@ function ItemSelectInput<Name extends string>(props: ItemSelectInputProps<Name>)
135141

136142
const [opened, setOpened] = useState(false);
137143
const [searchText, setSearchText] = useState<string>('');
138-
139144
const debouncedSearchText = useDebouncedValue(searchText);
140145

141146
const variables = useMemo(() => ({
142147
search: debouncedSearchText,
148+
offset: 0,
149+
limit: 5,
143150
}), [debouncedSearchText]);
144151

145152
const {
146153
previousData: previousUserData,
147154
data: userData = previousUserData,
148155
loading: userDataLoading,
156+
fetchMore: fetchMoreUser,
149157
} = useQuery<UserOptionsQuery, UserOptionsQueryVariables>(
150158
USERS,
151159
{
@@ -158,6 +166,7 @@ function ItemSelectInput<Name extends string>(props: ItemSelectInputProps<Name>)
158166
previousData: previousUserGroupData,
159167
data: userGroupData = previousUserGroupData,
160168
loading: userGroupDataLoading,
169+
fetchMore: fetchMoreUserGroup,
161170
} = useQuery<UserGroupOptionsQuery, UserGroupOptionsQueryVariables>(
162171
USER_GROUPS,
163172
{
@@ -168,8 +177,14 @@ function ItemSelectInput<Name extends string>(props: ItemSelectInputProps<Name>)
168177

169178
const loading = userDataLoading || userGroupDataLoading;
170179
const count = (userData?.users.count ?? 0) + (userGroupData?.userGroups.count ?? 0);
171-
const usersData = userData?.users.items;
172-
const userGroupsData = userGroupData?.userGroups.items;
180+
const usersData = useMemo(
181+
() => userData?.users.items,
182+
[userData?.users.items],
183+
);
184+
const userGroupsData = useMemo(
185+
() => userGroupData?.userGroups.items,
186+
[userGroupData?.userGroups.items],
187+
);
173188

174189
const data: SearchItemType[] = useMemo(
175190
() => ([
@@ -198,7 +213,6 @@ function ItemSelectInput<Name extends string>(props: ItemSelectInputProps<Name>)
198213

199214
const optionRendererParams = useCallback(
200215
(_: number | string, option: SearchItemType) => {
201-
// const isActive = key === selectedItem;
202216
const isActive = false;
203217

204218
return {
@@ -209,32 +223,78 @@ function ItemSelectInput<Name extends string>(props: ItemSelectInputProps<Name>)
209223
};
210224
},
211225
[],
212-
// [selectedItem],
213226
);
214227

215-
// TODO: only for test remove later
216228
const handleShowMoreClick = useCallback(
217229
() => {
218-
console.log('puran ban');
219-
}, [],
230+
fetchMoreUser({
231+
variables: {
232+
offset: (userData?.users.offset ?? 0) + LIMIT,
233+
},
234+
updateQuery: (previousResult, { fetchMoreResult }) => {
235+
const oldUsers = previousResult;
236+
const newUsers = fetchMoreResult;
237+
238+
if (!newUsers) {
239+
return previousResult;
240+
}
241+
242+
return ({
243+
users: {
244+
...newUsers.users,
245+
items: [
246+
...oldUsers.users?.items ?? [],
247+
...newUsers.users?.items ?? [],
248+
],
249+
},
250+
});
251+
},
252+
});
253+
fetchMoreUserGroup({
254+
variables: {
255+
offset: (userGroupData?.userGroups.offset ?? 0) + LIMIT,
256+
},
257+
updateQuery: (previousResult, { fetchMoreResult }) => {
258+
const oldUserGroups = previousResult;
259+
const newUserGroups = fetchMoreResult;
260+
261+
if (!newUserGroups) {
262+
return previousResult;
263+
}
264+
265+
return ({
266+
userGroups: {
267+
...newUserGroups.userGroups,
268+
items: [
269+
...oldUserGroups.userGroups.items ?? [],
270+
...newUserGroups.userGroups.items ?? [],
271+
],
272+
},
273+
});
274+
},
275+
});
276+
}, [
277+
variables,
278+
fetchMoreUser,
279+
fetchMoreUserGroup,
280+
userData?.users.offset,
281+
userGroupData?.userGroups.offset,
282+
],
220283
);
221284

222285
return (
223286
<SearchSelectInput
224287
{...otherProps}
288+
className={className}
225289
name="item-select-input"
226290
icons={(
227291
<IoSearch />
228292
)}
229293
optionRendererParams={optionRendererParams}
230294
optionRenderer={Option}
231295
options={[]}
232-
// onOptionsChange={setItemOptions}
233-
// value={selectedItem}
234296
value={undefined}
235297
onChange={handleSelectItem}
236-
// Other props
237-
className={className}
238298
keySelector={keySelector}
239299
labelSelector={titleSelector}
240300
onSearchValueChange={setSearchText}

0 commit comments

Comments
 (0)