PropertyType Management abgeschlossen
This commit is contained in:
@@ -66,6 +66,7 @@ export async function getPropertyTypesByGroupId(groupId: number): Promise<Proper
|
||||
.get(`${API_BASE_URL}/getPropertyTypesByGroupId/${groupId}`)
|
||||
.json<ApiResponse<BackendPropertyType[]>>()
|
||||
|
||||
return response.data.map(toClientPropertyType)
|
||||
// the backend returns null instead of [] when there are no matching property types
|
||||
return (response.data ?? []).map(toClientPropertyType)
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,8 @@ export async function getPropertyTypeGroupsByParentId(parentId: number): Promise
|
||||
.get(`${API_BASE_URL}/getPropertyTypeGroupsByParentId/${parentId}`)
|
||||
.json<ParentGroupsResponse>()
|
||||
|
||||
return response.data
|
||||
// the backend returns null instead of [] when there are no matching groups
|
||||
return response.data ?? []
|
||||
}
|
||||
|
||||
export async function deletePropertyTypeFromPropertyTypeGroupId(propertyTypeGroupId: number): Promise<void> {
|
||||
|
||||
@@ -25,7 +25,8 @@ export async function getValuesFromPropertyTypeId(propertyTypeId: number): Promi
|
||||
.get(`${API_BASE_URL}/getValuesFromPropertyTypeId/${propertyTypeId}`)
|
||||
.json<ApiResponse<Value[]>>()
|
||||
|
||||
return response.data
|
||||
// the backend returns null instead of [] when there are no matching values
|
||||
return response.data ?? []
|
||||
}
|
||||
|
||||
export async function getValuesFromPropertyId(propertyId: number): Promise<Value[]> {
|
||||
@@ -33,7 +34,7 @@ export async function getValuesFromPropertyId(propertyId: number): Promise<Value
|
||||
.get(`${API_BASE_URL}/getValuesFromPropertyId/${propertyId}`)
|
||||
.json<ApiResponse<Value[]>>()
|
||||
|
||||
return response.data
|
||||
return response.data ?? []
|
||||
}
|
||||
|
||||
export async function deleteValueFromPropertyId(valueId: number, propertyId: number): Promise<void> {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import Dialog from '@mui/material/Dialog';
|
||||
import DialogTitle from '@mui/material/DialogTitle';
|
||||
import DialogContent from '@mui/material/DialogContent';
|
||||
import DialogActions from '@mui/material/DialogActions';
|
||||
import Button from '@mui/material/Button';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import Stack from '@mui/material/Stack';
|
||||
|
||||
export type EntityFormValues = {
|
||||
label: string;
|
||||
description: string;
|
||||
};
|
||||
|
||||
type EntityFormDialogProps = {
|
||||
open: boolean;
|
||||
title: string;
|
||||
onClose: () => void;
|
||||
onSubmit: (values: EntityFormValues) => Promise<void> | void;
|
||||
};
|
||||
|
||||
const EntityFormDialog = ({ open, title, onClose, onSubmit }: EntityFormDialogProps) => {
|
||||
const [label, setLabel] = useState('');
|
||||
const [description, setDescription] = useState('');
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setLabel('');
|
||||
setDescription('');
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!label.trim()) return;
|
||||
setSubmitting(true);
|
||||
try {
|
||||
await onSubmit({ label: label.trim(), description: description.trim() });
|
||||
onClose();
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onClose={onClose} fullWidth maxWidth="xs">
|
||||
<DialogTitle>{title}</DialogTitle>
|
||||
<DialogContent>
|
||||
<Stack spacing={2} sx={{ mt: 1 }}>
|
||||
<TextField
|
||||
label="Label"
|
||||
value={label}
|
||||
onChange={(e) => setLabel(e.target.value)}
|
||||
autoFocus
|
||||
required
|
||||
fullWidth
|
||||
/>
|
||||
<TextField
|
||||
label="Description"
|
||||
value={description}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
fullWidth
|
||||
multiline
|
||||
minRows={2}
|
||||
/>
|
||||
</Stack>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={onClose} disabled={submitting}>Cancel</Button>
|
||||
<Button onClick={handleSubmit} variant="contained" disabled={submitting || !label.trim()}>
|
||||
Add
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default EntityFormDialog;
|
||||
@@ -1,26 +1,52 @@
|
||||
import React from 'react';
|
||||
import React, { useCallback, useState } from 'react';
|
||||
import Box from '@mui/material/Box';
|
||||
import Typography from '@mui/material/Typography';
|
||||
|
||||
const Properties: React.FC = () => {
|
||||
return (
|
||||
<Box component="section" sx={{ p: 2, border: '1px dashed grey' }}>
|
||||
<Typography variant="body1" gutterBottom>
|
||||
Property Type Management
|
||||
</Typography>
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex", // Arrange children in a row
|
||||
flexDirection: "row",
|
||||
width: "100vw", // Full width of the parent
|
||||
p: 2, // Padding around the flex container
|
||||
bgcolor: "#f5f5f5"
|
||||
}}
|
||||
>
|
||||
import PropertyTypeGroupTree from './PropertyTypeGroupTree';
|
||||
import PropertyTypeGrid from './PropertyTypeGrid';
|
||||
import ValueGrid from './ValueGrid';
|
||||
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
const Properties: React.FC = () => {
|
||||
const [selectedGroupId, setSelectedGroupId] = useState<number | null>(null);
|
||||
const [selectedGroupLabel, setSelectedGroupLabel] = useState<string | null>(null);
|
||||
const [selectedPropertyTypeId, setSelectedPropertyTypeId] = useState<number | null>(null);
|
||||
const [selectedPropertyTypeLabel, setSelectedPropertyTypeLabel] = useState<string | null>(null);
|
||||
|
||||
const handleSelectedGroupChange = useCallback((groupId: number | null, groupLabel: string | null) => {
|
||||
setSelectedGroupId(groupId);
|
||||
setSelectedGroupLabel(groupLabel);
|
||||
}, []);
|
||||
|
||||
const handleSelectedPropertyTypeChange = useCallback((propertyTypeId: number | null, propertyTypeLabel: string | null) => {
|
||||
setSelectedPropertyTypeId(propertyTypeId);
|
||||
setSelectedPropertyTypeLabel(propertyTypeLabel);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Box component="section" sx={{ p: 2, border: '1px dashed grey' }}>
|
||||
<Typography variant="body1" gutterBottom>
|
||||
Property Type Management
|
||||
</Typography>
|
||||
<Box
|
||||
sx={{
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
width: '100%',
|
||||
minHeight: 500,
|
||||
bgcolor: '#f5f5f5',
|
||||
}}
|
||||
>
|
||||
<PropertyTypeGroupTree selectedGroupId={selectedGroupId} onSelectedGroupChange={handleSelectedGroupChange} />
|
||||
<PropertyTypeGrid
|
||||
groupId={selectedGroupId}
|
||||
groupLabel={selectedGroupLabel}
|
||||
selectedPropertyTypeId={selectedPropertyTypeId}
|
||||
onSelectedPropertyTypeChange={handleSelectedPropertyTypeChange}
|
||||
/>
|
||||
<ValueGrid propertyTypeId={selectedPropertyTypeId} propertyTypeLabel={selectedPropertyTypeLabel} />
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default Properties;
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import Box from '@mui/material/Box';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import IconButton from '@mui/material/IconButton';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import InputAdornment from '@mui/material/InputAdornment';
|
||||
import AddIcon from '@mui/icons-material/Add';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import SearchIcon from '@mui/icons-material/Search';
|
||||
import { DataGrid } from '@mui/x-data-grid';
|
||||
import type { GridColDef, GridRowSelectionModel } from '@mui/x-data-grid';
|
||||
|
||||
import type { PropertyType } from '../../data/propertyType';
|
||||
import {
|
||||
createPropertyType,
|
||||
deletePropertyType,
|
||||
getPropertyTypesByGroupId,
|
||||
} from '../../api/propertyType.api';
|
||||
import EntityFormDialog, { type EntityFormValues } from './EntityFormDialog';
|
||||
|
||||
const EMPTY_SELECTION: GridRowSelectionModel = { type: 'include', ids: new Set() };
|
||||
|
||||
function toSelectionModel(id: number | null): GridRowSelectionModel {
|
||||
return id == null ? EMPTY_SELECTION : { type: 'include', ids: new Set([id]) };
|
||||
}
|
||||
|
||||
function fromSelectionModel(model: GridRowSelectionModel): number | null {
|
||||
const [id] = Array.from(model.ids);
|
||||
return id == null ? null : Number(id);
|
||||
}
|
||||
|
||||
const columns: GridColDef<PropertyType>[] = [
|
||||
{ field: 'id', headerName: 'ID', width: 80 },
|
||||
{ field: 'label', headerName: 'Label', flex: 1 },
|
||||
{ field: 'description', headerName: 'Description', flex: 2 },
|
||||
];
|
||||
|
||||
type PropertyTypeGridProps = {
|
||||
groupId: number | null;
|
||||
groupLabel: string | null;
|
||||
selectedPropertyTypeId: number | null;
|
||||
onSelectedPropertyTypeChange: (propertyTypeId: number | null, propertyTypeLabel: string | null) => void;
|
||||
};
|
||||
|
||||
const PropertyTypeGrid = ({
|
||||
groupId,
|
||||
groupLabel,
|
||||
selectedPropertyTypeId,
|
||||
onSelectedPropertyTypeChange,
|
||||
}: PropertyTypeGridProps) => {
|
||||
const [propertyTypes, setPropertyTypes] = useState<PropertyType[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [search, setSearch] = useState('');
|
||||
const [dialogOpen, setDialogOpen] = useState(false);
|
||||
|
||||
const reload = useCallback(async () => {
|
||||
if (groupId == null) {
|
||||
setPropertyTypes([]);
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
setPropertyTypes(await getPropertyTypesByGroupId(groupId));
|
||||
} catch {
|
||||
setError('Failed to load property types.');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [groupId]);
|
||||
|
||||
useEffect(() => {
|
||||
onSelectedPropertyTypeChange(null, null);
|
||||
reload();
|
||||
}, [groupId, reload, onSelectedPropertyTypeChange]);
|
||||
|
||||
const filteredRows = useMemo(() => {
|
||||
const normalizedQuery = search.trim().toLowerCase();
|
||||
if (!normalizedQuery) return propertyTypes;
|
||||
return propertyTypes.filter(
|
||||
(propertyType) =>
|
||||
propertyType.label.toLowerCase().includes(normalizedQuery) ||
|
||||
propertyType.description.toLowerCase().includes(normalizedQuery),
|
||||
);
|
||||
}, [propertyTypes, search]);
|
||||
|
||||
const handleAddPropertyType = async ({ label, description }: EntityFormValues) => {
|
||||
if (groupId == null) return;
|
||||
await createPropertyType({ id: 0, label, description, propertyGroupId: groupId });
|
||||
await reload();
|
||||
};
|
||||
|
||||
const handleRemovePropertyType = async () => {
|
||||
if (selectedPropertyTypeId == null) return;
|
||||
if (!window.confirm('Remove selected property type?')) return;
|
||||
await deletePropertyType(selectedPropertyTypeId);
|
||||
onSelectedPropertyTypeChange(null, null);
|
||||
await reload();
|
||||
};
|
||||
|
||||
return (
|
||||
<Stack sx={{ flex: 1, minWidth: 320, p: 1 }} spacing={1}>
|
||||
<Typography variant="subtitle1">Property Types{groupLabel ? ` - ${groupLabel}` : ''}</Typography>
|
||||
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
|
||||
<TextField
|
||||
size="small"
|
||||
placeholder="Search property types"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
fullWidth
|
||||
disabled={groupId == null}
|
||||
slotProps={{
|
||||
input: {
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<SearchIcon fontSize="small" />
|
||||
</InputAdornment>
|
||||
),
|
||||
},
|
||||
}}
|
||||
/>
|
||||
<Tooltip title="Add property type">
|
||||
<span>
|
||||
<IconButton size="small" onClick={() => setDialogOpen(true)} disabled={groupId == null}>
|
||||
<AddIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
<Tooltip title="Remove selected property type">
|
||||
<span>
|
||||
<IconButton size="small" onClick={handleRemovePropertyType} disabled={selectedPropertyTypeId == null}>
|
||||
<DeleteIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
</Stack>
|
||||
{error && (
|
||||
<Typography color="error" variant="body2">
|
||||
{error}
|
||||
</Typography>
|
||||
)}
|
||||
{groupId == null ? (
|
||||
<Typography variant="body2" sx={{ p: 2 }}>
|
||||
Select a property type group to view its property types.
|
||||
</Typography>
|
||||
) : (
|
||||
<Box sx={{ flexGrow: 1, minHeight: 300 }}>
|
||||
<DataGrid
|
||||
rows={filteredRows}
|
||||
columns={columns}
|
||||
loading={loading}
|
||||
density="compact"
|
||||
rowSelectionModel={toSelectionModel(selectedPropertyTypeId)}
|
||||
onRowSelectionModelChange={(model) => {
|
||||
const id = fromSelectionModel(model);
|
||||
const label = id == null ? null : (propertyTypes.find((p) => p.id === id)?.label ?? null);
|
||||
onSelectedPropertyTypeChange(id, label);
|
||||
}}
|
||||
disableMultipleRowSelection
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
<EntityFormDialog
|
||||
open={dialogOpen}
|
||||
title="Add Property Type"
|
||||
onClose={() => setDialogOpen(false)}
|
||||
onSubmit={handleAddPropertyType}
|
||||
/>
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
|
||||
export default PropertyTypeGrid;
|
||||
@@ -0,0 +1,192 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import type { SyntheticEvent } from 'react';
|
||||
import Box from '@mui/material/Box';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import IconButton from '@mui/material/IconButton';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import InputAdornment from '@mui/material/InputAdornment';
|
||||
import AddIcon from '@mui/icons-material/Add';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import SearchIcon from '@mui/icons-material/Search';
|
||||
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';
|
||||
|
||||
import {
|
||||
createPropertyTypeGroup,
|
||||
deletePropertyTypeGroup,
|
||||
getPropertyTypeGroupsByParentId,
|
||||
} from '../../api/propertyTypeGroup.api';
|
||||
import EntityFormDialog, { type EntityFormValues } from './EntityFormDialog';
|
||||
|
||||
const ROOT_PARENT_GROUP_ID = 0;
|
||||
|
||||
export type GroupTreeItem = {
|
||||
id: string;
|
||||
label: string;
|
||||
children?: GroupTreeItem[];
|
||||
};
|
||||
|
||||
async function buildGroupTree(parentId: number): Promise<GroupTreeItem[]> {
|
||||
// the backend returns null instead of [] when a group has no children
|
||||
const groups = (await getPropertyTypeGroupsByParentId(parentId)) ?? [];
|
||||
return Promise.all(
|
||||
groups.map(async (group) => {
|
||||
const children = await buildGroupTree(group.id);
|
||||
return {
|
||||
id: String(group.id),
|
||||
label: group.label,
|
||||
children: children.length > 0 ? children : undefined,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function filterGroupTree(items: GroupTreeItem[], query: string): GroupTreeItem[] {
|
||||
const normalizedQuery = query.trim().toLowerCase();
|
||||
if (!normalizedQuery) return items;
|
||||
|
||||
const filterNodes = (nodes: GroupTreeItem[]): GroupTreeItem[] =>
|
||||
nodes.reduce<GroupTreeItem[]>((matches, node) => {
|
||||
const filteredChildren = node.children ? filterNodes(node.children) : [];
|
||||
const selfMatches = node.label.toLowerCase().includes(normalizedQuery);
|
||||
if (selfMatches || filteredChildren.length > 0) {
|
||||
matches.push({ ...node, children: selfMatches ? node.children : filteredChildren });
|
||||
}
|
||||
return matches;
|
||||
}, []);
|
||||
|
||||
return filterNodes(items);
|
||||
}
|
||||
|
||||
function collectIds(items: GroupTreeItem[]): string[] {
|
||||
return items.flatMap((item) => [item.id, ...(item.children ? collectIds(item.children) : [])]);
|
||||
}
|
||||
|
||||
function findLabel(items: GroupTreeItem[], id: string): string | null {
|
||||
for (const item of items) {
|
||||
if (item.id === id) return item.label;
|
||||
const childLabel = item.children ? findLabel(item.children, id) : null;
|
||||
if (childLabel != null) return childLabel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
type PropertyTypeGroupTreeProps = {
|
||||
selectedGroupId: number | null;
|
||||
onSelectedGroupChange: (groupId: number | null, groupLabel: string | null) => void;
|
||||
};
|
||||
|
||||
const PropertyTypeGroupTree = ({ selectedGroupId, onSelectedGroupChange }: PropertyTypeGroupTreeProps) => {
|
||||
const [tree, setTree] = useState<GroupTreeItem[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [search, setSearch] = useState('');
|
||||
const [expandedItems, setExpandedItems] = useState<string[]>([]);
|
||||
const [dialogOpen, setDialogOpen] = useState(false);
|
||||
|
||||
const reloadTree = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
setTree(await buildGroupTree(ROOT_PARENT_GROUP_ID));
|
||||
} catch {
|
||||
setError('Failed to load property type groups.');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
reloadTree();
|
||||
}, [reloadTree]);
|
||||
|
||||
const filteredTree = useMemo(() => filterGroupTree(tree, search), [tree, search]);
|
||||
|
||||
useEffect(() => {
|
||||
if (search.trim()) {
|
||||
setExpandedItems(collectIds(filteredTree));
|
||||
}
|
||||
}, [search, filteredTree]);
|
||||
|
||||
const handleSelectedItemsChange = (_event: SyntheticEvent | null, itemId: string | null) => {
|
||||
onSelectedGroupChange(itemId ? Number(itemId) : null, itemId ? findLabel(tree, itemId) : null);
|
||||
};
|
||||
|
||||
const handleAddGroup = async ({ label, description }: EntityFormValues) => {
|
||||
await createPropertyTypeGroup({
|
||||
id: 0,
|
||||
label,
|
||||
description,
|
||||
parentGroupId: selectedGroupId ?? ROOT_PARENT_GROUP_ID,
|
||||
});
|
||||
await reloadTree();
|
||||
};
|
||||
|
||||
const handleRemoveGroup = async () => {
|
||||
if (selectedGroupId == null) return;
|
||||
if (!window.confirm('Remove selected property type group?')) return;
|
||||
await deletePropertyTypeGroup(selectedGroupId);
|
||||
onSelectedGroupChange(null, null);
|
||||
await reloadTree();
|
||||
};
|
||||
|
||||
return (
|
||||
<Stack sx={{ width: 320, minWidth: 280, borderRight: '1px solid #e0e0e0', p: 1 }} spacing={1}>
|
||||
<Typography variant="subtitle1">Property Type Groups</Typography>
|
||||
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
|
||||
<TextField
|
||||
size="small"
|
||||
placeholder="Search groups"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
fullWidth
|
||||
slotProps={{
|
||||
input: {
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<SearchIcon fontSize="small" />
|
||||
</InputAdornment>
|
||||
),
|
||||
},
|
||||
}}
|
||||
/>
|
||||
<Tooltip title="Add group">
|
||||
<IconButton size="small" onClick={() => setDialogOpen(true)}>
|
||||
<AddIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
<Tooltip title="Remove selected group">
|
||||
<span>
|
||||
<IconButton size="small" onClick={handleRemoveGroup} disabled={selectedGroupId == null}>
|
||||
<DeleteIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
</Stack>
|
||||
{error && (
|
||||
<Typography color="error" variant="body2">
|
||||
{error}
|
||||
</Typography>
|
||||
)}
|
||||
<Box sx={{ flexGrow: 1, overflowY: 'auto', minHeight: 300 }}>
|
||||
<RichTreeView
|
||||
items={filteredTree}
|
||||
selectedItems={selectedGroupId != null ? String(selectedGroupId) : null}
|
||||
onSelectedItemsChange={handleSelectedItemsChange}
|
||||
expandedItems={expandedItems}
|
||||
onExpandedItemsChange={(_event, itemIds) => setExpandedItems(itemIds)}
|
||||
/>
|
||||
</Box>
|
||||
{loading && <Typography variant="body2">Loading...</Typography>}
|
||||
<EntityFormDialog
|
||||
open={dialogOpen}
|
||||
title="Add Property Type Group"
|
||||
onClose={() => setDialogOpen(false)}
|
||||
onSubmit={handleAddGroup}
|
||||
/>
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
|
||||
export default PropertyTypeGroupTree;
|
||||
@@ -0,0 +1,166 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import Box from '@mui/material/Box';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import IconButton from '@mui/material/IconButton';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import InputAdornment from '@mui/material/InputAdornment';
|
||||
import AddIcon from '@mui/icons-material/Add';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import SearchIcon from '@mui/icons-material/Search';
|
||||
import { DataGrid } from '@mui/x-data-grid';
|
||||
import type { GridColDef, GridRowSelectionModel } from '@mui/x-data-grid';
|
||||
|
||||
import type { Value } from '../../data/value';
|
||||
import {
|
||||
createValueForPropertyTypeId,
|
||||
deleteValueFromPropertyId,
|
||||
getValuesFromPropertyTypeId,
|
||||
} from '../../api/value.api';
|
||||
import EntityFormDialog, { type EntityFormValues } from './EntityFormDialog';
|
||||
|
||||
const EMPTY_SELECTION: GridRowSelectionModel = { type: 'include', ids: new Set() };
|
||||
|
||||
function toSelectionModel(id: number | null): GridRowSelectionModel {
|
||||
return id == null ? EMPTY_SELECTION : { type: 'include', ids: new Set([id]) };
|
||||
}
|
||||
|
||||
function fromSelectionModel(model: GridRowSelectionModel): number | null {
|
||||
const [id] = Array.from(model.ids);
|
||||
return id == null ? null : Number(id);
|
||||
}
|
||||
|
||||
const columns: GridColDef<Value>[] = [
|
||||
{ field: 'id', headerName: 'ID', width: 80 },
|
||||
{ field: 'label', headerName: 'Label', flex: 1 },
|
||||
{ field: 'description', headerName: 'Description', flex: 2 },
|
||||
];
|
||||
|
||||
type ValueGridProps = {
|
||||
propertyTypeId: number | null;
|
||||
propertyTypeLabel: string | null;
|
||||
};
|
||||
|
||||
const ValueGrid = ({ propertyTypeId, propertyTypeLabel }: ValueGridProps) => {
|
||||
const [values, setValues] = useState<Value[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [search, setSearch] = useState('');
|
||||
const [selectedValueId, setSelectedValueId] = useState<number | null>(null);
|
||||
const [dialogOpen, setDialogOpen] = useState(false);
|
||||
|
||||
const reload = useCallback(async () => {
|
||||
if (propertyTypeId == null) {
|
||||
setValues([]);
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
setValues(await getValuesFromPropertyTypeId(propertyTypeId));
|
||||
} catch {
|
||||
setError('Failed to load values.');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [propertyTypeId]);
|
||||
|
||||
useEffect(() => {
|
||||
setSelectedValueId(null);
|
||||
reload();
|
||||
}, [propertyTypeId, reload]);
|
||||
|
||||
const filteredRows = useMemo(() => {
|
||||
const normalizedQuery = search.trim().toLowerCase();
|
||||
if (!normalizedQuery) return values;
|
||||
return values.filter(
|
||||
(value) =>
|
||||
value.label.toLowerCase().includes(normalizedQuery) ||
|
||||
value.description.toLowerCase().includes(normalizedQuery),
|
||||
);
|
||||
}, [values, search]);
|
||||
|
||||
const handleAddValue = async ({ label, description }: EntityFormValues) => {
|
||||
if (propertyTypeId == null) return;
|
||||
await createValueForPropertyTypeId(propertyTypeId, { id: 0, label, description, propertyId: propertyTypeId });
|
||||
await reload();
|
||||
};
|
||||
|
||||
const handleRemoveValue = async () => {
|
||||
if (propertyTypeId == null || selectedValueId == null) return;
|
||||
if (!window.confirm('Remove selected value?')) return;
|
||||
await deleteValueFromPropertyId(selectedValueId, propertyTypeId);
|
||||
setSelectedValueId(null);
|
||||
await reload();
|
||||
};
|
||||
|
||||
return (
|
||||
<Stack sx={{ flex: 1, minWidth: 320, p: 1 }} spacing={1}>
|
||||
<Typography variant="subtitle1">Values{propertyTypeLabel ? ` - ${propertyTypeLabel}` : ''}</Typography>
|
||||
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
|
||||
<TextField
|
||||
size="small"
|
||||
placeholder="Search values"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
fullWidth
|
||||
disabled={propertyTypeId == null}
|
||||
slotProps={{
|
||||
input: {
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<SearchIcon fontSize="small" />
|
||||
</InputAdornment>
|
||||
),
|
||||
},
|
||||
}}
|
||||
/>
|
||||
<Tooltip title="Add value">
|
||||
<span>
|
||||
<IconButton size="small" onClick={() => setDialogOpen(true)} disabled={propertyTypeId == null}>
|
||||
<AddIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
<Tooltip title="Remove selected value">
|
||||
<span>
|
||||
<IconButton size="small" onClick={handleRemoveValue} disabled={selectedValueId == null}>
|
||||
<DeleteIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
</Stack>
|
||||
{error && (
|
||||
<Typography color="error" variant="body2">
|
||||
{error}
|
||||
</Typography>
|
||||
)}
|
||||
{propertyTypeId == null ? (
|
||||
<Typography variant="body2" sx={{ p: 2 }}>
|
||||
Select a property type to view its values.
|
||||
</Typography>
|
||||
) : (
|
||||
<Box sx={{ flexGrow: 1, minHeight: 300 }}>
|
||||
<DataGrid
|
||||
rows={filteredRows}
|
||||
columns={columns}
|
||||
loading={loading}
|
||||
density="compact"
|
||||
rowSelectionModel={toSelectionModel(selectedValueId)}
|
||||
onRowSelectionModelChange={(model) => setSelectedValueId(fromSelectionModel(model))}
|
||||
disableMultipleRowSelection
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
<EntityFormDialog
|
||||
open={dialogOpen}
|
||||
title="Add Value"
|
||||
onClose={() => setDialogOpen(false)}
|
||||
onSubmit={handleAddValue}
|
||||
/>
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
|
||||
export default ValueGrid;
|
||||
@@ -6,7 +6,7 @@ import './index.css'
|
||||
import App from './App.tsx'
|
||||
import keycloak from './keycloak.ts';
|
||||
|
||||
keycloak.init({ onLoad: 'login-required' }).then(() => {
|
||||
keycloak.init({ onLoad: 'login-required' }).then(() => {
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
|
||||
@@ -11,7 +11,7 @@ import Properties from '../components/properties/Properties';
|
||||
const HomePage: React.FC = () => {
|
||||
const [value, setValue] = React.useState('1');
|
||||
|
||||
const handleChange = (event: React.SyntheticEvent, newValue: string) => {
|
||||
const handleChange = (_event: React.SyntheticEvent, newValue: string) => {
|
||||
setValue(newValue);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user