import type { EntityData } from "bknd";
import { useState } from "react";
import { useEntityMutate } from "bknd/client";
import { useBkndData } from "ui/client/schema/data/use-bknd-data";
import { Button } from "ui/components/buttons/Button";
import { Message } from "ui/components/display/Message";
import { useBrowserTitle } from "ui/hooks/use-browser-title";
import { useSearch } from "ui/hooks/use-search";
import * as AppShell from "ui/layouts/AppShell/AppShell";
import { Breadcrumbs2 } from "ui/layouts/AppShell/Breadcrumbs2";
import { routes, useNavigate } from "ui/lib/routes";
import { EntityForm } from "ui/modules/data/components/EntityForm";
import { useEntityForm } from "ui/modules/data/hooks/useEntityForm";
import { s } from "bknd/utils";
import { notifications } from "@mantine/notifications";
import { useEntityAdminOptions } from "ui/options";
import { Dropdown } from "ui/components/overlay/Dropdown";
import { TbDots } from "react-icons/tb";
import { IconButton } from "ui/components/buttons/IconButton";
export function DataEntityCreate({ params }) {
const { $data } = useBkndData();
const [navigate, _, _goBack] = useNavigate();
const entity = $data.entity(params.entity as string);
if (!entity) {
return ;
} else if (entity.type === "system") {
return ;
}
const options = useEntityAdminOptions(entity, "create");
const [error, setError] = useState(null);
useBrowserTitle(["Data", entity.label, "Create"]);
const $q = useEntityMutate(entity.name);
// @todo: use entity schema for prefilling
const search = useSearch(s.object({}), {});
const backHref = routes.data.entity.list(entity.name);
const goBack = () => _goBack({ fallback: backHref });
async function onSubmitted(changeSet?: EntityData) {
console.log("create:changeSet", changeSet);
if (!changeSet) return;
try {
const result = await $q.create(changeSet);
if (error) setError(null);
if (result.id) {
notifications.show({
title: `Creating ${entity?.label}`,
message: `Successfully created with ID ${result.id}`,
color: "green",
});
navigate(routes.data.entity.edit(params.entity, result.id));
} else {
goBack();
}
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to create");
}
}
const { Form, handleSubmit } = useEntityForm({
action: "create",
entity: entity,
initialData: search.value,
onSubmitted,
});
const fieldsDisabled = $q.isLoading || $q.isValidating || Form.state.isSubmitting;
return (
<>
{options.actions?.context && (
)}
{options.actions?.primary?.map(
(button, key) =>
button && ,
)}
[state.canSubmit, state.isSubmitting]}
children={([canSubmit, isSubmitting]) => (
)}
/>
>
}
className="pl-3"
>
{options.header}
{error && (
Create failed: {error}
)}
{options.footer}
>
);
}