Skip to content

feat(database): switch columns #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -2,6 +2,7 @@ import {
CellEditingStoppedEvent,
ColDef,
ColSpanParams,
ColumnMovedEvent,
ICellRendererParams,
} from 'ag-grid-community';
import { AgGridReact } from 'ag-grid-react';
Expand Down Expand Up @@ -158,6 +159,11 @@ export const DatabaseGrid = ({
[documentId, tableId],
);

const onColumnMoved = useCallback((e: ColumnMovedEvent<DatabaseRow>) => {
if (e.finished === false) { return}
console.log('Event Column Moved', e, e.column?.colId);
}, []);

return (
<>
<Box style={{ height: '100%', width: '100%' }}>
Expand All @@ -169,6 +175,7 @@ export const DatabaseGrid = ({
enableCellSpan={true}
onCellEditingStopped={onCellEditingStopped}
pinnedBottomRowData={[addNewRowRow]}
onColumnMoved={onColumnMoved}
/>
</Box>
<AddButtonComponent addColumn={addColumn} />
Expand Down
101 changes: 101 additions & 0 deletions src/frontend/apps/impress/src/features/grist/useGristCrudTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { APIError, errorCauses, gristFetchApi } from '@/api';
import { TableDescription } from './useListGristTables';

export enum ColumnType {
TEXT = 'Text',
NUMBER = 'Numeric',
BOOLEAN = 'Bool',
}

export const useGristCrudColumns = () => {
const createTable = async (
name: string,
): Promise<{
documentId: string;
tableId: string;
}> => {
const DEFAULT_WORKSPACE_ID = 2;
const docUrl = `workspaces/${DEFAULT_WORKSPACE_ID}/docs`;
try {
const docResponse = await gristFetchApi(docUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name }),
});

if (!docResponse.ok) {
throw new APIError(
'Failed to fetch Grist tables',
await errorCauses(docResponse),
);
}

const documentId = (await docResponse.json()) as string;

const tableUrl = `docs/${documentId}/tables`;
const tableResponse = await gristFetchApi(tableUrl);
if (!tableResponse.ok) {
throw new APIError(
'Failed to fetch Grist tables',
await errorCauses(tableResponse),
);
}

const tableDescription = (await tableResponse.json()) as TableDescription;

if (tableDescription.tables.length === 0) {
throw new Error('No tables found in the created document');
}

if (tableDescription.tables.length > 1) {
throw new Error(
'More than one table has been found in the created document, this should not happen.',
);
}

return {
documentId,
tableId: tableDescription.tables[0].id,
};
} catch (error) {
console.error('Error creating Grist table:', error);
throw error;
}
}

const updateTable = async (
{documentId, tableId}: {
documentId: string;
tableId: string;
}
): Promise<void> => {
const table = { id: tableId, fields: [] };
const tableUrl = `docs/${documentId}/tables`;
try {
const response = await gristFetchApi(tableUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ tables: [table] }),
});

if (!response.ok) {
throw new APIError(
'Failed to update Grist table',
await errorCauses(response),
);
}


} catch (error) {
console.error('Error updating Grist table:', error);
throw error;
}
};


return { createTable };
};