Skip to content

Commit

Permalink
Editing interface repaired. trailing zeros issue during editing shoul…
Browse files Browse the repository at this point in the history
…d be resolved.
  • Loading branch information
siddheshraze committed Feb 7, 2025
1 parent 75af177 commit 7964788
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 24 deletions.
33 changes: 24 additions & 9 deletions frontend/components/client/datagridcolumns.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HEADER_ALIGN } from '@/config/macros';
import { Autocomplete, Box, Chip, IconButton, Stack, Typography } from '@mui/joy';
import { GridColDef, GridRenderEditCellParams } from '@mui/x-data-grid';
import { GridColDef, GridPreProcessEditCellProps, GridRenderEditCellParams } from '@mui/x-data-grid';
import React, { Dispatch, SetStateAction, useState } from 'react';
import { AttributeStatusOptions } from '@/config/sqlrdsdefinitions/core';
import { standardizeGridColumns } from '@/components/client/clientmacros';
Expand Down Expand Up @@ -369,6 +369,20 @@ export function InputChip({
);
}

function preprocessor(params: GridPreProcessEditCellProps) {
const { props } = params;
let newValue = props.value;

if (typeof newValue === 'string' && newValue.trim() !== '') {
const parsedValue = parseFloat(newValue);
if (!isNaN(parsedValue)) {
newValue = parseFloat(parsedValue.toFixed(2));
}
}

return { ...props, value: newValue };
}

export const MeasurementsSummaryViewGridColumns: GridColDef[] = standardizeGridColumns([
{
field: 'id',
Expand Down Expand Up @@ -432,13 +446,13 @@ export const MeasurementsSummaryViewGridColumns: GridColDef[] = standardizeGridC
headerName: 'X',
renderHeader: () => formatHeader('X', 'Stem'),
flex: 0.7,
type: 'number',
valueFormatter: (value: any) => {
return Number(value).toFixed(2);
},
maxWidth: 100,
editable: true,
filterOperators: customNumericOperators
filterOperators: customNumericOperators,
preProcessEditCellProps: params => preprocessor(params)
},
{
field: 'stemLocalY',
Expand All @@ -451,17 +465,17 @@ export const MeasurementsSummaryViewGridColumns: GridColDef[] = standardizeGridC
return Number(value).toFixed(2);
},
editable: true,
filterOperators: customNumericOperators
filterOperators: customNumericOperators,
preProcessEditCellProps: params => preprocessor(params)
},
{
field: 'measuredDBH',
headerName: 'DBH',
flex: 0.5,
editable: true,
valueFormatter: (value: any) => {
return Number(value).toFixed(2);
},
filterOperators: customNumericOperators
valueFormatter: (value: any) => parseFloat(Number(value).toFixed(2)),
filterOperators: customNumericOperators,
preProcessEditCellProps: params => preprocessor(params)
},
{
field: 'measuredHOM',
Expand All @@ -471,7 +485,8 @@ export const MeasurementsSummaryViewGridColumns: GridColDef[] = standardizeGridC
valueFormatter: (value: any) => {
return Number(value).toFixed(2);
},
filterOperators: customNumericOperators
filterOperators: customNumericOperators,
preProcessEditCellProps: params => preprocessor(params)
},
{
field: 'description',
Expand Down
54 changes: 39 additions & 15 deletions frontend/components/datagrids/measurementscommons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
Dropdown,
FormLabel,
IconButton,
Input,
Menu,
MenuButton,
MenuItem,
Expand Down Expand Up @@ -473,6 +474,26 @@ const EditToolbar = (props: EditToolbarProps) => {
);
};

function EditMeasurements({ params }: { params: GridRenderEditCellParams }) {
const initialValue = params.value ? Number(params.value).toFixed(2) : '0.00';
const [value, setValue] = useState(initialValue);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newValue = event.target.value;

if (/^\d*\.?\d{0,2}$/.test(newValue) || newValue === '') {
setValue(newValue);
}
};

const handleBlur = () => {
const formattedValue = parseFloat(value).toFixed(2);
params.api.setEditCellValue({ id: params.id, field: params.field, value: parseFloat(formattedValue) });
};

return <Input autoFocus value={value} onChange={handleChange} onBlur={handleBlur} size="sm" sx={{ width: '100%', height: '100%' }} type="text" />;
}

