@postxl/generator 0.41.0 → 0.41.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.
@@ -38,6 +38,7 @@ function generateModelCreateModalComponent({ model, meta }) {
38
38
  const { fields } = model;
39
39
  const { react: { components: { modals }, }, trpc, } = meta;
40
40
  const selectorCollector = id_collector_1.SelectorCollector.from(meta.seed.constantName + '-createModal');
41
+ const dimensions = getModalComponentDimensions({ model });
41
42
  return `
42
43
  /* eslint-disable @typescript-eslint/no-unused-vars */
43
44
  ${getFormImports({ model, meta })}
@@ -46,10 +47,25 @@ type CreateInputData = {
46
47
  ${getCreateFormInputFields({ model })}
47
48
  }
48
49
 
50
+ const INITIAL_VALUES = {
51
+ ${fields
52
+ .filter((f) => f.kind !== 'id' && !f.attributes.isReadonly)
53
+ .map((field) => `${getFormikFieldName(field.name)}: null,`)
54
+ .join('\n')}
55
+ }
56
+
49
57
  /**
50
58
  * A modal component that lets the user create a new ${meta.userFriendlyName} instance.
51
59
  */
52
- export const ${modals.createComponentName} = ({ show, onHide }: { show: boolean; onHide: () => void }) => {
60
+ export const ${modals.createComponentName} = ({
61
+ show,
62
+ onHide,
63
+ data
64
+ }: {
65
+ show: boolean;
66
+ onHide: () => void;
67
+ data?: Partial<CreateInputData>
68
+ }) => {
53
69
  const Typed = React.useMemo(
54
70
  () => createTypedForm<CreateInputData>().with({ ${(() => {
55
71
  const components = new Set();
@@ -76,18 +92,14 @@ export const ${modals.createComponentName} = ({ show, onHide }: { show: boolean;
76
92
  })()} }),
77
93
  []
78
94
  )
95
+
79
96
  const cache = trpc.useContext()
80
97
  const mutation = trpc.${trpc.create.reactQueryMethod}.useMutation()
81
98
 
82
99
  return (
83
- <ModalWithNavigationContainer label="Create new ${meta.userFriendlyName}" show={show} onHide={onHide} fixed wide>
100
+ <ModalWithNavigationContainer label="Create new ${meta.userFriendlyName}" show={show} onHide={onHide} ${dimensions}>
84
101
  <Typed.Formik
85
- initialValues={{
86
- ${fields
87
- .filter((f) => f.kind !== 'id' && !f.attributes.isReadonly)
88
- .map((field) => `${getFormikFieldName(field.name)}: null,`)
89
- .join('\n')}
90
- }}
102
+ initialValues={{ ...INITIAL_VALUES, ...data }}
91
103
  validate={(values) => {
92
104
  const errors: FormikErrors<CreateInputData> = {}
93
105
 
@@ -112,7 +124,13 @@ export const ${modals.createComponentName} = ({ show, onHide }: { show: boolean;
112
124
  {
113
125
  loading: "Creating new ${meta.userFriendlyName}...",
114
126
  success: "New ${meta.userFriendlyName} created!",
115
- error: "Something went wrong...",
127
+ error: (error) => {
128
+ if (error instanceof TRPCClientError) {
129
+ return error.message
130
+ }
131
+
132
+ return 'Something went wrong...'
133
+ },
116
134
  },
117
135
  )
118
136
 
@@ -153,6 +171,7 @@ function generateEditModalModelComponent({ model, meta }) {
153
171
  const { fields } = model;
154
172
  const { react: { components }, trpc, } = meta;
155
173
  const selectorCollector = id_collector_1.SelectorCollector.from(meta.seed.constantName + '-editModal');
174
+ const dimensions = getModalComponentDimensions({ model });
156
175
  return `
157
176
  /* eslint-disable @typescript-eslint/no-unused-vars */
158
177
  ${getFormImports({ model, meta })}
@@ -198,7 +217,7 @@ export const ${components.modals.editComponentName} = ({
198
217
  const mutation = trpc.${trpc.update.reactQueryMethod}.useMutation()
199
218
 
200
219
  return (
201
- <ModalWithNavigationContainer label="Edit ${meta.userFriendlyName}" show={show} onHide={onHide} fixed wide>
220
+ <ModalWithNavigationContainer label="Edit ${meta.userFriendlyName}" show={show} onHide={onHide} ${dimensions}>
202
221
  <Typed.Formik
203
222
  initialValues={{
204
223
  ${fields
@@ -243,7 +262,13 @@ export const ${components.modals.editComponentName} = ({
243
262
  {
244
263
  loading: "Updating ${meta.userFriendlyName}...",
245
264
  success: "${meta.userFriendlyName} updated!",
246
- error: "Something went wrong...",
265
+ error: (error) => {
266
+ if (error instanceof TRPCClientError) {
267
+ return error.message
268
+ }
269
+
270
+ return 'Something went wrong...'
271
+ },
247
272
  },
248
273
  )
249
274
 
@@ -376,6 +401,7 @@ function getFormImports({ model, meta }) {
376
401
  });
377
402
  }
378
403
  return `
404
+ import { TRPCClientError } from '@trpc/client'
379
405
  import React from 'react'
380
406
  import { toast } from 'react-hot-toast'
381
407
  import { FormikErrors } from 'formik'
@@ -665,3 +691,14 @@ function getFormikValidationCases({ model }) {
665
691
  function getFormikFieldName(name) {
666
692
  return `formik${StringUtils.toPascalCase(name)}`;
667
693
  }
694
+ /**
695
+ * Returns a string representing the "missing" dimension parameters for the modal component
696
+ * depending on the type of the modal.
697
+ */
698
+ function getModalComponentDimensions({ model }) {
699
+ const _fields = model.fields.filter((f) => f.kind !== 'id' && !f.attributes.isReadonly);
700
+ if (_fields.length > 3) {
701
+ return 'fixed wide';
702
+ }
703
+ return '';
704
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.41.0",
3
+ "version": "0.41.2",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {