asasvirtuais 2.3.0 → 2.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/package.json +1 -1
- package/packages/action.tsx +30 -9
- package/packages/react-interface.tsx +14 -6
package/package.json
CHANGED
package/packages/action.tsx
CHANGED
|
@@ -5,7 +5,7 @@ export type ActionProps<Params, Result> = {
|
|
|
5
5
|
params: Partial<Params>
|
|
6
6
|
action: (fields: Params) => Promise<Result>
|
|
7
7
|
onResult?: (result: Result) => any
|
|
8
|
-
onError?: (error: Error) => any
|
|
8
|
+
onError?: (error: Error) => any
|
|
9
9
|
autoTrigger?: boolean
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -14,11 +14,6 @@ export function useActionProvider<Fields, Result>(props: React.PropsWithChildren
|
|
|
14
14
|
const [error, setError] = useState<Error | null>(null)
|
|
15
15
|
const [result, setResult] = useState<Result | null>(null)
|
|
16
16
|
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
if (props.autoTrigger)
|
|
19
|
-
props.action(props.params as Fields)
|
|
20
|
-
}, [])
|
|
21
|
-
|
|
22
17
|
/** receives an event (optional) and processes the form submission using the fields state as props, stores the result or error in the respective states */
|
|
23
18
|
const submit = useCallback(
|
|
24
19
|
async (e?: any) => {
|
|
@@ -44,20 +39,46 @@ export function useActionProvider<Fields, Result>(props: React.PropsWithChildren
|
|
|
44
39
|
[props.params, props.action, props.onError]
|
|
45
40
|
)
|
|
46
41
|
|
|
42
|
+
const callback = useCallback(async (fields: Fields) => {
|
|
43
|
+
setLoading(true)
|
|
44
|
+
setError(null)
|
|
45
|
+
try {
|
|
46
|
+
const result = await props.action(fields)
|
|
47
|
+
if (props.onResult)
|
|
48
|
+
props.onResult(result)
|
|
49
|
+
setResult(result)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(error)
|
|
52
|
+
if (props.onError)
|
|
53
|
+
props.onError(error as Error)
|
|
54
|
+
setError(error as Error)
|
|
55
|
+
throw error
|
|
56
|
+
} finally {
|
|
57
|
+
setLoading(false)
|
|
58
|
+
}
|
|
59
|
+
return false
|
|
60
|
+
}, [props.action])
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (props.autoTrigger)
|
|
64
|
+
callback(props.params as Fields)
|
|
65
|
+
}, [])
|
|
66
|
+
|
|
67
|
+
|
|
47
68
|
return {
|
|
48
69
|
loading,
|
|
49
70
|
result,
|
|
50
71
|
error,
|
|
51
72
|
submit,
|
|
52
|
-
callback
|
|
73
|
+
callback,
|
|
53
74
|
params: props.params,
|
|
54
75
|
}
|
|
55
76
|
}
|
|
56
77
|
|
|
57
78
|
export const ActionContext = createContext<ReturnType<typeof useActionProvider<any, any>> | undefined>(undefined)
|
|
58
79
|
|
|
59
|
-
export function ActionProvider<Fields, Result>({children, ...params}: ActionProps<Fields, Result> & {
|
|
60
|
-
children: React.ReactNode | (
|
|
80
|
+
export function ActionProvider<Fields, Result>({ children, ...params }: ActionProps<Fields, Result> & {
|
|
81
|
+
children: React.ReactNode | ((props: ReturnType<typeof useActionProvider<Fields, Result>>) => React.ReactNode)
|
|
61
82
|
}) {
|
|
62
83
|
const context = useActionProvider<Fields, Result>(params)
|
|
63
84
|
return (
|
|
@@ -65,22 +65,28 @@ export function useTableProvider<TSchema extends TableSchema>({
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
const
|
|
68
|
+
const TableRegistryContext = createContext<Record<string, ReturnType<typeof useTableProvider<any>>> | undefined>(undefined)
|
|
69
69
|
|
|
70
70
|
export function TableProvider<TSchema extends TableSchema>({ children, ...props }: React.PropsWithChildren<TableProviderProps<TSchema>>) {
|
|
71
71
|
|
|
72
72
|
const context = useTableProvider(props)
|
|
73
|
+
const registry = useContext(TableRegistryContext) ?? {}
|
|
74
|
+
|
|
75
|
+
const newRegistry = useMemo(() => {
|
|
76
|
+
return { ...registry, [props.table]: context }
|
|
77
|
+
}, [registry, props.table, context])
|
|
73
78
|
|
|
74
79
|
return (
|
|
75
|
-
<
|
|
80
|
+
<TableRegistryContext.Provider value={newRegistry}>
|
|
76
81
|
{children}
|
|
77
|
-
</
|
|
82
|
+
</TableRegistryContext.Provider>
|
|
78
83
|
)
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
export function TableConsumer<TSchema extends TableSchema>({ children }: { children: React.ReactNode | ((props: ReturnType<typeof useTableProvider<TSchema>>) => React.ReactNode) }) {
|
|
82
|
-
const
|
|
83
|
-
if (!
|
|
87
|
+
const registry = useContext(TableRegistryContext)
|
|
88
|
+
if (!registry || Object.keys(registry).length === 0) throw new Error('TableConsumer must be used within a TableProvider.')
|
|
89
|
+
const context = Object.values(registry)[0]
|
|
84
90
|
return (
|
|
85
91
|
<>
|
|
86
92
|
{typeof children === 'function' ? children(context) : children}
|
|
@@ -89,7 +95,9 @@ export function TableConsumer<TSchema extends TableSchema>({ children }: { child
|
|
|
89
95
|
}
|
|
90
96
|
|
|
91
97
|
export function useTable<TSchema extends TableSchema>(table: string, schema: TSchema) {
|
|
92
|
-
|
|
98
|
+
const registry = useContext(TableRegistryContext)
|
|
99
|
+
if (!registry || !registry[table]) throw new Error(`useTable("${table}") must be used within a TableProvider for that table.`)
|
|
100
|
+
return registry[table] as ReturnType<typeof useTableProvider<TSchema>>
|
|
93
101
|
}
|
|
94
102
|
|
|
95
103
|
export function CreateForm<TSchema extends TableSchema>({ table, schema, defaults, onSuccess, children }: {
|