export default function MeasurementsCommons(props: Readonly<MeasurementsCommonsProps>) {
const {
addNewRowToGrid,
Expand Down Expand Up @@ -1273,6 +1294,20 @@ export default function MeasurementsCommons(props: Readonly<MeasurementsCommonsP

const columns = useMemo(() => {
const commonColumns = gridColumns.map(column => {
if (column.field === 'attributes') {
column = {
...column,
renderEditCell: (params: GridRenderEditCellParams) => (
<InputChip params={params} selectableAttributes={selectableAttributes} setReloadAttributes={setReloadAttrs} />
)
};
}
if (['measuredDBH', 'measuredHOM', 'stemLocalX', 'stemLocalY'].includes(column.field)) {
column = {
...column,
renderEditCell: (params: GridRenderEditCellParams) => <EditMeasurements params={params} />
};
}
return {
...column,
renderCell: (params: GridCellParams) => {
Expand All @@ -1281,21 +1316,14 @@ export default function MeasurementsCommons(props: Readonly<MeasurementsCommonsP
const rowError = rowHasError(params.id);
const cellError = cellHasError(column.field, params.id) ? getCellErrorMessages(column.field, Number(params.row.coreMeasurementID)) : '';

const isMeasurementField = column.field === 'measuredDBH' || column.field === 'measuredHOM';
const isMeasurementField =
column.field === 'measuredDBH' || column.field === 'measuredHOM' || column.field.includes('X') || column.field.includes('Y');
const isAttributeField = column.field === 'attributes';
const attributeValues = column.field === 'attributes' && typeof params.value === 'string' ? params.value.replace(/\s+/g, '').split(';') : [];

function renderMeasurementDetails() {
return (
<Typography level="body-sm">
{column.field === 'measuredDBH'
? params.row.measuredDBH
? Number(params.row.measuredDBH).toFixed(2)
: 'null'
: params.row.measuredHOM
? Number(params.row.measuredHOM).toFixed(2)
: 'null'}
</Typography>
<Typography level="body-sm">{isMeasurementField && params.row[column.field] ? Number(params.row[column.field]).toFixed(2) : 'null'}</Typography>
);
}

Expand Down Expand Up @@ -1342,11 +1370,7 @@ export default function MeasurementsCommons(props: Readonly<MeasurementsCommonsP
)}
</Box>
);
},
renderEditCell: (params: GridRenderEditCellParams) =>
column.field === 'attributes' && selectableAttributes.length > 0 ? (
<InputChip params={params} selectableAttributes={selectableAttributes} setReloadAttributes={setReloadAttrs} />
) : undefined
}
};
});
if (locked || (session?.user.userStatus !== 'global' && session?.user.userStatus !== 'db admin')) {
Expand Down
13 changes: 13 additions & 0 deletions frontend/config/datamapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ export class GenericMapper<RDS, Result> implements IDataMapper<RDS, Result> {
else if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
return bitToBoolean(value);
}
// dynamically detect if values are decimal and provide handling
else if (this.isDecimal(value)) {
return parseFloat(value.toFixed(2));
}
// Now process date-like fields more carefully
else if (this.isDateKey(key) && value !== null) {
return parseDate(value);
Expand All @@ -193,6 +197,15 @@ export class GenericMapper<RDS, Result> implements IDataMapper<RDS, Result> {
// Add other specific date-related keys as needed
return ['measurementdate', 'createddate', 'updateddate'].includes(lowerKey);
}

private isDecimal(value: any): boolean {
if (typeof value !== 'number') return false;

// Check if the number has more than 2 decimal places
const decimalPlaces = (value.toString().split('.')[1] || '').length;

return decimalPlaces > 2; // If more than 2 decimal places, it's likely a DECIMAL type
}
}

class MapperFactory {
Expand Down

0 comments on commit 7964788

Please sign in to comment.