Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type React from 'react';
import * as Y from 'yjs';

import { DatabaseContext, DatabaseContextState, FieldType } from '@/application/database-yjs';
import { useUpdateCellDispatch } from '@/application/database-yjs/dispatch';
import { useUpdateCellDispatch, useUpdateStartEndTimeCell } from '@/application/database-yjs/dispatch';
import {
RowId,
YDatabase,
Expand Down Expand Up @@ -62,6 +62,13 @@ function getCellData(rowDoc: YDoc) {
return cells?.get(fieldId)?.get(YjsDatabaseKey.data);
}

function getCell(rowDoc: YDoc) {
const row = rowDoc.getMap(YjsEditorKey.data_section).get(YjsEditorKey.database_row);
const cells = row?.get(YjsDatabaseKey.cells);

return cells?.get(fieldId);
}

function createWrapper(contextValue: DatabaseContextState) {
return ({ children }: { children: React.ReactNode }) => (
<DatabaseContext.Provider value={contextValue}>{children}</DatabaseContext.Provider>
Expand Down Expand Up @@ -94,3 +101,57 @@ describe('useUpdateCellDispatch', () => {
expect(ensureRow).toHaveBeenCalledWith(rowId);
});
});

describe('useUpdateStartEndTimeCell', () => {
it('ensures a missing row doc before committing the calendar time update', async () => {
const databaseDoc = createDatabaseDoc();
const rowDoc = createRowDoc(rowId, databaseId, {});
const ensureRow = jest.fn<Promise<YDoc>, [RowId]>().mockResolvedValue(rowDoc);
const contextValue: DatabaseContextState = {
readOnly: false,
databaseDoc,
databasePageId: viewId,
activeViewId: viewId,
rowMap: {},
ensureRow,
workspaceId: 'workspace-id',
};
const { result } = renderHook(() => useUpdateStartEndTimeCell(), {
wrapper: createWrapper(contextValue),
});

result.current(rowId, fieldId, '100', '200', false);

await waitFor(() => {
const cell = getCell(rowDoc);

expect(cell?.get(YjsDatabaseKey.data)).toBe('100');
expect(cell?.get(YjsDatabaseKey.end_timestamp)).toBe('200');
expect(cell?.get(YjsDatabaseKey.include_time)).toBe(true);
});
expect(ensureRow).toHaveBeenCalledWith(rowId);
});

it('does not commit calendar time updates when the row doc cannot be loaded', async () => {
const databaseDoc = createDatabaseDoc();
const ensureRow = jest.fn<Promise<YDoc | undefined>, [RowId]>().mockResolvedValue(undefined);
const contextValue: DatabaseContextState = {
readOnly: false,
databaseDoc,
databasePageId: viewId,
activeViewId: viewId,
rowMap: {},
ensureRow,
workspaceId: 'workspace-id',
};
const { result } = renderHook(() => useUpdateStartEndTimeCell(), {
wrapper: createWrapper(contextValue),
});

result.current(rowId, fieldId, '100');

await waitFor(() => {
expect(ensureRow).toHaveBeenCalledWith(rowId);
});
});
});
2 changes: 1 addition & 1 deletion src/components/database/fullcalendar/CalendarContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function CalendarContent({ onDataChange, normalToolbarRef, onDragEnd }: C

// Handle external event creation (FullCalendar eventReceive callback)
const handleEventReceive = useCallback(
(receiveInfo: EventReceiveArg) => {
async (receiveInfo: EventReceiveArg) => {
Log.debug('📅 FullCalendar eventReceive:', receiveInfo);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function useCalendarEvents() {

// Handle event drop (move event to different time)
const handleEventDrop = useCallback(
(dropInfo: EventDropArg) => {
async (dropInfo: EventDropArg) => {
Log.debug('📅 Event dropped:', dropInfo.event);

try {
Expand Down Expand Up @@ -74,7 +74,7 @@ export function useCalendarEvents() {

// Handle event resize (change event duration)
const handleEventResize = useCallback(
(resizeInfo: EventResizeDoneArg) => {
async (resizeInfo: EventResizeDoneArg) => {
Log.debug('📅 Event resized:', resizeInfo.event);

try {
Expand Down
Loading