Skip to content

Commit

Permalink
Make dataset readonly after commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadsw committed Jan 28, 2025
1 parent 1556f3f commit d2b8ffd
Showing 1 changed file with 70 additions and 30 deletions.
100 changes: 70 additions & 30 deletions specifyweb/frontend/js_src/lib/components/WorkBench/handsontable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import type { Dataset } from '../WbPlanView/Wrapped';
import type { WbMapping } from './mapping';
import type { WbPickLists } from './pickLists';
import { getPhysicalColToMappingCol } from './hotHelpers';
import { BATCH_EDIT_KEY, BatchEditPack, isBatchEditNullRecord } from './batchEditHelpers';
import {
BATCH_EDIT_KEY,
BatchEditPack,
isBatchEditNullRecord,
} from './batchEditHelpers';
import { CellProperties } from 'handsontable/settings';

export function configureHandsontable(
Expand All @@ -33,23 +37,40 @@ export function identifyDefaultValues(
});
}

type GetProperty = (physicalRow: number, physicalCol: number, _property: string | number) => Partial<CellProperties>;
type GetProperty = (
physicalRow: number,
physicalCol: number,
_property: string | number
) => Partial<CellProperties>;

function curryCells(hot: Handsontable,
function curryCells(
hot: Handsontable,
mappings: WbMapping | undefined,
dataset: Dataset,
pickLists: WbPickLists) : void {
const identifyPickLists = getPickListsIdentifier(pickLists);
const identifyNullRecords = getIdentifyNullRecords(hot, mappings, dataset);
hot.updateSettings({cells: (physicalRow, physicalColumn, property)=>{
const pickListsResults = identifyPickLists?.(physicalRow, physicalColumn, property) ?? {};
const nullRecordsResults = identifyNullRecords?.(physicalRow, physicalColumn, property) ?? {};
return {...pickListsResults, ...nullRecordsResults}
}})
}
pickLists: WbPickLists
): void {
const identifyPickLists = getPickListsIdentifier(pickLists);
const identifyNullRecords = getIdentifyNullRecords(hot, mappings, dataset);
hot.updateSettings({
cells: (physicalRow, physicalColumn, property) => {
const pickListsResults =
identifyPickLists?.(physicalRow, physicalColumn, property) ?? {};
const nullRecordsResults =
dataset.uploadresult?.success === true
? {}
: (identifyNullRecords?.(physicalRow, physicalColumn, property) ??
{});
return { ...pickListsResults, ...nullRecordsResults };
},
});
}

function getPickListsIdentifier(pickLists: WbPickLists): undefined | GetProperty {
const callback: GetProperty = (_physicalRow, physicalCol, _property) => physicalCol in pickLists ? ({
function getPickListsIdentifier(
pickLists: WbPickLists
): undefined | GetProperty {
const callback: GetProperty = (_physicalRow, physicalCol, _property) =>
physicalCol in pickLists
? {
type: 'autocomplete',
source: writable(pickLists[physicalCol].items),
strict: pickLists[physicalCol].readOnly,
Expand All @@ -62,32 +83,51 @@ function getPickListsIdentifier(pickLists: WbPickLists): undefined | GetProperty
'case-sensitive',
sortByRelevance: false,
trimDropdown: false,
})
: ({ type: 'text' });
return callback
}
: { type: 'text' };
return callback;
}

function getIdentifyNullRecords(hot: Handsontable, mappings: WbMapping | undefined, dataset: Dataset) : GetProperty | undefined {
function getIdentifyNullRecords(
hot: Handsontable,
mappings: WbMapping | undefined,
dataset: Dataset
): GetProperty | undefined {
if (dataset.isupdate !== true || mappings === undefined) return undefined;
const makeNullRecordsReadOnly: GetProperty = (physicalRow, physicalCol, _property) => {
const physicalColToMappingCol = getPhysicalColToMappingCol(mappings, dataset);
const makeNullRecordsReadOnly: GetProperty = (
physicalRow,
physicalCol,
_property
) => {
const physicalColToMappingCol = getPhysicalColToMappingCol(
mappings,
dataset
);
const mappingCol = physicalColToMappingCol(physicalCol);
if (mappingCol === -1 || mappingCol === undefined){
if (mappingCol === -1 || mappingCol === undefined) {
// Definitely don't need to anything, not even mapped
return {readOnly: true};
return { readOnly: true };
}

const batchEditRaw: string | undefined = hot.getDataAtRow(hot.toVisualRow(physicalRow)).at(-1) ?? undefined;
if ((batchEditRaw === undefined) || (
const batchEditRaw: string | undefined =
hot.getDataAtRow(hot.toVisualRow(physicalRow)).at(-1) ?? undefined;
if (
batchEditRaw === undefined ||
// will happen for new rows + rows auto-added at the bottom.
batchEditRaw.trim() === ''
)){
return {readOnly: false};
}
const batchEditPack: BatchEditPack | undefined = JSON.parse(batchEditRaw)[BATCH_EDIT_KEY];
return { readOnly: isBatchEditNullRecord(batchEditPack, mappings.lines[mappingCol].mappingPath)};
) {
return { readOnly: false };
}
return makeNullRecordsReadOnly
const batchEditPack: BatchEditPack | undefined =
JSON.parse(batchEditRaw)[BATCH_EDIT_KEY];
return {
readOnly: isBatchEditNullRecord(
batchEditPack,
mappings.lines[mappingCol].mappingPath
),
};
};
return makeNullRecordsReadOnly;
}

function setSort(hot: Handsontable, dataset: Dataset): void {
Expand Down

0 comments on commit d2b8ffd

Please sign in to comment.