mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-19 05:46:04 +00:00
introduced auth strategy actions to allow user creation in UI
This commit is contained in:
22
app/src/ui/modals/debug/OverlayModal.tsx
Normal file
22
app/src/ui/modals/debug/OverlayModal.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { ContextModalProps } from "@mantine/modals";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
export function OverlayModal({
|
||||
context,
|
||||
id,
|
||||
innerProps: { content }
|
||||
}: ContextModalProps<{ content?: ReactNode }>) {
|
||||
return content;
|
||||
}
|
||||
|
||||
OverlayModal.defaultTitle = undefined;
|
||||
OverlayModal.modalProps = {
|
||||
withCloseButton: false,
|
||||
classNames: {
|
||||
size: "md",
|
||||
root: "bknd-admin",
|
||||
content: "text-center justify-center",
|
||||
title: "font-bold !text-md",
|
||||
body: "py-3 px-5 gap-4 flex flex-col"
|
||||
}
|
||||
};
|
||||
@@ -7,21 +7,31 @@ import {
|
||||
} from "ui/components/form/json-schema";
|
||||
|
||||
import type { ContextModalProps } from "@mantine/modals";
|
||||
import { Alert } from "ui/components/display/Alert";
|
||||
|
||||
type Props = JsonSchemaFormProps & {
|
||||
onSubmit?: (data: any) => void | Promise<void>;
|
||||
autoCloseAfterSubmit?: boolean;
|
||||
onSubmit?: (
|
||||
data: any,
|
||||
context: {
|
||||
close: () => void;
|
||||
}
|
||||
) => void | Promise<void>;
|
||||
};
|
||||
|
||||
export function SchemaFormModal({
|
||||
context,
|
||||
id,
|
||||
innerProps: { schema, uiSchema, onSubmit }
|
||||
innerProps: { schema, uiSchema, onSubmit, autoCloseAfterSubmit }
|
||||
}: ContextModalProps<Props>) {
|
||||
const [valid, setValid] = useState(false);
|
||||
const formRef = useRef<JsonSchemaFormRef>(null);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const was_submitted = useRef(false);
|
||||
const [error, setError] = useState<string>();
|
||||
|
||||
function handleChange(data) {
|
||||
const valid = formRef.current?.validateForm() ?? false;
|
||||
function handleChange(data, isValid) {
|
||||
const valid = isValid();
|
||||
console.log("Data changed", data, valid);
|
||||
setValid(valid);
|
||||
}
|
||||
@@ -30,29 +40,45 @@ export function SchemaFormModal({
|
||||
context.closeModal(id);
|
||||
}
|
||||
|
||||
async function handleClickAdd() {
|
||||
await onSubmit?.(formRef.current?.formData());
|
||||
handleClose();
|
||||
async function handleSubmit() {
|
||||
was_submitted.current = true;
|
||||
if (!formRef.current?.validateForm()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setSubmitting(true);
|
||||
await onSubmit?.(formRef.current?.formData(), {
|
||||
close: handleClose,
|
||||
setError
|
||||
});
|
||||
setSubmitting(false);
|
||||
|
||||
if (autoCloseAfterSubmit !== false) {
|
||||
handleClose();
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="pt-3 pb-3 px-3 gap-4 flex flex-col">
|
||||
<JsonSchemaForm
|
||||
tagName="form"
|
||||
ref={formRef}
|
||||
schema={schema}
|
||||
uiSchema={uiSchema}
|
||||
className="legacy hide-required-mark fieldset-alternative mute-root"
|
||||
onChange={handleChange}
|
||||
onSubmit={handleClickAdd}
|
||||
/>
|
||||
<div className="flex flex-row justify-end gap-2">
|
||||
<Button onClick={handleClose}>Cancel</Button>
|
||||
<Button variant="primary" onClick={handleClickAdd} disabled={!valid}>
|
||||
Create
|
||||
</Button>
|
||||
<>
|
||||
{error && <Alert.Exception message={error} />}
|
||||
<div className="pt-3 pb-3 px-3 gap-4 flex flex-col">
|
||||
<JsonSchemaForm
|
||||
tagName="form"
|
||||
ref={formRef}
|
||||
schema={schema}
|
||||
uiSchema={uiSchema}
|
||||
className="legacy hide-required-mark fieldset-alternative mute-root"
|
||||
onChange={handleChange}
|
||||
onSubmit={handleSubmit}
|
||||
/>
|
||||
<div className="flex flex-row justify-end gap-2">
|
||||
<Button onClick={handleClose}>Cancel</Button>
|
||||
<Button variant="primary" onClick={handleSubmit} disabled={!valid || submitting}>
|
||||
Create
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -63,7 +89,7 @@ SchemaFormModal.modalProps = {
|
||||
root: "bknd-admin",
|
||||
header: "!bg-primary/5 border-b border-b-muted !py-3 px-5 !h-auto !min-h-px",
|
||||
content: "rounded-lg select-none",
|
||||
title: "font-bold !text-md",
|
||||
title: "!font-bold !text-md",
|
||||
body: "!p-0"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { ModalProps } from "@mantine/core";
|
||||
import { ModalsProvider, modals as mantineModals } from "@mantine/modals";
|
||||
import { modals as $modals, ModalsProvider, closeModal, openContextModal } from "@mantine/modals";
|
||||
import { transformObject } from "core/utils";
|
||||
import type { ComponentProps } from "react";
|
||||
import { OverlayModal } from "ui/modals/debug/OverlayModal";
|
||||
import { DebugModal } from "./debug/DebugModal";
|
||||
import { SchemaFormModal } from "./debug/SchemaFormModal";
|
||||
import { TestModal } from "./debug/TestModal";
|
||||
@@ -9,7 +10,8 @@ import { TestModal } from "./debug/TestModal";
|
||||
const modals = {
|
||||
test: TestModal,
|
||||
debug: DebugModal,
|
||||
form: SchemaFormModal
|
||||
form: SchemaFormModal,
|
||||
overlay: OverlayModal
|
||||
};
|
||||
|
||||
declare module "@mantine/modals" {
|
||||
@@ -33,17 +35,22 @@ function open<Modal extends keyof typeof modals>(
|
||||
) {
|
||||
const title = _title ?? modals[modal].defaultTitle ?? undefined;
|
||||
const cmpModalProps = modals[modal].modalProps ?? {};
|
||||
return mantineModals.openContextModal({
|
||||
const props = {
|
||||
title,
|
||||
...modalProps,
|
||||
...cmpModalProps,
|
||||
modal,
|
||||
innerProps
|
||||
});
|
||||
};
|
||||
openContextModal(props);
|
||||
return {
|
||||
close: () => close(modal),
|
||||
closeAll: $modals.closeAll
|
||||
};
|
||||
}
|
||||
|
||||
function close<Modal extends keyof typeof modals>(modal: Modal) {
|
||||
return mantineModals.close(modal);
|
||||
return closeModal(modal);
|
||||
}
|
||||
|
||||
export const bkndModals = {
|
||||
@@ -53,5 +60,5 @@ export const bkndModals = {
|
||||
>,
|
||||
open,
|
||||
close,
|
||||
closeAll: mantineModals.closeAll
|
||||
closeAll: $modals.closeAll
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user