import { getHotkeyHandler, useHotkeys } from "@mantine/hooks"; import type { FieldApi } from "@tanstack/react-form"; import { ucFirst } from "core/utils"; import type { EntityData, RelationField } from "data"; import { useEffect, useRef, useState } from "react"; import { TbEye } from "react-icons/tb"; import { useClient } from "ui/client"; import { useBknd } from "ui/client/bknd"; import { Button } from "ui/components/buttons/Button"; import * as Formy from "ui/components/form/Formy"; import { Popover } from "ui/components/overlay/Popover"; import { useEntities } from "ui/container"; import { routes } from "ui/lib/routes"; import { useLocation } from "wouter"; import { EntityTable } from "../EntityTable"; // @todo: allow clear if not required export function EntityRelationalFormField({ fieldApi, field, data, disabled, tabIndex }: { fieldApi: FieldApi; field: RelationField; data?: EntityData; disabled?: boolean; tabIndex?: number; }) { const { app } = useBknd(); const entity = app.entity(field.target())!; const [query, setQuery] = useState({ limit: 10, page: 1, perPage: 10 }); const [location, navigate] = useLocation(); const ref = useRef(null); const client = useClient(); const container = useEntities( field.target(), { limit: query.limit, offset: (query.page - 1) * query.limit //select: entity.getSelect(undefined, "form") }, { enabled: true } ); const [_value, _setValue] = useState<{ id: number | undefined; [key: string]: any }>(); const referenceField = data?.[field.reference()]; const relationalField = data?.[field.name]; useEffect(() => { _setValue(data?.[field.reference()]); }, [referenceField]); useEffect(() => { (async () => { const rel_value = field.target(); if (!rel_value || !relationalField) return; console.log("-- need to fetch", field.target(), relationalField); const fetched = await client.api.data.readOne(field.target(), relationalField); if (fetched.res.ok && fetched.data) { _setValue(fetched.data as any); } console.log("-- fetched", fetched); console.log("relation", { referenceField, relationalField, data, field, entity }); })(); }, [relationalField]); /*const initialValue: { id: number | undefined; [key: string]: any } = data?.[ field.reference() ] ?? { id: data?.[field.name], };*/ function handleViewItem(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); console.log("yo"); if (_value) { navigate(routes.data.entity.edit(entity.name, _value.id as any)); } } /*console.log( "relationfield:data", { _value, initialValue }, data, field.reference(), entity, //container.entity, //data[field.reference()], data?.[field.name], field, );*/ // fix missing value on fields that are required useEffect(() => { if (field.isRequired() && !fieldApi.state.value) { fieldApi.setValue(container.data?.[0]?.id); } }, [container.data]); return ( {field.getLabel()}
( { fieldApi.setValue(row.id); _setValue(row as any); toggle(); }} onClickPage={(page) => { console.log("setting page", page); setQuery((prev) => ({ ...prev, page })); }} /> )} >
{ ref.current?.click(); } ] ])} > {_value ? ( <>
{ucFirst(entity.name)}
{_value && Object.entries(_value).map(([key, value]) => { const field = entity.getField(key)!; if (field.isHidden("table")) return null; const _value = field.getValue(value, "table"); return (
{field.getLabel()}: {" "} {_value !== null && typeof value !== "undefined" ? ( {_value} ) : ( null )}
); })}
) : (
- Select -
)}
{/* {container.data ? ( <> {emptyOption} {!field.isRequired() && emptyOption} {container.data?.map(renderRow)} ) : ( )} */}
); } const PopoverTable = ({ container, entity, query, toggle, onClickRow, onClickPage }) => { function handleNext() { if (query.limit * query.page < container.meta?.count) { onClickPage(query.page + 1); } } function handlePrev() { if (query.page > 1) { onClickPage(query.page - 1); } } useHotkeys([ ["ArrowRight", handleNext], ["ArrowLeft", handlePrev], ["Escape", toggle] ]); return (
); };