better code: sounder
Massimo Melina committed
Mar 17, 2024 at 10:52 UTC
1cee85f3a26702f598b366808534084105172266
1 file changed
+34
-34
admin/src/dialog.ts
+34
-34
@@ -123,9 +123,9 @@ export function confirmDialog(msg: ReactNode, { href, confirmText="Go", dontText
123
h(Flex, {},
124
h('a', {
125
href,
126
- onClick: () => closeDialog(true),
126
+ onClick: () => dialog.close(true),
127
}, h(Button, { variant: 'contained' }, confirmText)),
128
- h(Button, { onClick: () => closeDialog(false) }, dontText),
128
+ h(Button, { onClick: () => dialog.close(false) }, dontText),
129
),
130
)
131
}
@@ -143,37 +143,38 @@ export async function formDialog<T>(
143
form: FormDialog<T> | ((values: Partial<T>) => FormDialog<T>),
144
},
145
) : Promise<T> {
146
- return new Promise(resolve => newDialog({
147
- className: 'dialog-confirm',
148
- onClose: resolve,
149
- ...options,
150
- Content
151
- }) )
152
-
153
- function Content() {
154
- const [curValues, setCurValues] = useState<Partial<T>>(values||{})
155
- const { onChange, before, ...props } = typeof form === 'function' ? form(curValues) : form
156
- return h(Fragment, {},
157
- before,
158
- h(Form, {
159
- ...props,
160
- values: curValues,
161
- set(v, k) {
162
- setCurValues(curValues => {
163
- const newV = { ...curValues, [k]: v }
164
- onChange?.(newV, { setValues: setCurValues })
165
- return newV
146
+ return new Promise(resolve => {
147
+ const dialog = newDialog({
148
+ className: 'dialog-confirm',
149
+ onClose: resolve,
150
+ ...options,
151
+ Content() {
152
+ const [curValues, setCurValues] = useState<Partial<T>>(values||{})
153
+ const { onChange, before, ...props } = typeof form === 'function' ? form(curValues) : form
154
+ return h(Fragment, {},
155
+ before,
156
+ h(Form, {
157
+ ...props,
158
+ values: curValues,
159
+ set(v, k) {
160
+ setCurValues(curValues => {
161
+ const newV = { ...curValues, [k]: v }
162
+ onChange?.(newV, { setValues: setCurValues })
163
+ return newV
164
+ })
165
+ },
166
+ save: {
167
+ ...props.save,
168
+ onClick() {
169
+ dialog.close(curValues)
170
+ }
171
+ }
172
})
167
- },
168
- save: {
169
- ...props.save,
170
- onClick() {
171
- closeDialog(curValues)
172
- }
173
- }
174
- })
175
- )
176
- }
173
+ )
174
+ }
175
+ })
176
+ })
177
+
178
}
179
180
export async function promptDialog(msg: ReactNode, { value, field, save, addToBar=[], ...props }:any={}) : Promise<string | undefined> {
@@ -220,8 +221,7 @@ export function toast(msg: string | ReactElement, type: AlertType | ReactElement
221
}
222
}
223
})
223
- const { close } = dialog
224
- setTimeout(close, ms)
224
+ setTimeout(dialog.close, ms)
225
return dialog
226
227
function Content(){