@qoretechnologies/reqraft 0.2.2 → 0.2.4
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/contexts/ReqraftContext.d.ts +0 -4
- package/dist/contexts/ReqraftContext.d.ts.map +1 -1
- package/dist/contexts/ReqraftContext.js +0 -3
- package/dist/contexts/ReqraftContext.js.map +1 -1
- package/dist/hooks/useReqraftProperty.d.ts +1 -1
- package/dist/hooks/useReqraftProperty.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/FetchProvider.d.ts.map +1 -1
- package/dist/providers/FetchProvider.js +0 -18
- package/dist/providers/FetchProvider.js.map +1 -1
- package/dist/providers/ReqraftProvider.d.ts +9 -1
- package/dist/providers/ReqraftProvider.d.ts.map +1 -1
- package/dist/providers/ReqraftProvider.js +13 -3
- package/dist/providers/ReqraftProvider.js.map +1 -1
- package/dist/providers/StorageProvider.d.ts +3 -2
- package/dist/providers/StorageProvider.d.ts.map +1 -1
- package/dist/providers/StorageProvider.js +2 -2
- package/dist/providers/StorageProvider.js.map +1 -1
- package/dist/utils/fetch.d.ts +1 -0
- package/dist/utils/fetch.d.ts.map +1 -1
- package/dist/utils/fetch.js +9 -9
- package/dist/utils/fetch.js.map +1 -1
- package/package.json +1 -1
- package/src/components/form/fields/Field.tsx +0 -37
- package/src/components/form/fields/string/String.stories.tsx +0 -23
- package/src/components/form/fields/string/String.tsx +0 -68
- package/src/components/menu/Menu.stories.tsx +0 -73
- package/src/components/menu/Menu.tsx +0 -244
- package/src/contexts/FetchContext.tsx +0 -25
- package/src/contexts/ReqraftContext.tsx +0 -16
- package/src/contexts/StorageContext.tsx +0 -33
- package/src/hooks/useFetch/useFetch.stories.tsx +0 -123
- package/src/hooks/useFetch/useFetch.tsx +0 -71
- package/src/hooks/useReqraftProperty.ts +0 -16
- package/src/hooks/useStorage/useStorage.stories.tsx +0 -84
- package/src/hooks/useStorage/useStorage.ts +0 -30
- package/src/hooks/useValidation.ts +0 -9
- package/src/index.tsx +0 -12
- package/src/providers/FetchProvider.tsx +0 -62
- package/src/providers/ReqraftProvider.tsx +0 -33
- package/src/providers/StorageProvider.tsx +0 -80
- package/src/types/Form.ts +0 -57
- package/src/types.ts +0 -12
- package/src/utils/fetch.ts +0 -121
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ReqoreButton,
|
|
3
|
-
ReqoreControlGroup,
|
|
4
|
-
ReqoreInput,
|
|
5
|
-
ReqoreMenuDivider,
|
|
6
|
-
ReqoreMenuItem,
|
|
7
|
-
ReqoreMenuSection,
|
|
8
|
-
} from '@qoretechnologies/reqore';
|
|
9
|
-
import ReqoreMenu, { IReqoreMenuProps } from '@qoretechnologies/reqore/dist/components/Menu';
|
|
10
|
-
import { IReqoreMenuDividerProps } from '@qoretechnologies/reqore/dist/components/Menu/divider';
|
|
11
|
-
import { IReqoreMenuItemProps } from '@qoretechnologies/reqore/dist/components/Menu/item';
|
|
12
|
-
import { map, reduce, size } from 'lodash';
|
|
13
|
-
import { useEffect, useMemo, useState } from 'react';
|
|
14
|
-
import { useReqraftStorage } from '../../hooks/useStorage/useStorage';
|
|
15
|
-
|
|
16
|
-
export interface IReqraftMenuItem extends IReqoreMenuItemProps {
|
|
17
|
-
submenu?: TReqraftMenuItem[];
|
|
18
|
-
activePaths?: string[];
|
|
19
|
-
to?: string;
|
|
20
|
-
href?: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type TReqraftMenuItem = IReqraftMenuItem | ({ divider: true } & IReqoreMenuDividerProps);
|
|
24
|
-
export type TReqraftMenu = TReqraftMenuItem[];
|
|
25
|
-
|
|
26
|
-
export interface IReqraftMenuProps extends Partial<Omit<IReqoreMenuProps, 'resizable'>> {
|
|
27
|
-
hidden?: boolean;
|
|
28
|
-
onHideClick?: () => void;
|
|
29
|
-
|
|
30
|
-
defaultQuery?: string;
|
|
31
|
-
onQueryChange?: (query: string) => void;
|
|
32
|
-
|
|
33
|
-
menu: TReqraftMenu;
|
|
34
|
-
inputFocusShortcut?: string;
|
|
35
|
-
path?: string;
|
|
36
|
-
|
|
37
|
-
resizable?: boolean;
|
|
38
|
-
onResizeChange?: (width: number) => void;
|
|
39
|
-
defaultWidth?: number;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const ReqraftMenuItem = ({
|
|
43
|
-
path,
|
|
44
|
-
isCollapsed,
|
|
45
|
-
...props
|
|
46
|
-
}: TReqraftMenuItem & { path?: string; isCollapsed?: boolean }) => {
|
|
47
|
-
if ('divider' in props) {
|
|
48
|
-
return <ReqoreMenuDivider />;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const isActive = useMemo(
|
|
52
|
-
() =>
|
|
53
|
-
props.activePaths?.some(
|
|
54
|
-
(activePath) => activePath === path || path?.startsWith(`${activePath}/`)
|
|
55
|
-
),
|
|
56
|
-
[path]
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
if (props.submenu) {
|
|
60
|
-
const { submenu, ...menuData } = props;
|
|
61
|
-
|
|
62
|
-
return (
|
|
63
|
-
<ReqoreMenuSection
|
|
64
|
-
label={menuData.label}
|
|
65
|
-
icon={menuData.icon}
|
|
66
|
-
isCollapsed={isCollapsed && !isActive}
|
|
67
|
-
verticalPadding='tiny'
|
|
68
|
-
{...menuData}
|
|
69
|
-
>
|
|
70
|
-
{map(submenu, (submenuData, submenuId) => (
|
|
71
|
-
<ReqraftMenuItem key={submenuId} {...submenuData} path={path} />
|
|
72
|
-
))}
|
|
73
|
-
</ReqoreMenuSection>
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return (
|
|
78
|
-
<ReqoreMenuItem
|
|
79
|
-
customTheme={{ main: '#050505' }}
|
|
80
|
-
effect={
|
|
81
|
-
isActive
|
|
82
|
-
? {
|
|
83
|
-
gradient: {
|
|
84
|
-
colors: {
|
|
85
|
-
0: 'info:darken:5:0.4',
|
|
86
|
-
40: '#181818',
|
|
87
|
-
100: '#181818',
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
}
|
|
91
|
-
: undefined
|
|
92
|
-
}
|
|
93
|
-
leftIconColor={isActive ? 'info:lighten:10' : undefined}
|
|
94
|
-
verticalPadding='tiny'
|
|
95
|
-
{...props}
|
|
96
|
-
/>
|
|
97
|
-
);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export const ReqraftMenu = ({
|
|
101
|
-
defaultQuery,
|
|
102
|
-
defaultWidth = 250,
|
|
103
|
-
inputFocusShortcut = '/',
|
|
104
|
-
hidden,
|
|
105
|
-
menu,
|
|
106
|
-
onQueryChange,
|
|
107
|
-
onResizeChange,
|
|
108
|
-
onHideClick,
|
|
109
|
-
resizable,
|
|
110
|
-
path,
|
|
111
|
-
...rest
|
|
112
|
-
}: IReqraftMenuProps) => {
|
|
113
|
-
const [query, setQuery] = useState<string>(defaultQuery);
|
|
114
|
-
|
|
115
|
-
const [isSidebarOpen, update] = useReqraftStorage<boolean>('sidebar-open', true, false);
|
|
116
|
-
const [sidebarSize, updateSidebarSize] = useReqraftStorage<number>(
|
|
117
|
-
'sidebar-size',
|
|
118
|
-
defaultWidth,
|
|
119
|
-
false
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
useEffect(() => {
|
|
123
|
-
if (defaultQuery) {
|
|
124
|
-
setQuery(defaultQuery);
|
|
125
|
-
}
|
|
126
|
-
}, [defaultQuery]);
|
|
127
|
-
|
|
128
|
-
const handleQueryChange = (newQuery: string) => {
|
|
129
|
-
setQuery(newQuery);
|
|
130
|
-
onQueryChange?.(newQuery);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const handleWidthChange = (newWidth: number) => {
|
|
134
|
-
updateSidebarSize(newWidth);
|
|
135
|
-
onResizeChange?.(newWidth);
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const filteredMenu: TReqraftMenu = useMemo<TReqraftMenu>(() => {
|
|
139
|
-
if (!query) {
|
|
140
|
-
return menu;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const filterItems = (items: TReqraftMenu): TReqraftMenu => {
|
|
144
|
-
return reduce(
|
|
145
|
-
items,
|
|
146
|
-
(acc, item) => {
|
|
147
|
-
if ('divider' in item) {
|
|
148
|
-
acc.push(item);
|
|
149
|
-
return acc;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (item.submenu) {
|
|
153
|
-
const submenu = filterItems(item.submenu);
|
|
154
|
-
const hasChildMatch = size(submenu);
|
|
155
|
-
|
|
156
|
-
if (hasChildMatch) {
|
|
157
|
-
acc.push({
|
|
158
|
-
...item,
|
|
159
|
-
submenu,
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
return acc;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (item.label.toString().toLowerCase().includes(query.toLowerCase())) {
|
|
167
|
-
acc.push(item);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return acc;
|
|
171
|
-
},
|
|
172
|
-
[]
|
|
173
|
-
);
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
return filterItems(menu);
|
|
177
|
-
}, [menu, query]);
|
|
178
|
-
|
|
179
|
-
if (hidden) {
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return (
|
|
184
|
-
<ReqoreMenu
|
|
185
|
-
width='250px'
|
|
186
|
-
minimal
|
|
187
|
-
position='left'
|
|
188
|
-
resizable={{
|
|
189
|
-
enable: { right: resizable, left: false },
|
|
190
|
-
minWidth: '250px',
|
|
191
|
-
maxWidth: '350px',
|
|
192
|
-
onResizeStop: (_e, _direction, _ref, d) => {
|
|
193
|
-
handleWidthChange(sidebarSize + d.width);
|
|
194
|
-
},
|
|
195
|
-
size: {
|
|
196
|
-
width: `${sidebarSize}px`,
|
|
197
|
-
height: '100%',
|
|
198
|
-
},
|
|
199
|
-
}}
|
|
200
|
-
rounded={false}
|
|
201
|
-
customTheme={{ main: '#181818' }}
|
|
202
|
-
{...rest}
|
|
203
|
-
>
|
|
204
|
-
<ReqoreControlGroup>
|
|
205
|
-
<ReqoreInput
|
|
206
|
-
icon='Search2Line'
|
|
207
|
-
minimal={false}
|
|
208
|
-
flat={false}
|
|
209
|
-
placeholder={`Filter menu "${inputFocusShortcut}"`}
|
|
210
|
-
intent={query ? 'info' : 'muted'}
|
|
211
|
-
leftIconProps={{ size: 'small' }}
|
|
212
|
-
iconColor={query ? 'info' : 'muted'}
|
|
213
|
-
pill
|
|
214
|
-
value={query}
|
|
215
|
-
onClearClick={() => handleQueryChange('')}
|
|
216
|
-
onChange={(e: any) => handleQueryChange(e.target.value)}
|
|
217
|
-
focusRules={{
|
|
218
|
-
shortcut: inputFocusShortcut,
|
|
219
|
-
type: 'keypress',
|
|
220
|
-
clearOnFocus: true,
|
|
221
|
-
doNotInsertShortcut: true,
|
|
222
|
-
}}
|
|
223
|
-
/>
|
|
224
|
-
<ReqoreButton
|
|
225
|
-
icon='SideBarLine'
|
|
226
|
-
fixed
|
|
227
|
-
minimal={false}
|
|
228
|
-
onClick={() => {
|
|
229
|
-
update(!isSidebarOpen);
|
|
230
|
-
onHideClick?.();
|
|
231
|
-
}}
|
|
232
|
-
/>
|
|
233
|
-
</ReqoreControlGroup>
|
|
234
|
-
{map(filteredMenu, (menuData, menuId) => (
|
|
235
|
-
<ReqraftMenuItem
|
|
236
|
-
key={menuId}
|
|
237
|
-
{...menuData}
|
|
238
|
-
path={path}
|
|
239
|
-
isCollapsed={!query && !!(menuData as IReqraftMenuItem).submenu}
|
|
240
|
-
/>
|
|
241
|
-
))}
|
|
242
|
-
</ReqoreMenu>
|
|
243
|
-
);
|
|
244
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'use-context-selector';
|
|
2
|
-
import { IReqraftFetchResponse, IReqraftQueryConfig } from '../utils/fetch';
|
|
3
|
-
|
|
4
|
-
export type TReqraftContextQueryConfig = Omit<IReqraftQueryConfig, 'method'>;
|
|
5
|
-
export interface IReqraftFetchContext {
|
|
6
|
-
get: <T>(config?: TReqraftContextQueryConfig) => Promise<IReqraftFetchResponse<T>>;
|
|
7
|
-
post: <T>(config?: TReqraftContextQueryConfig) => Promise<IReqraftFetchResponse<T>>;
|
|
8
|
-
put: <T>(config?: TReqraftContextQueryConfig) => Promise<IReqraftFetchResponse<T>>;
|
|
9
|
-
del: <T>(config?: TReqraftContextQueryConfig) => Promise<IReqraftFetchResponse<T>>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const FetchContext = createContext<IReqraftFetchContext>({
|
|
13
|
-
get: async () => {
|
|
14
|
-
throw new Error('FetchContext not implemented');
|
|
15
|
-
},
|
|
16
|
-
post: async () => {
|
|
17
|
-
throw new Error('FetchContext not implemented');
|
|
18
|
-
},
|
|
19
|
-
put: async () => {
|
|
20
|
-
throw new Error('FetchContext not implemented');
|
|
21
|
-
},
|
|
22
|
-
del: async () => {
|
|
23
|
-
throw new Error('FetchContext not implemented');
|
|
24
|
-
},
|
|
25
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'use-context-selector';
|
|
2
|
-
import { IReqraftFetchConfig } from '../utils/fetch';
|
|
3
|
-
|
|
4
|
-
export interface IReqraftContext {
|
|
5
|
-
appName: string;
|
|
6
|
-
instance?: string;
|
|
7
|
-
instanceToken: string;
|
|
8
|
-
instanceUnauthorizedRedirect?: IReqraftFetchConfig['unauthorizedRedirect'];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const ReqraftContext = createContext<IReqraftContext>({
|
|
12
|
-
appName: '',
|
|
13
|
-
instance: '',
|
|
14
|
-
instanceToken: '',
|
|
15
|
-
instanceUnauthorizedRedirect: undefined,
|
|
16
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Get } from 'type-fest';
|
|
2
|
-
import { createContext } from 'use-context-selector';
|
|
3
|
-
import { TReqraftStorageValue } from '../hooks/useStorage/useStorage';
|
|
4
|
-
|
|
5
|
-
export type TReqraftStorage = Record<string, any>;
|
|
6
|
-
|
|
7
|
-
export interface IReqraftStorageContext {
|
|
8
|
-
storage?: Record<string, any>;
|
|
9
|
-
getStorage?: <T extends TReqraftStorageValue>(
|
|
10
|
-
path: string,
|
|
11
|
-
defaultValue?: T,
|
|
12
|
-
includeAppPrefix?: boolean
|
|
13
|
-
) => Get<TReqraftStorage, string>;
|
|
14
|
-
updateStorage?: <T extends TReqraftStorageValue>(
|
|
15
|
-
path: string,
|
|
16
|
-
value: T,
|
|
17
|
-
includeAppPrefix?: boolean
|
|
18
|
-
) => void;
|
|
19
|
-
removeStorageValue: (path: string, includeAppPrefix?: boolean) => void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const ReqraftStorageContext = createContext<IReqraftStorageContext>({
|
|
23
|
-
storage: {},
|
|
24
|
-
getStorage: () => {
|
|
25
|
-
throw new Error('Storage not implemented');
|
|
26
|
-
},
|
|
27
|
-
updateStorage: () => {
|
|
28
|
-
throw new Error('Storage not implemented');
|
|
29
|
-
},
|
|
30
|
-
removeStorageValue: () => {
|
|
31
|
-
throw new Error('Storage not implemented');
|
|
32
|
-
},
|
|
33
|
-
});
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { ReqoreSpinner, ReqoreTree } from '@qoretechnologies/reqore';
|
|
2
|
-
import { StoryObj } from '@storybook/react';
|
|
3
|
-
import { useEffectOnce } from 'react-use';
|
|
4
|
-
import { testsWaitForText } from '../../../__tests__/utils';
|
|
5
|
-
import { StoryMeta } from '../../types';
|
|
6
|
-
import { useFetch } from './useFetch';
|
|
7
|
-
|
|
8
|
-
const meta = {
|
|
9
|
-
title: 'Hooks/useFetch',
|
|
10
|
-
render: (args) => {
|
|
11
|
-
const { data = {}, load, loading } = useFetch<any>({ url: 'public/info', method: args.method });
|
|
12
|
-
|
|
13
|
-
useEffectOnce(() => {
|
|
14
|
-
load();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
return loading ? (
|
|
18
|
-
<ReqoreSpinner />
|
|
19
|
-
) : (
|
|
20
|
-
<ReqoreTree data={data} bottomActions={[{ label: 'Refetch', onClick: load }]} />
|
|
21
|
-
);
|
|
22
|
-
},
|
|
23
|
-
} as StoryMeta<any>;
|
|
24
|
-
|
|
25
|
-
export default meta;
|
|
26
|
-
export type Story = StoryObj<typeof meta>;
|
|
27
|
-
|
|
28
|
-
export const get: Story = {
|
|
29
|
-
args: {
|
|
30
|
-
method: 'GET',
|
|
31
|
-
},
|
|
32
|
-
parameters: {
|
|
33
|
-
mockData: [
|
|
34
|
-
{
|
|
35
|
-
// An array of mock objects which will add in every story
|
|
36
|
-
url: 'https://hq.qoretechnologies.com:8092/api/latest/public/info',
|
|
37
|
-
method: 'GET',
|
|
38
|
-
status: 200,
|
|
39
|
-
delay: 200,
|
|
40
|
-
response: {
|
|
41
|
-
'instance-key': 'rippy-dev1',
|
|
42
|
-
'omq-version': '6.1.0_prod',
|
|
43
|
-
'omq-build': 'd115c45fbed95c03327aea880be455137e9778ba',
|
|
44
|
-
'qore-version': '2.0.0',
|
|
45
|
-
'omq-schema': 'omq@omq',
|
|
46
|
-
edition: 'Enterprise',
|
|
47
|
-
tz_region: 'Europe/Prague',
|
|
48
|
-
tz_utc_offset: 3600,
|
|
49
|
-
noauth: false,
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
},
|
|
54
|
-
play: async () => {
|
|
55
|
-
await testsWaitForText('"rippy-dev1"');
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const put: Story = {
|
|
60
|
-
args: {
|
|
61
|
-
method: 'PUT',
|
|
62
|
-
},
|
|
63
|
-
parameters: {
|
|
64
|
-
mockData: [
|
|
65
|
-
{
|
|
66
|
-
// An array of mock objects which will add in every story
|
|
67
|
-
url: 'https://hq.qoretechnologies.com:8092/api/latest/public/info',
|
|
68
|
-
method: 'PUT',
|
|
69
|
-
status: 200,
|
|
70
|
-
response: {
|
|
71
|
-
status: 'Successfuly updated',
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
],
|
|
75
|
-
},
|
|
76
|
-
play: async () => {
|
|
77
|
-
await testsWaitForText('"Successfuly updated"');
|
|
78
|
-
},
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export const post: Story = {
|
|
82
|
-
args: {
|
|
83
|
-
method: 'POST',
|
|
84
|
-
},
|
|
85
|
-
parameters: {
|
|
86
|
-
mockData: [
|
|
87
|
-
{
|
|
88
|
-
// An array of mock objects which will add in every story
|
|
89
|
-
url: 'https://hq.qoretechnologies.com:8092/api/latest/public/info',
|
|
90
|
-
method: 'POST',
|
|
91
|
-
status: 200,
|
|
92
|
-
response: {
|
|
93
|
-
status: 'Successfuly created',
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
],
|
|
97
|
-
},
|
|
98
|
-
play: async () => {
|
|
99
|
-
await testsWaitForText('"Successfuly created"');
|
|
100
|
-
},
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
export const del: Story = {
|
|
104
|
-
args: {
|
|
105
|
-
method: 'DELETE',
|
|
106
|
-
},
|
|
107
|
-
parameters: {
|
|
108
|
-
mockData: [
|
|
109
|
-
{
|
|
110
|
-
// An array of mock objects which will add in every story
|
|
111
|
-
url: 'https://hq.qoretechnologies.com:8092/api/latest/public/info',
|
|
112
|
-
method: 'DELETE',
|
|
113
|
-
status: 200,
|
|
114
|
-
response: {
|
|
115
|
-
status: 'Successfuly deleted',
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
],
|
|
119
|
-
},
|
|
120
|
-
play: async () => {
|
|
121
|
-
await testsWaitForText('"Successfuly deleted"');
|
|
122
|
-
},
|
|
123
|
-
};
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
2
|
-
import { useEffectOnce } from 'react-use';
|
|
3
|
-
import { useContextSelector } from 'use-context-selector';
|
|
4
|
-
import { FetchContext } from '../../contexts/FetchContext';
|
|
5
|
-
import { IReqraftQueryConfig } from '../../utils/fetch';
|
|
6
|
-
|
|
7
|
-
export interface IReqraftUseFetch<T> {
|
|
8
|
-
data: T | undefined;
|
|
9
|
-
loading: boolean;
|
|
10
|
-
load: () => Promise<T>;
|
|
11
|
-
error: Error | undefined;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface IReqraftUseFetchOptions<T> extends IReqraftQueryConfig {
|
|
15
|
-
defaultData?: T;
|
|
16
|
-
loadOnMount?: boolean;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function useFetch<T>({
|
|
20
|
-
url,
|
|
21
|
-
method = 'GET',
|
|
22
|
-
body,
|
|
23
|
-
cache,
|
|
24
|
-
defaultData,
|
|
25
|
-
loadOnMount,
|
|
26
|
-
}: IReqraftUseFetchOptions<T>) {
|
|
27
|
-
const query = useContextSelector(FetchContext, (context) => {
|
|
28
|
-
switch (method) {
|
|
29
|
-
case 'GET':
|
|
30
|
-
return context.get;
|
|
31
|
-
case 'POST':
|
|
32
|
-
return context.post;
|
|
33
|
-
case 'PUT':
|
|
34
|
-
return context.put;
|
|
35
|
-
case 'DELETE':
|
|
36
|
-
return context.del;
|
|
37
|
-
default:
|
|
38
|
-
throw new Error('Invalid method');
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
const [loading, setLoading] = useState(loadOnMount);
|
|
43
|
-
const [data, setData] = useState<T | undefined>(defaultData);
|
|
44
|
-
const [error, setError] = useState<Error | undefined>();
|
|
45
|
-
|
|
46
|
-
async function load({
|
|
47
|
-
body: customBody,
|
|
48
|
-
mergeBodies,
|
|
49
|
-
}: { body?: Record<string | number, any>; mergeBodies?: boolean } = {}) {
|
|
50
|
-
setLoading(true);
|
|
51
|
-
|
|
52
|
-
const _body = mergeBodies ? { ...body, ...customBody } : customBody || body;
|
|
53
|
-
const response = await query<T>({ url, body: _body, cache });
|
|
54
|
-
|
|
55
|
-
setLoading(false);
|
|
56
|
-
|
|
57
|
-
if (response.ok) {
|
|
58
|
-
setData(response.data);
|
|
59
|
-
} else {
|
|
60
|
-
setError(response.error);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
useEffectOnce(() => {
|
|
65
|
-
if (loadOnMount) {
|
|
66
|
-
load();
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
return { data, loading, load, error };
|
|
71
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { useContextSelector } from 'use-context-selector';
|
|
2
|
-
import { IReqraftContext, ReqraftContext } from '../contexts/ReqraftContext';
|
|
3
|
-
|
|
4
|
-
export const useReqraftProperty = <T extends keyof IReqraftContext>(
|
|
5
|
-
property: T
|
|
6
|
-
): IReqraftContext[T] => {
|
|
7
|
-
const contextProperty = useContextSelector(ReqraftContext, (value) => {
|
|
8
|
-
if (!(property in value)) {
|
|
9
|
-
throw new Error(`Reqraft context property ${property} not found`);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return value[property];
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
return contextProperty;
|
|
16
|
-
};
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { ReqoreButton, ReqoreControlGroup, ReqoreP } from '@qoretechnologies/reqore';
|
|
2
|
-
import { StoryObj } from '@storybook/react';
|
|
3
|
-
import { fireEvent, within } from '@storybook/test';
|
|
4
|
-
import { storiesStorageMock } from '../../../__tests__/ mock';
|
|
5
|
-
import { testsWaitForText } from '../../../__tests__/utils';
|
|
6
|
-
import { StoryMeta } from '../../types';
|
|
7
|
-
import { useReqraftStorage } from './useStorage';
|
|
8
|
-
|
|
9
|
-
const meta = {
|
|
10
|
-
title: 'Hooks/useStorage',
|
|
11
|
-
render: () => {
|
|
12
|
-
const [storage, setStorage, removeValue] = useReqraftStorage<string>(
|
|
13
|
-
'some-path',
|
|
14
|
-
'This is a default value'
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
return (
|
|
18
|
-
<ReqoreControlGroup>
|
|
19
|
-
<ReqoreP>{storage}</ReqoreP>
|
|
20
|
-
<ReqoreButton onClick={() => setStorage('This is a NEW value')}>
|
|
21
|
-
Update storage
|
|
22
|
-
</ReqoreButton>
|
|
23
|
-
<ReqoreButton onClick={() => removeValue()}>Remove value</ReqoreButton>
|
|
24
|
-
</ReqoreControlGroup>
|
|
25
|
-
);
|
|
26
|
-
},
|
|
27
|
-
} as StoryMeta<any>;
|
|
28
|
-
|
|
29
|
-
export default meta;
|
|
30
|
-
export type Story = StoryObj<typeof meta>;
|
|
31
|
-
|
|
32
|
-
export const DefaultValue: Story = {
|
|
33
|
-
args: {
|
|
34
|
-
method: 'GET',
|
|
35
|
-
},
|
|
36
|
-
parameters: {
|
|
37
|
-
mockData: [
|
|
38
|
-
{
|
|
39
|
-
url: 'https://hq.qoretechnologies.com:8092/api/latest/users/_current_/storage',
|
|
40
|
-
method: 'GET',
|
|
41
|
-
status: 200,
|
|
42
|
-
response: {},
|
|
43
|
-
},
|
|
44
|
-
],
|
|
45
|
-
},
|
|
46
|
-
play: async () => {
|
|
47
|
-
await testsWaitForText('This is a default value');
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const StorageValue: Story = {
|
|
52
|
-
parameters: {
|
|
53
|
-
mockData: [...storiesStorageMock],
|
|
54
|
-
},
|
|
55
|
-
play: async () => {
|
|
56
|
-
await testsWaitForText('This is a storage value');
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export const ValueCanBeUpdated: Story = {
|
|
61
|
-
...StorageValue,
|
|
62
|
-
play: async ({ canvasElement }) => {
|
|
63
|
-
const canvas = within(canvasElement);
|
|
64
|
-
|
|
65
|
-
//await sleep(1000);
|
|
66
|
-
|
|
67
|
-
await testsWaitForText('This is a storage value');
|
|
68
|
-
await testsWaitForText('Update storage');
|
|
69
|
-
await fireEvent.click(canvas.getByText('Update storage'));
|
|
70
|
-
await testsWaitForText('This is a NEW value');
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export const ValueCanBeRemoved: Story = {
|
|
75
|
-
...StorageValue,
|
|
76
|
-
play: async ({ canvasElement }) => {
|
|
77
|
-
const canvas = within(canvasElement);
|
|
78
|
-
|
|
79
|
-
await testsWaitForText('This is a storage value');
|
|
80
|
-
await testsWaitForText('Remove value');
|
|
81
|
-
await fireEvent.click(canvas.getByText('Remove value'));
|
|
82
|
-
await testsWaitForText('This is a default value');
|
|
83
|
-
},
|
|
84
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { useContextSelector } from 'use-context-selector';
|
|
2
|
-
import { ReqraftStorageContext } from '../../contexts/StorageContext';
|
|
3
|
-
|
|
4
|
-
export type TReqraftStorageValue = string | number | boolean | Record<string | number, any> | any[];
|
|
5
|
-
export type TReqraftStorageHook<T extends TReqraftStorageValue> = [
|
|
6
|
-
T,
|
|
7
|
-
(newStorage: T) => void,
|
|
8
|
-
() => void,
|
|
9
|
-
];
|
|
10
|
-
|
|
11
|
-
export function useReqraftStorage<T extends TReqraftStorageValue>(
|
|
12
|
-
path: string,
|
|
13
|
-
defaultValue?: T,
|
|
14
|
-
includeAppPrefix?: boolean
|
|
15
|
-
): TReqraftStorageHook<T> {
|
|
16
|
-
const { getStorage, updateStorage, removeStorageValue } = useContextSelector(
|
|
17
|
-
ReqraftStorageContext,
|
|
18
|
-
({ getStorage, updateStorage, removeStorageValue }) => ({
|
|
19
|
-
getStorage,
|
|
20
|
-
updateStorage,
|
|
21
|
-
removeStorageValue,
|
|
22
|
-
})
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
return [
|
|
26
|
-
getStorage(path, defaultValue, includeAppPrefix),
|
|
27
|
-
(newStorage: T) => updateStorage(path, newStorage, includeAppPrefix),
|
|
28
|
-
() => removeStorageValue(path, includeAppPrefix),
|
|
29
|
-
];
|
|
30
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { TFormFieldType } from '../types/Form';
|
|
2
|
-
|
|
3
|
-
// @ts-expect-error "need to implement this"
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5
|
-
export const useValidation = (value: any, type?: TFormFieldType) => {
|
|
6
|
-
// Build validation...
|
|
7
|
-
|
|
8
|
-
return true;
|
|
9
|
-
};
|
package/src/index.tsx
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
IReqraftMenuItem,
|
|
3
|
-
IReqraftMenuProps,
|
|
4
|
-
ReqraftMenu,
|
|
5
|
-
TReqraftMenu,
|
|
6
|
-
TReqraftMenuItem,
|
|
7
|
-
} from './components/menu/Menu';
|
|
8
|
-
|
|
9
|
-
export { FormStringField, IStringFormFieldProps } from './components/form/fields/string/String';
|
|
10
|
-
export { IReqraftUseFetch, useFetch } from './hooks/useFetch/useFetch';
|
|
11
|
-
export { ReqraftProvider, ReqraftQueryClient } from './providers/ReqraftProvider';
|
|
12
|
-
export { query } from './utils/fetch';
|