@star-insure/sdk 4.3.0 → 4.3.2
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/lib/fetch.d.ts +20 -0
- package/dist/sdk.cjs.development.js +20 -1
- package/dist/sdk.cjs.development.js.map +1 -1
- package/dist/sdk.cjs.production.min.js +1 -1
- package/dist/sdk.cjs.production.min.js.map +1 -1
- package/dist/sdk.esm.js +20 -1
- package/dist/sdk.esm.js.map +1 -1
- package/dist/types/misc/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/components/filter/FilterItem.tsx +22 -0
- package/src/lib/fetch.tsx +81 -0
- package/src/types/misc/index.ts +1 -1
|
@@ -9,7 +9,7 @@ export interface FilterOption {
|
|
|
9
9
|
label: string;
|
|
10
10
|
name: string;
|
|
11
11
|
options?: FilterValue[];
|
|
12
|
-
type?: 'options' | 'date' | 'greaterThan' | 'scope' | 'select' | 'datalist' | 'column';
|
|
12
|
+
type?: 'options' | 'date' | 'greaterThan' | 'scope' | 'select' | 'datalist' | 'column' | 'text';
|
|
13
13
|
}
|
|
14
14
|
export interface FilterValue {
|
|
15
15
|
label: string;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@star-insure/sdk",
|
|
3
3
|
"description": "The SDK for Star Insure client apps with shared helper functions and TypeScript definitions.",
|
|
4
4
|
"author": "alexclark_nz",
|
|
5
|
-
"version": "4.3.
|
|
5
|
+
"version": "4.3.2",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
@@ -114,6 +114,16 @@ export function FilterItem({ filter }: { filter: FilterOption, path?: string })
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
function handleText(e: React.SyntheticEvent<HTMLInputElement>) {
|
|
118
|
+
const { value } = e.currentTarget;
|
|
119
|
+
|
|
120
|
+
if (value) {
|
|
121
|
+
setSelected([value]);
|
|
122
|
+
} else {
|
|
123
|
+
setSelected([]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
117
127
|
function handleApply(e: React.FormEvent) {
|
|
118
128
|
e.preventDefault();
|
|
119
129
|
|
|
@@ -304,6 +314,18 @@ export function FilterItem({ filter }: { filter: FilterOption, path?: string })
|
|
|
304
314
|
/>
|
|
305
315
|
</div>
|
|
306
316
|
)}
|
|
317
|
+
{filter.type === 'text' && (
|
|
318
|
+
<div className={'w-full'}>
|
|
319
|
+
<input
|
|
320
|
+
type="text"
|
|
321
|
+
name={filter.name}
|
|
322
|
+
placeholder={filter.label}
|
|
323
|
+
value={selected[0] ?? ''}
|
|
324
|
+
onChange={handleText}
|
|
325
|
+
className="!p-2 text-sm w-full"
|
|
326
|
+
/>
|
|
327
|
+
</div>
|
|
328
|
+
)}
|
|
307
329
|
</div>
|
|
308
330
|
<div className="bg-gray-100 border-t border-gray-200 flex items-center gap-2 p-4 bottom-0 sticky">
|
|
309
331
|
<Button type="button" className="!min-w-[0px] flex-grow !px-2 text-sm !transition-none" onClick={handleClear} small>
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { usePage } from './page';
|
|
3
|
+
|
|
4
|
+
type FetchOptions = {
|
|
5
|
+
url: string;
|
|
6
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
7
|
+
params?: Record<string, any>;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type FetcherFunction<T> = () => Promise<
|
|
12
|
+
| {
|
|
13
|
+
ok: false;
|
|
14
|
+
message: string | undefined;
|
|
15
|
+
data?: undefined;
|
|
16
|
+
}
|
|
17
|
+
| {
|
|
18
|
+
ok: true;
|
|
19
|
+
data?: T;
|
|
20
|
+
message: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A helper to return a fetch function including default headers and authorization.
|
|
26
|
+
*/
|
|
27
|
+
export function useFetch<T = any>({
|
|
28
|
+
url: baseUrl,
|
|
29
|
+
method = 'GET',
|
|
30
|
+
params = {},
|
|
31
|
+
headers = {},
|
|
32
|
+
}: FetchOptions): FetcherFunction<T> {
|
|
33
|
+
const { access_token } = usePage().props;
|
|
34
|
+
|
|
35
|
+
let url = baseUrl;
|
|
36
|
+
|
|
37
|
+
if (method === 'GET') {
|
|
38
|
+
const queryParams = new URLSearchParams(params);
|
|
39
|
+
url = `${url}?${queryParams.toString()}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return React.useMemo(
|
|
43
|
+
() => async () => {
|
|
44
|
+
const res = await fetch(url, {
|
|
45
|
+
method,
|
|
46
|
+
headers: {
|
|
47
|
+
'Content-Type': 'application/json',
|
|
48
|
+
Accept: 'application/json',
|
|
49
|
+
Authorization: access_token ? `Bearer ${access_token}` : '',
|
|
50
|
+
...headers,
|
|
51
|
+
},
|
|
52
|
+
body: Object.keys(params).length > 0 && method !== 'GET' ? JSON.stringify(params) : null,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const { ok } = res;
|
|
56
|
+
const body = await res.text();
|
|
57
|
+
|
|
58
|
+
let parsedBody: T | string = body;
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
parsedBody = JSON.parse(body) as T;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
// Response wasn't JSON, just use the text version.
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const message = typeof parsedBody === 'string' ? parsedBody : undefined;
|
|
67
|
+
|
|
68
|
+
if (!ok) {
|
|
69
|
+
return {
|
|
70
|
+
ok,
|
|
71
|
+
message,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const data = typeof parsedBody !== 'string' ? parsedBody : undefined;
|
|
76
|
+
|
|
77
|
+
return { ok, data, message };
|
|
78
|
+
},
|
|
79
|
+
[access_token, url, params, headers, method],
|
|
80
|
+
);
|
|
81
|
+
}
|
package/src/types/misc/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface FilterOption {
|
|
|
11
11
|
label: string;
|
|
12
12
|
name: string;
|
|
13
13
|
options?: FilterValue[];
|
|
14
|
-
type?: 'options' | 'date' | 'greaterThan' | 'scope' | 'select' | 'datalist' | 'column';
|
|
14
|
+
type?: 'options' | 'date' | 'greaterThan' | 'scope' | 'select' | 'datalist' | 'column' | 'text';
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export interface FilterValue {
|