chore: move cell hook into custom cell component

This commit is contained in:
ascarbek
2023-03-06 17:47:37 +06:00
parent 3635b6b63e
commit c8569d3c62
5 changed files with 79 additions and 24 deletions

View File

@ -3,12 +3,12 @@ import { CellCache } from '../../../stores/effects/database/cell/cell_cache';
import { FieldController } from '../../../stores/effects/database/field/field_controller'; import { FieldController } from '../../../stores/effects/database/field/field_controller';
import { CellControllerBuilder } from '../../../stores/effects/database/cell/controller_builder'; import { CellControllerBuilder } from '../../../stores/effects/database/cell/controller_builder';
import { DateCellDataPB, SelectOptionCellDataPB, URLCellDataPB } from '../../../../services/backend'; import { DateCellDataPB, SelectOptionCellDataPB, URLCellDataPB } from '../../../../services/backend';
import { useState } from 'react'; import { useEffect, useState } from 'react';
export const useCell = (cellIdentifier: CellIdentifier, cellCache: CellCache, fieldController: FieldController) => { export const useCell = (cellIdentifier: CellIdentifier, cellCache: CellCache, fieldController: FieldController) => {
const [data, setData] = useState<DateCellDataPB | URLCellDataPB | SelectOptionCellDataPB | string | undefined>(); const [data, setData] = useState<DateCellDataPB | URLCellDataPB | SelectOptionCellDataPB | string | undefined>();
const loadCell = async () => { useEffect(() => {
const builder = new CellControllerBuilder(cellIdentifier, cellCache, fieldController); const builder = new CellControllerBuilder(cellIdentifier, cellCache, fieldController);
const cellController = builder.build(); const cellController = builder.build();
cellController.subscribeChanged({ cellController.subscribeChanged({
@ -19,10 +19,14 @@ export const useCell = (cellIdentifier: CellIdentifier, cellCache: CellCache, fi
// ignore the return value, because we are using the subscription // ignore the return value, because we are using the subscription
void cellController.getCellData(); void cellController.getCellData();
};
// dispose the cell controller when the component is unmounted
return () => {
void cellController.dispose();
};
}, []);
return { return {
loadCell,
data, data,
}; };
}; };

View File

@ -1,9 +1,7 @@
import { useCell } from '../_shared/database-hooks/useCell';
import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc'; import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
import { CellCache } from '../../stores/effects/database/cell/cell_cache'; import { CellCache } from '../../stores/effects/database/cell/cell_cache';
import { FieldController } from '../../stores/effects/database/field/field_controller'; import { FieldController } from '../../stores/effects/database/field/field_controller';
import { useEffect } from 'react'; import { FieldType } from '../../../services/backend';
import { DateCellDataPB, FieldType, SelectOptionCellDataPB } from '../../../services/backend';
import { BoardOptionsCell } from './BoardOptionsCell'; import { BoardOptionsCell } from './BoardOptionsCell';
import { BoardDateCell } from './BoardDateCell'; import { BoardDateCell } from './BoardDateCell';
import { BoardTextCell } from './BoardTextCell'; import { BoardTextCell } from './BoardTextCell';
@ -17,23 +15,28 @@ export const BoardCell = ({
cellCache: CellCache; cellCache: CellCache;
fieldController: FieldController; fieldController: FieldController;
}) => { }) => {
const { loadCell, data } = useCell(cellIdentifier, cellCache, fieldController);
useEffect(() => {
void (async () => {
await loadCell();
})();
}, []);
return ( return (
<> <>
{cellIdentifier.fieldType === FieldType.SingleSelect || {cellIdentifier.fieldType === FieldType.SingleSelect ||
cellIdentifier.fieldType === FieldType.MultiSelect || cellIdentifier.fieldType === FieldType.MultiSelect ||
cellIdentifier.fieldType === FieldType.Checklist ? ( cellIdentifier.fieldType === FieldType.Checklist ? (
<BoardOptionsCell value={data as SelectOptionCellDataPB | undefined}></BoardOptionsCell> <BoardOptionsCell
cellIdentifier={cellIdentifier}
cellCache={cellCache}
fieldController={fieldController}
></BoardOptionsCell>
) : cellIdentifier.fieldType === FieldType.DateTime ? ( ) : cellIdentifier.fieldType === FieldType.DateTime ? (
<BoardDateCell value={data as DateCellDataPB | undefined}></BoardDateCell> <BoardDateCell
cellIdentifier={cellIdentifier}
cellCache={cellCache}
fieldController={fieldController}
></BoardDateCell>
) : ( ) : (
<BoardTextCell value={data as string | undefined}></BoardTextCell> <BoardTextCell
cellIdentifier={cellIdentifier}
cellCache={cellCache}
fieldController={fieldController}
></BoardTextCell>
)} )}
</> </>
); );

View File

@ -1,5 +1,18 @@
import { DateCellDataPB } from '../../../services/backend'; import { DateCellDataPB } from '../../../services/backend';
import { useCell } from '../_shared/database-hooks/useCell';
import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
import { CellCache } from '../../stores/effects/database/cell/cell_cache';
import { FieldController } from '../../stores/effects/database/field/field_controller';
export const BoardDateCell = ({ value }: { value: DateCellDataPB | undefined }) => { export const BoardDateCell = ({
return <div>{value?.date || ''}</div>; cellIdentifier,
cellCache,
fieldController,
}: {
cellIdentifier: CellIdentifier;
cellCache: CellCache;
fieldController: FieldController;
}) => {
const { data } = useCell(cellIdentifier, cellCache, fieldController);
return <div>{(data as DateCellDataPB | undefined)?.date || ''}</div>;
}; };

View File

@ -1,5 +1,25 @@
import { SelectOptionCellDataPB, SelectOptionColorPB } from '../../../services/backend'; import { SelectOptionCellDataPB } from '../../../services/backend';
import { useCell } from '../_shared/database-hooks/useCell';
import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
import { CellCache } from '../../stores/effects/database/cell/cell_cache';
import { FieldController } from '../../stores/effects/database/field/field_controller';
export const BoardOptionsCell = ({ value }: { value: SelectOptionCellDataPB | undefined }) => { export const BoardOptionsCell = ({
return <>{value?.select_options?.map((option, index) => <div key={index}>{option?.name || ''}</div>) || ''}</>; cellIdentifier,
cellCache,
fieldController,
}: {
cellIdentifier: CellIdentifier;
cellCache: CellCache;
fieldController: FieldController;
}) => {
const { data } = useCell(cellIdentifier, cellCache, fieldController);
return (
<>
{(data as SelectOptionCellDataPB | undefined)?.select_options?.map((option, index) => (
<div key={index}>{option?.name || ''}</div>
)) || ''}
</>
);
}; };

View File

@ -1,3 +1,18 @@
export const BoardTextCell = ({ value }: { value: string | undefined }) => { import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
return <div>{value || ''}</div>; import { CellCache } from '../../stores/effects/database/cell/cell_cache';
import { FieldController } from '../../stores/effects/database/field/field_controller';
import { useCell } from '../_shared/database-hooks/useCell';
export const BoardTextCell = ({
cellIdentifier,
cellCache,
fieldController,
}: {
cellIdentifier: CellIdentifier;
cellCache: CellCache;
fieldController: FieldController;
}) => {
const { data } = useCell(cellIdentifier, cellCache, fieldController);
return <div>{(data as string | undefined) || ''}</div>;
}; };