@ventlio/tanstack-query 0.2.66 → 0.2.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +14 -9
- package/dist/index.mjs.map +1 -1
- package/dist/model/model.interface.d.ts +2 -2
- package/dist/model/useQueryModel.js +5 -6
- package/dist/model/useQueryModel.js.map +1 -1
- package/dist/queries/useDeleteRequest.d.ts +12 -4
- package/dist/queries/useDeleteRequest.js +1 -1
- package/dist/queries/usePatchRequest.js +9 -3
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/package.json +1 -1
- package/src/model/model.interface.ts +2 -2
- package/src/model/useQueryModel.ts +9 -10
- package/src/queries/useDeleteRequest.ts +2 -2
- package/src/queries/usePatchRequest.ts +11 -3
package/dist/index.mjs
CHANGED
|
@@ -104,7 +104,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
104
104
|
const { getQueryKey } = useKeyTrackerModel(keyTracker);
|
|
105
105
|
const queryKey = getQueryKey();
|
|
106
106
|
const add = (data, position, path) => {
|
|
107
|
-
let records = findAll(path) ?? [];
|
|
107
|
+
let records = (findAll(path) ?? []);
|
|
108
108
|
if (!position || position === 'end') {
|
|
109
109
|
records = [...records, data];
|
|
110
110
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
@@ -132,7 +132,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
132
132
|
return result(data, path, []);
|
|
133
133
|
};
|
|
134
134
|
const findMany = (selector, path) => {
|
|
135
|
-
const data = findAll(path) ?? [];
|
|
135
|
+
const data = (findAll(path) ?? []);
|
|
136
136
|
return data.filter(selector);
|
|
137
137
|
};
|
|
138
138
|
const find = (id, path) => {
|
|
@@ -140,7 +140,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
140
140
|
if (!modelConfig?.idColumn) {
|
|
141
141
|
return undefined;
|
|
142
142
|
}
|
|
143
|
-
const data = findAll(path) ?? [];
|
|
143
|
+
const data = (findAll(path) ?? []);
|
|
144
144
|
return data.find((record) => record[modelConfig.idColumn] === id);
|
|
145
145
|
};
|
|
146
146
|
const get = (path) => {
|
|
@@ -154,7 +154,6 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
154
154
|
if (path) {
|
|
155
155
|
const data = get();
|
|
156
156
|
newData = lodashSet(data, path, newData);
|
|
157
|
-
return queryClient.setQueryData(queryKey, newData);
|
|
158
157
|
}
|
|
159
158
|
return queryClient.setQueryData(queryKey, newData);
|
|
160
159
|
};
|
|
@@ -164,7 +163,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
164
163
|
return modelConfig;
|
|
165
164
|
};
|
|
166
165
|
const update = (id, data, path) => {
|
|
167
|
-
const oldData = findAll(path) ?? [];
|
|
166
|
+
const oldData = (findAll(path) ?? []);
|
|
168
167
|
const modelConfig = getModelConfig();
|
|
169
168
|
if (!modelConfig?.idColumn) {
|
|
170
169
|
return undefined;
|
|
@@ -189,7 +188,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
189
188
|
return updatedRecord;
|
|
190
189
|
};
|
|
191
190
|
const remove = (id, path) => {
|
|
192
|
-
const oldData = findAll(path) ?? [];
|
|
191
|
+
const oldData = (findAll(path) ?? []);
|
|
193
192
|
const modelConfig = getModelConfig();
|
|
194
193
|
if (!modelConfig?.idColumn) {
|
|
195
194
|
return false;
|
|
@@ -424,8 +423,8 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
424
423
|
// set enabled to be true for every delete
|
|
425
424
|
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
426
425
|
internalDeleteOptions.enabled = true;
|
|
427
|
-
await updatedPathAsync(link);
|
|
428
426
|
await setOptionsAsync(internalDeleteOptions);
|
|
427
|
+
await updatedPathAsync(link);
|
|
429
428
|
return query.data;
|
|
430
429
|
};
|
|
431
430
|
return { destroy, ...query };
|
|
@@ -541,6 +540,8 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
541
540
|
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
542
541
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
543
542
|
const { getHeaders } = useQueryHeaders();
|
|
543
|
+
const queryClient = useQueryClient();
|
|
544
|
+
const config = queryClient.getQueryData(['config']);
|
|
544
545
|
const sendRequest = async (res, rej, data) => {
|
|
545
546
|
// get request headers
|
|
546
547
|
const globalHeaders = getHeaders();
|
|
@@ -554,12 +555,16 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
554
555
|
});
|
|
555
556
|
if (patchResponse.status) {
|
|
556
557
|
// scroll to top after success
|
|
557
|
-
|
|
558
|
+
if (config?.options?.context !== 'app') {
|
|
559
|
+
scrollToTop();
|
|
560
|
+
}
|
|
558
561
|
res(patchResponse);
|
|
559
562
|
}
|
|
560
563
|
else {
|
|
561
564
|
// scroll to top after error
|
|
562
|
-
|
|
565
|
+
if (config?.options?.context !== 'app') {
|
|
566
|
+
scrollToTop();
|
|
567
|
+
}
|
|
563
568
|
rej(patchResponse);
|
|
564
569
|
}
|
|
565
570
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export interface QueryModelBuilder<T> {
|
|
2
2
|
add: (data: T, position?: QueryModelAddPosition, path?: string) => T | undefined;
|
|
3
|
-
findAll: (path?: string) => T[] | undefined;
|
|
3
|
+
findAll: (path?: string) => T[] | T | undefined;
|
|
4
4
|
findMany: (selector: (record: T) => boolean, path?: string) => T[];
|
|
5
5
|
find: (id: number | string, path?: string) => T | undefined;
|
|
6
6
|
update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
|
|
7
7
|
remove: (id: number, path?: string) => boolean;
|
|
8
8
|
get: (path?: string) => T | undefined;
|
|
9
|
-
set: (data: Partial<
|
|
9
|
+
set: <DataType>(data: Partial<DataType>, path?: string) => DataType | undefined;
|
|
10
10
|
}
|
|
11
11
|
export type QueryModelAddPosition = 'start' | 'end';
|
|
@@ -8,7 +8,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
8
8
|
const { getQueryKey } = useKeyTrackerModel(keyTracker);
|
|
9
9
|
const queryKey = getQueryKey();
|
|
10
10
|
const add = (data, position, path) => {
|
|
11
|
-
let records = findAll(path) ?? [];
|
|
11
|
+
let records = (findAll(path) ?? []);
|
|
12
12
|
if (!position || position === 'end') {
|
|
13
13
|
records = [...records, data];
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
@@ -36,7 +36,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
36
36
|
return result(data, path, []);
|
|
37
37
|
};
|
|
38
38
|
const findMany = (selector, path) => {
|
|
39
|
-
const data = findAll(path) ?? [];
|
|
39
|
+
const data = (findAll(path) ?? []);
|
|
40
40
|
return data.filter(selector);
|
|
41
41
|
};
|
|
42
42
|
const find = (id, path) => {
|
|
@@ -44,7 +44,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
44
44
|
if (!modelConfig?.idColumn) {
|
|
45
45
|
return undefined;
|
|
46
46
|
}
|
|
47
|
-
const data = findAll(path) ?? [];
|
|
47
|
+
const data = (findAll(path) ?? []);
|
|
48
48
|
return data.find((record) => record[modelConfig.idColumn] === id);
|
|
49
49
|
};
|
|
50
50
|
const get = (path) => {
|
|
@@ -58,7 +58,6 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
58
58
|
if (path) {
|
|
59
59
|
const data = get();
|
|
60
60
|
newData = lodashSet(data, path, newData);
|
|
61
|
-
return queryClient.setQueryData(queryKey, newData);
|
|
62
61
|
}
|
|
63
62
|
return queryClient.setQueryData(queryKey, newData);
|
|
64
63
|
};
|
|
@@ -68,7 +67,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
68
67
|
return modelConfig;
|
|
69
68
|
};
|
|
70
69
|
const update = (id, data, path) => {
|
|
71
|
-
const oldData = findAll(path) ?? [];
|
|
70
|
+
const oldData = (findAll(path) ?? []);
|
|
72
71
|
const modelConfig = getModelConfig();
|
|
73
72
|
if (!modelConfig?.idColumn) {
|
|
74
73
|
return undefined;
|
|
@@ -93,7 +92,7 @@ const useQueryModel = (keyTracker, exact = true) => {
|
|
|
93
92
|
return updatedRecord;
|
|
94
93
|
};
|
|
95
94
|
const remove = (id, path) => {
|
|
96
|
-
const oldData = findAll(path) ?? [];
|
|
95
|
+
const oldData = (findAll(path) ?? []);
|
|
97
96
|
const modelConfig = getModelConfig();
|
|
98
97
|
if (!modelConfig?.idColumn) {
|
|
99
98
|
return false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -27,7 +27,9 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
27
27
|
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
28
28
|
remove: () => void;
|
|
29
29
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
30
|
-
destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]>
|
|
30
|
+
destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
|
|
31
|
+
cached?: boolean | undefined;
|
|
32
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
31
33
|
} | {
|
|
32
34
|
data: IRequestSuccess<TResponse>;
|
|
33
35
|
error: null;
|
|
@@ -54,7 +56,9 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
54
56
|
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
55
57
|
remove: () => void;
|
|
56
58
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
57
|
-
destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]>
|
|
59
|
+
destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
|
|
60
|
+
cached?: boolean | undefined;
|
|
61
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
58
62
|
} | {
|
|
59
63
|
data: undefined;
|
|
60
64
|
error: any;
|
|
@@ -81,7 +85,9 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
81
85
|
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
82
86
|
remove: () => void;
|
|
83
87
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
84
|
-
destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]>
|
|
88
|
+
destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
|
|
89
|
+
cached?: boolean | undefined;
|
|
90
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
85
91
|
} | {
|
|
86
92
|
data: undefined;
|
|
87
93
|
error: null;
|
|
@@ -108,5 +114,7 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
108
114
|
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
109
115
|
remove: () => void;
|
|
110
116
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
111
|
-
destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]>
|
|
117
|
+
destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
|
|
118
|
+
cached?: boolean | undefined;
|
|
119
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
112
120
|
};
|
|
@@ -40,8 +40,8 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
40
40
|
// set enabled to be true for every delete
|
|
41
41
|
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
42
42
|
internalDeleteOptions.enabled = true;
|
|
43
|
-
await updatedPathAsync(link);
|
|
44
43
|
await setOptionsAsync(internalDeleteOptions);
|
|
44
|
+
await updatedPathAsync(link);
|
|
45
45
|
return query.data;
|
|
46
46
|
};
|
|
47
47
|
return { destroy, ...query };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useMutation } from '@tanstack/react-query';
|
|
1
|
+
import { useQueryClient, useMutation } from '@tanstack/react-query';
|
|
2
2
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
3
3
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
4
4
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
@@ -9,6 +9,8 @@ import { HttpMethod } from '../request/request.enum.js';
|
|
|
9
9
|
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
10
10
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
11
11
|
const { getHeaders } = useQueryHeaders();
|
|
12
|
+
const queryClient = useQueryClient();
|
|
13
|
+
const config = queryClient.getQueryData(['config']);
|
|
12
14
|
const sendRequest = async (res, rej, data) => {
|
|
13
15
|
// get request headers
|
|
14
16
|
const globalHeaders = getHeaders();
|
|
@@ -22,12 +24,16 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
22
24
|
});
|
|
23
25
|
if (patchResponse.status) {
|
|
24
26
|
// scroll to top after success
|
|
25
|
-
|
|
27
|
+
if (config?.options?.context !== 'app') {
|
|
28
|
+
scrollToTop();
|
|
29
|
+
}
|
|
26
30
|
res(patchResponse);
|
|
27
31
|
}
|
|
28
32
|
else {
|
|
29
33
|
// scroll to top after error
|
|
30
|
-
|
|
34
|
+
if (config?.options?.context !== 'app') {
|
|
35
|
+
scrollToTop();
|
|
36
|
+
}
|
|
31
37
|
rej(patchResponse);
|
|
32
38
|
}
|
|
33
39
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export interface QueryModelBuilder<T> {
|
|
2
2
|
add: (data: T, position?: QueryModelAddPosition, path?: string) => T | undefined;
|
|
3
|
-
findAll: (path?: string) => T[] | undefined;
|
|
3
|
+
findAll: (path?: string) => T[] | T | undefined;
|
|
4
4
|
findMany: (selector: (record: T) => boolean, path?: string) => T[];
|
|
5
5
|
find: (id: number | string, path?: string) => T | undefined;
|
|
6
6
|
update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
|
|
7
7
|
remove: (id: number, path?: string) => boolean;
|
|
8
8
|
get: (path?: string) => T | undefined;
|
|
9
|
-
set: (data: Partial<
|
|
9
|
+
set: <DataType>(data: Partial<DataType>, path?: string) => DataType | undefined;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export type QueryModelAddPosition = 'start' | 'end';
|
|
@@ -11,7 +11,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
11
11
|
const queryKey = getQueryKey() as any[];
|
|
12
12
|
|
|
13
13
|
const add = (data: T, position?: QueryModelAddPosition, path?: string): T | undefined => {
|
|
14
|
-
let records = findAll(path) ?? [];
|
|
14
|
+
let records = (findAll(path) ?? []) as T[];
|
|
15
15
|
|
|
16
16
|
if (!position || position === 'end') {
|
|
17
17
|
records = [...records, data];
|
|
@@ -30,7 +30,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
30
30
|
return data;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
const findAll = (path?: string): T[] | undefined => {
|
|
33
|
+
const findAll = (path?: string): T[] | T | undefined => {
|
|
34
34
|
const data = queryClient.getQueryData(queryKey, { exact });
|
|
35
35
|
|
|
36
36
|
if (!data) {
|
|
@@ -45,7 +45,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
const findMany = (selector: (record: T) => boolean, path?: string): T[] => {
|
|
48
|
-
const data = findAll(path) ?? [];
|
|
48
|
+
const data = (findAll(path) ?? []) as T[];
|
|
49
49
|
return data.filter(selector);
|
|
50
50
|
};
|
|
51
51
|
|
|
@@ -55,7 +55,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
55
55
|
if (!modelConfig?.idColumn) {
|
|
56
56
|
return undefined;
|
|
57
57
|
}
|
|
58
|
-
const data = findAll(path) ?? [];
|
|
58
|
+
const data = (findAll(path) ?? []) as T[];
|
|
59
59
|
|
|
60
60
|
return data.find((record) => (record as Record<string, any>)[modelConfig.idColumn] === id);
|
|
61
61
|
};
|
|
@@ -68,14 +68,13 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
68
68
|
return data as T;
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
-
const set = (newData:
|
|
71
|
+
const set = <DataType>(newData: Partial<DataType>, path?: string): DataType | undefined => {
|
|
72
72
|
if (path) {
|
|
73
73
|
const data = get() as any;
|
|
74
74
|
newData = lodashSet(data, path, newData);
|
|
75
|
-
|
|
76
|
-
return queryClient.setQueryData(queryKey, newData) as T;
|
|
77
75
|
}
|
|
78
|
-
|
|
76
|
+
|
|
77
|
+
return queryClient.setQueryData(queryKey, newData) as DataType;
|
|
79
78
|
};
|
|
80
79
|
|
|
81
80
|
const getModelConfig = () => {
|
|
@@ -86,7 +85,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
86
85
|
};
|
|
87
86
|
|
|
88
87
|
const update = (id: string | number, data: Partial<T>, path?: string): T | undefined => {
|
|
89
|
-
const oldData = findAll(path) ?? [];
|
|
88
|
+
const oldData = (findAll(path) ?? []) as T[];
|
|
90
89
|
const modelConfig = getModelConfig();
|
|
91
90
|
|
|
92
91
|
if (!modelConfig?.idColumn) {
|
|
@@ -115,7 +114,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
115
114
|
};
|
|
116
115
|
|
|
117
116
|
const remove = (id: number | string, path?: string): boolean => {
|
|
118
|
-
const oldData = findAll(path) ?? [];
|
|
117
|
+
const oldData = (findAll(path) ?? []) as T[];
|
|
119
118
|
const modelConfig = getModelConfig();
|
|
120
119
|
|
|
121
120
|
if (!modelConfig?.idColumn) {
|
|
@@ -56,14 +56,14 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
56
56
|
IRequestError,
|
|
57
57
|
IRequestSuccess<TResponse | undefined>,
|
|
58
58
|
Array<any>
|
|
59
|
-
>
|
|
59
|
+
> & { cached?: boolean }
|
|
60
60
|
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
61
61
|
// set enabled to be true for every delete
|
|
62
62
|
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
63
63
|
internalDeleteOptions.enabled = true;
|
|
64
64
|
|
|
65
|
-
await updatedPathAsync(link);
|
|
66
65
|
await setOptionsAsync(internalDeleteOptions);
|
|
66
|
+
await updatedPathAsync(link);
|
|
67
67
|
|
|
68
68
|
return query.data;
|
|
69
69
|
};
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
|
-
import { useMutation } from '@tanstack/react-query';
|
|
2
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
4
4
|
import { useEnvironmentVariables, useQueryHeaders } from '../config';
|
|
5
5
|
import { scrollToTop } from '../helpers';
|
|
6
6
|
import { HttpMethod, makeRequest } from '../request';
|
|
7
7
|
import type { IRequestError, IRequestSuccess } from '../request/request.interface';
|
|
8
|
+
import type { TanstackQueryConfig } from '../types';
|
|
8
9
|
import type { DefaultRequestOptions } from './queries.interface';
|
|
9
10
|
|
|
10
11
|
export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
|
|
11
12
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
12
13
|
|
|
13
14
|
const { getHeaders } = useQueryHeaders();
|
|
15
|
+
const queryClient = useQueryClient();
|
|
16
|
+
|
|
17
|
+
const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
|
|
14
18
|
|
|
15
19
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
|
|
16
20
|
// get request headers
|
|
@@ -27,11 +31,15 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
27
31
|
|
|
28
32
|
if (patchResponse.status) {
|
|
29
33
|
// scroll to top after success
|
|
30
|
-
|
|
34
|
+
if (config?.options?.context !== 'app') {
|
|
35
|
+
scrollToTop();
|
|
36
|
+
}
|
|
31
37
|
res(patchResponse as IRequestSuccess<TResponse>);
|
|
32
38
|
} else {
|
|
33
39
|
// scroll to top after error
|
|
34
|
-
|
|
40
|
+
if (config?.options?.context !== 'app') {
|
|
41
|
+
scrollToTop();
|
|
42
|
+
}
|
|
35
43
|
rej(patchResponse);
|
|
36
44
|
}
|
|
37
45
|
};
|