Skip to content

Commit

Permalink
fix: contemplate one2many's without form views
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Nov 13, 2024
1 parent 4c5fc17 commit 5de9244
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/widgets/base/one2many/One2manyInputInfinite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ export const One2manyInput: React.FC<One2manyInputInfiniteProps> = (
showFormChangesDialogIfNeeded,
]);

const showPointerCursorInRows = useMemo(() => {
return views.get("form")?.fields !== undefined;
}, [views]);

return (
<>
<One2manyTopBar
Expand Down Expand Up @@ -271,6 +275,7 @@ export const One2manyInput: React.FC<One2manyInputInfiniteProps> = (
ooui={treeOoui}
context={context}
onRowDoubleClick={onRowDoubleClick}
showPointerCursorInRows={showPointerCursorInRows}
onRowSelectionChange={setSelectedRowKeys}
relation={relation}
onChangeFirstVisibleRowIndex={onChangeFirstVisibleRowIndex}
Expand Down
7 changes: 5 additions & 2 deletions src/widgets/base/one2many/One2manyTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "@gisce/react-formiga-table";
import { One2manyItem } from "./One2manyInput";
import { Tree as TreeOoui } from "@gisce/ooui";
import { RefObject, useCallback, useRef } from "react";
import { RefObject, useCallback, useMemo, useRef } from "react";
import { getTableColumns, getTableItems } from "@/helpers/treeHelper";
import { COLUMN_COMPONENTS } from "@/widgets/views/Tree/treeComponents";
import useDeepCompareEffect from "use-deep-compare-effect";
Expand All @@ -22,7 +22,7 @@ import { useLocale } from "@gisce/react-formiga-components";

export type One2manyTreeProps = {
items: One2manyItem[];
onRowDoubleClick: (record: any) => void;
onRowDoubleClick?: (record: any) => void;
readOnly: boolean;
height?: number;
ooui: TreeOoui;
Expand All @@ -47,6 +47,7 @@ export type One2manyTreeProps = {
dataForHash: One2manyTreeDataForHash;
aggregates?: TreeAggregates;
selectedRowKeys?: number[];
showPointerCursorInRows?: boolean;
};

const DEFAULT_HEIGHT = 400;
Expand All @@ -68,6 +69,7 @@ export const One2manyTree = ({
dataForHash,
aggregates,
selectedRowKeys = [],
showPointerCursorInRows = true,
}: One2manyTreeProps) => {
const internalGridRef = useRef<InfiniteTableRef>();
const tableRef: RefObject<InfiniteTableRef> = gridRef! || internalGridRef!;
Expand Down Expand Up @@ -205,6 +207,7 @@ export const One2manyTree = ({
columns={columns}
onRequestData={onRequestData}
onRowDoubleClick={onRowDoubleClick}
showPointerCursorInRows={showPointerCursorInRows}
readonly={readOnly}
onRowStyle={onRowStyle}
onRowSelectionChange={onRowSelectionChange}
Expand Down
18 changes: 14 additions & 4 deletions src/widgets/base/one2many/useOne2manyForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useOne2manyForm = ({
context: any[];
relation: string;
treeView: TreeView;
formView: FormView;
formView?: FormView;
triggerChange: (items: One2manyItem[]) => void;
}) => {
const [formHasChanges, setFormHasChanges] = useState<boolean>(false);
Expand All @@ -38,6 +38,7 @@ export const useOne2manyForm = ({

const getOriginalItemsWithRestoredItemId = useCallback(
async ({ id }: { id: number }) => {
if (!formView) return;
const updatedFormObject = (
await ConnectionProvider.getHandler().readObjects({
model: relation,
Expand Down Expand Up @@ -70,7 +71,7 @@ export const useOne2manyForm = ({

return updatedItems;
},
[context, formView.fields, items, relation, treeView.fields],
[context, formView, items, relation, treeView.fields],
);

const onFormChanges = useCallback(
Expand All @@ -82,11 +83,20 @@ export const useOne2manyForm = ({
);

const reloadOriginalValuesForCurrentItem = useCallback(async () => {
if (!formView) return;
const { id } = items[itemIndex];
if (!id) return;
const originalItems = await getOriginalItemsWithRestoredItemId({ id });
triggerChange(originalItems);
}, [getOriginalItemsWithRestoredItemId, itemIndex, items, triggerChange]);
if (originalItems) {
triggerChange(originalItems);
}
}, [
formView,
getOriginalItemsWithRestoredItemId,
itemIndex,
items,
triggerChange,
]);

const showFormChangesDialogIfNeeded = useCallback(
(callback: () => void) => {
Expand Down
5 changes: 3 additions & 2 deletions src/widgets/base/one2many/useOne2manyFormModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useOne2manyFormModal = ({
setCurrentView: (view: ViewType) => void;
setItemIndex: (index: number) => void;
relation: string;
formView: FormView;
formView?: FormView;
context: any;
}) => {
const [showFormModal, setShowFormModal] = useState<boolean>(false);
Expand Down Expand Up @@ -108,6 +108,7 @@ export const useOne2manyFormModal = ({

const openItemInFormModal = useDeepCompareCallback(
async (item: One2manyItem) => {
if (!formView) return;
const { id: itemId } = item;

let itemsToLoadFrom: One2manyItem[] | undefined = items;
Expand All @@ -128,7 +129,7 @@ export const useOne2manyFormModal = ({
setContinuousEntryMode(false);
setShowFormModal(true);
},
[context, formView.fields, items, relation, setCurrentView, setItemIndex],
[context, formView, items, relation, setCurrentView, setItemIndex],
);

return {
Expand Down

0 comments on commit 5de9244

Please sign in to comment.