feat(ui): Verbesserung von CreateFilterModal mit Validierung und dynamischer Filtererstellung
- Validierung für `name` und `type` vor der Filtererstellung hinzugefügt - Verbinden der Felder `label`, `name` und `type` mit dem Formularstatus - Hardcodierte Werte im Aufruf `createFiltersAsync` durch Benutzereingaben ersetzt - Einführung der Funktion `tryCreateFilter` für eine bessere Struktur - Falsche Verwendung von `Name` und `Label` behoben
This commit is contained in:
@@ -13,7 +13,7 @@ export const filterTypes: Type[] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
export type FilterCreateDto = {
|
export type FilterCreateDto = {
|
||||||
label?: string;
|
label?: string | undefined;
|
||||||
name: string;
|
name: string;
|
||||||
type: Type;
|
type: Type;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
import Modal from '@mui/material/Modal';
|
import Modal from '@mui/material/Modal';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
@@ -7,7 +9,6 @@ import Autocomplete from '@mui/material/Autocomplete';
|
|||||||
import { Iconify } from 'src/components/iconify/iconify';
|
import { Iconify } from 'src/components/iconify/iconify';
|
||||||
|
|
||||||
import { createFiltersAsync, FilterCreateDto, filterTypes, Type } from '../../../api/filter-service';
|
import { createFiltersAsync, FilterCreateDto, filterTypes, Type } from '../../../api/filter-service';
|
||||||
import { useState } from 'react';
|
|
||||||
|
|
||||||
const style = {
|
const style = {
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
@@ -26,8 +27,21 @@ type ModalProps = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function CreateFilterModal({ open, handleClose }: ModalProps) {
|
export default function CreateFilterModal({ open, handleClose }: ModalProps) {
|
||||||
const [name, setName] = useState<string>('');
|
const [name, setName] = useState<string | undefined>(undefined);
|
||||||
const [label, setLabel] = useState<string>('');
|
const [label, setLabel] = useState<string | undefined>(undefined);
|
||||||
|
const [selectedType, setSelectedType] = useState<Type | undefined>(undefined);
|
||||||
|
|
||||||
|
async function tryCreateFilter(): Promise<any> {
|
||||||
|
if (!name) {
|
||||||
|
alert('No name.');
|
||||||
|
}
|
||||||
|
else if (!selectedType) {
|
||||||
|
alert('No type.');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await createFiltersAsync({ name: name, type: selectedType, label: label }).then(() => handleClose());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
@@ -43,13 +57,15 @@ export default function CreateFilterModal({ open, handleClose }: ModalProps) {
|
|||||||
<Autocomplete
|
<Autocomplete
|
||||||
disablePortal
|
disablePortal
|
||||||
options={filterTypes}
|
options={filterTypes}
|
||||||
|
value={selectedType}
|
||||||
|
onChange={(event, newValue) => setSelectedType(newValue ?? undefined)}
|
||||||
renderInput={params => <TextField {...params} label="Type" variant="filled" />}
|
renderInput={params => <TextField {...params} label="Type" variant="filled" />}
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="inherit"
|
color="inherit"
|
||||||
startIcon={<Iconify icon="mingcute:add-line" />}
|
startIcon={<Iconify icon="mingcute:add-line" />}
|
||||||
onClick={() => createFiltersAsync({ name: '', type: 'VARCHAR' })}
|
onClick={() => tryCreateFilter()}
|
||||||
>
|
>
|
||||||
Add filter
|
Add filter
|
||||||
</Button>
|
</Button>
|
||||||
@@ -58,4 +74,3 @@ export default function CreateFilterModal({ open, handleClose }: ModalProps) {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user