@postxl/generator 0.31.0 → 0.32.0
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.
|
@@ -8,8 +8,6 @@ const string_1 = require("../../../lib/utils/string");
|
|
|
8
8
|
*/
|
|
9
9
|
function generateModelContext({ model, meta }) {
|
|
10
10
|
const queryName = (0, string_1.toCamelCase)(model.name);
|
|
11
|
-
// NOTE: Reference name is not used outside of this file, that's why it's local.
|
|
12
|
-
const contextReferenceName = `${(0, string_1.toPascalCase)(model.name)}Context`;
|
|
13
11
|
const imports = imports_1.ImportsGenerator.from(meta.react.folderPath).addImport({
|
|
14
12
|
items: [model.typeName, model.brandedIdType],
|
|
15
13
|
from: meta.types.importPath,
|
|
@@ -19,51 +17,36 @@ function generateModelContext({ model, meta }) {
|
|
|
19
17
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
20
18
|
import React, { useMemo } from 'react'
|
|
21
19
|
|
|
22
|
-
import { createUseContext } from '@lib/react'
|
|
23
20
|
import { trpc } from '@lib/trpc'
|
|
24
21
|
import { filterMap, mapMap } from '@lib/utils'
|
|
25
22
|
|
|
26
23
|
${imports.generate()}
|
|
27
24
|
|
|
28
|
-
type
|
|
25
|
+
type ProviderHookType = {
|
|
29
26
|
ready: boolean
|
|
30
27
|
map: ${resultMapTypeDefinition}
|
|
31
28
|
list: ${model.typeName}[]
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
const ReactContext = React.createContext<Context | null>(null)
|
|
35
|
-
|
|
36
31
|
/**
|
|
37
|
-
*
|
|
32
|
+
* A React utility hook to access the model data in the generated forms.
|
|
38
33
|
*/
|
|
39
|
-
export const ${meta.react.context.
|
|
34
|
+
export const ${meta.react.context.hookFnName} = (): ProviderHookType => {
|
|
40
35
|
const ${queryName} = trpc.${meta.trpc.getMap.reactQueryMethod}.useQuery()
|
|
41
36
|
|
|
42
|
-
const
|
|
37
|
+
const value = useMemo<ProviderHookType>(() => {
|
|
43
38
|
const data: ${resultMapTypeDefinition} = ${queryName}.data ?? new ${resultMapTypeDefinition}()
|
|
44
39
|
|
|
45
40
|
return {
|
|
46
41
|
ready: ${queryName}.isLoading === false,
|
|
47
42
|
map: data,
|
|
48
|
-
list:
|
|
43
|
+
list: Array.from(data.values())
|
|
49
44
|
}
|
|
50
45
|
}, [${queryName}.isLoading, ${queryName}.data])
|
|
51
46
|
|
|
52
|
-
return
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export const ${meta.react.context.testContextProviderName} = ({
|
|
56
|
-
value,
|
|
57
|
-
children
|
|
58
|
-
}: React.PropsWithChildren<{ value: Context }>) => {
|
|
59
|
-
return <ReactContext.Provider value={value}>{children}</ReactContext.Provider>
|
|
47
|
+
return value
|
|
60
48
|
}
|
|
61
49
|
|
|
62
|
-
/**
|
|
63
|
-
* A React hook to access the model data.
|
|
64
|
-
*/
|
|
65
|
-
export const ${meta.react.context.hookFnName} = createUseContext(ReactContext, "${contextReferenceName}")
|
|
66
|
-
|
|
67
50
|
/**
|
|
68
51
|
* Returns a single instance of the model if the data is ready.
|
|
69
52
|
*/
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateModelLibraryComponents = void 0;
|
|
4
|
-
const meta_1 = require("../../../lib/meta");
|
|
5
|
-
const fields_1 = require("../../../lib/schema/fields");
|
|
6
4
|
const imports_1 = require("../../../lib/imports");
|
|
7
5
|
/**
|
|
8
6
|
* Generates components that may be used to list all entries of a given data type.
|
|
@@ -21,18 +19,6 @@ function generateModelLibraryComponents({ model, meta }) {
|
|
|
21
19
|
items: [context.hookFnName, components.modals.editComponentName, components.modals.deleteComponentName],
|
|
22
20
|
from: meta.react.folderPath,
|
|
23
21
|
});
|
|
24
|
-
for (const relation of (0, fields_1.getRelationFields)(model)) {
|
|
25
|
-
const refMeta = (0, meta_1.getModelMetadata)({ model: relation.relationToModel });
|
|
26
|
-
imports.addImport({
|
|
27
|
-
items: [refMeta.react.context.contextProviderName],
|
|
28
|
-
from: refMeta.react.folderPath,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
const reactContextProvidersOfDependencies = new Set();
|
|
32
|
-
for (const relation of (0, fields_1.getRelationFields)(model)) {
|
|
33
|
-
const refMeta = (0, meta_1.getModelMetadata)({ model: relation.relationToModel });
|
|
34
|
-
reactContextProvidersOfDependencies.add(refMeta.react.context.contextProviderName);
|
|
35
|
-
}
|
|
36
22
|
return `
|
|
37
23
|
import React, { useState } from 'react'
|
|
38
24
|
|
|
@@ -81,16 +67,11 @@ function generateModelLibraryComponents({ model, meta }) {
|
|
|
81
67
|
]}
|
|
82
68
|
/>
|
|
83
69
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
onHide={() => setIsEditModalOpen(false)}
|
|
90
|
-
/>
|
|
91
|
-
`,
|
|
92
|
-
components: reactContextProvidersOfDependencies,
|
|
93
|
-
})}
|
|
70
|
+
<${components.modals.editComponentName}
|
|
71
|
+
data={item}
|
|
72
|
+
show={isEditModalOpen}
|
|
73
|
+
onHide={() => setIsEditModalOpen(false)}
|
|
74
|
+
/>
|
|
94
75
|
|
|
95
76
|
<${components.modals.deleteComponentName}
|
|
96
77
|
id={item.id}
|
|
@@ -106,27 +87,3 @@ function generateModelLibraryComponents({ model, meta }) {
|
|
|
106
87
|
`;
|
|
107
88
|
}
|
|
108
89
|
exports.generateModelLibraryComponents = generateModelLibraryComponents;
|
|
109
|
-
/**
|
|
110
|
-
* Deterministifcally nests a given React component in an array of parent wrappers.
|
|
111
|
-
*/
|
|
112
|
-
function nestChildrenInComponents({ child, components }) {
|
|
113
|
-
if (components.size === 0) {
|
|
114
|
-
return child;
|
|
115
|
-
}
|
|
116
|
-
const sortedComponents = [...components.values()].sort((a, b) => a.localeCompare(b));
|
|
117
|
-
return _nestInComponents({ child, components: sortedComponents });
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* DO NOT USE THIS FUNCTION DIRECTLY. Use `nestChildrenInComponents` instead.
|
|
121
|
-
*/
|
|
122
|
-
function _nestInComponents({ child, components }) {
|
|
123
|
-
if (components.length === 0) {
|
|
124
|
-
return child;
|
|
125
|
-
}
|
|
126
|
-
const [parent, ...rest] = components;
|
|
127
|
-
return `
|
|
128
|
-
<${parent}>
|
|
129
|
-
${_nestInComponents({ child, components: rest })}
|
|
130
|
-
</${parent}>
|
|
131
|
-
`;
|
|
132
|
-
}
|
package/dist/lib/meta.d.ts
CHANGED
|
@@ -340,14 +340,6 @@ export type ModelMetaData = {
|
|
|
340
340
|
* Name of the function that may be used to get a single instance of this model (e.g. `useAggregation`).
|
|
341
341
|
*/
|
|
342
342
|
instanceGetterHookFnName: Types.Fnction;
|
|
343
|
-
/**
|
|
344
|
-
* Name of the context wrapper.
|
|
345
|
-
*/
|
|
346
|
-
testContextProviderName: Types.VariableName;
|
|
347
|
-
/**
|
|
348
|
-
* Name of the context wrapper for this model.
|
|
349
|
-
*/
|
|
350
|
-
contextProviderName: Types.VariableName;
|
|
351
343
|
};
|
|
352
344
|
/**
|
|
353
345
|
* Names of React components that should be generated for this model.
|
package/dist/lib/meta.js
CHANGED
|
@@ -134,8 +134,6 @@ function getModelMetadata({ model }) {
|
|
|
134
134
|
context: {
|
|
135
135
|
hookFnName: Types.toFunction(`use${PascalCase}Context`),
|
|
136
136
|
instanceGetterHookFnName: Types.toFunction(`use${PascalCase}`),
|
|
137
|
-
testContextProviderName: Types.toVariableName(`${PascalCase}TestContextProvider`),
|
|
138
|
-
contextProviderName: Types.toVariableName(`${PascalCase}Provider`),
|
|
139
137
|
},
|
|
140
138
|
components: {
|
|
141
139
|
modals: {
|