Skip to content

Commit 1eccb7a

Browse files
Issue #40 Port over useQuery custom hook
implement useQuery custom react hook
2 parents c62e035 + b475a6d commit 1eccb7a

5 files changed

Lines changed: 128 additions & 6 deletions

File tree

package-lock.json

Lines changed: 19 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"@types/react": "16.9.26",
1212
"@types/react-dom": "16.9.6",
1313
"@types/react-router-dom": "5.1.5",
14-
"andculturecode-javascript-core": "0.1.2",
14+
"andculturecode-javascript-core": "0.3.3",
1515
"axios": "0.19.2",
1616
"i18next": "19.4.5",
1717
"immutable": "4.0.0-rc.12",

src/hooks/use-query.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
describe("useQuery", () => {
2+
test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React/issues/41", () => {});
3+
});

src/hooks/use-query.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {
2+
Do,
3+
EnvironmentUtils,
4+
ResultRecord,
5+
} from "andculturecode-javascript-core";
6+
import { useCallback, useEffect, useState } from "react";
7+
import { UseQueryOptions } from "../interfaces/use-query-options";
8+
import { ListService } from "../types/list-service-type";
9+
import { NestedListService } from "../types/nested-list-service-type";
10+
11+
export function useQuery<TRecord, TQueryParams, TPathParams = undefined>(
12+
options: UseQueryOptions<TRecord, TQueryParams, TPathParams>
13+
) {
14+
const {
15+
initialPathParams,
16+
initialQuery,
17+
onError,
18+
onSuccess,
19+
serviceHook,
20+
} = options;
21+
22+
const { list: listApi } = serviceHook();
23+
24+
const handleSuccess = useCallback(
25+
(records: Array<TRecord>) => {
26+
if (onSuccess != null) {
27+
onSuccess(records);
28+
return;
29+
}
30+
},
31+
[onSuccess]
32+
);
33+
34+
const handleError = useCallback(
35+
(result?: ResultRecord<TRecord>, error?: any) => {
36+
if (onError != null) {
37+
onError(result, error);
38+
return;
39+
}
40+
41+
EnvironmentUtils.runIfDevelopment(() => {
42+
console.error("No error handler defined.");
43+
console.error(result, error);
44+
});
45+
},
46+
[onError]
47+
);
48+
49+
const [loading, setLoading] = useState(false);
50+
const [query, setQuery] = useState(initialQuery);
51+
const [pathParams, setPathParams] = useState(initialPathParams);
52+
const [values, setValues] = useState<Array<TRecord>>([]);
53+
54+
useEffect(() => {
55+
Do.try(async () => {
56+
// if regular list, not nested list
57+
if (pathParams == null) {
58+
const api = listApi as ListService<TRecord, TQueryParams>;
59+
const result = await api(query);
60+
setValues(result.resultObjects!);
61+
handleSuccess(result.resultObjects!);
62+
return;
63+
}
64+
65+
// otherwise, nested list
66+
const api = listApi as NestedListService<
67+
TRecord,
68+
TPathParams,
69+
TQueryParams
70+
>;
71+
const result = await api(pathParams, query);
72+
setValues(result.resultObjects!);
73+
handleSuccess(result.resultObjects!);
74+
})
75+
.catch(handleError)
76+
.finally(() => setLoading(false));
77+
});
78+
79+
return {
80+
loading,
81+
query,
82+
setQuery,
83+
pathParams,
84+
setPathParams,
85+
values,
86+
setValues,
87+
};
88+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CatchResultHandler } from "andculturecode-javascript-core";
2+
import { ListServiceHook } from "../types/list-service-hook-type";
3+
import { NestedListServiceHook } from "../types/nested-list-service-hook-type";
4+
5+
export interface UseQueryOptions<
6+
TRecord,
7+
TQueryParams,
8+
TPathParams = undefined
9+
> {
10+
serviceHook:
11+
| ListServiceHook<TRecord, TQueryParams>
12+
| NestedListServiceHook<TRecord, TPathParams, TQueryParams>;
13+
initialQuery: TQueryParams;
14+
initialPathParams?: TPathParams;
15+
onSuccess?: (records: Array<TRecord>) => void;
16+
onError?: CatchResultHandler<TRecord>;
17+
}

0 commit comments

Comments
 (0)