import { useMemo, useState } from "react"; import { Alert, Box, Button, Chip, CircularProgress, FormControl, InputLabel, MenuItem, Paper, Select, Stack, TextField, Typography, } from "@mui/material"; import { SettingsNav } from "@/components/common/settings-nav"; import { DROPDOWN_OPTION_CATEGORIES, type DropdownOption, type DropdownOptionCategory, } from "@/domain/settings/dropdown-options/types/dropdown-option"; import { useCreateDropdownOption } from "@/domain/settings/dropdown-options/use-cases/use-create-dropdown-option"; import { useDeleteDropdownOption } from "@/domain/settings/dropdown-options/use-cases/use-delete-dropdown-option"; import { useDropdownOptionsByCategory } from "@/domain/settings/dropdown-options/use-cases/use-dropdown-options-by-category"; import { useUpdateDropdownOption } from "@/domain/settings/dropdown-options/use-cases/use-update-dropdown-option"; export default function DropdownOptionsPage() { const [activeCategory, setActiveCategory] = useState("Trade"); const [newValue, setNewValue] = useState(""); const [newParent, setNewParent] = useState(""); const [editingId, setEditingId] = useState(null); const [editValue, setEditValue] = useState(""); const categoryConfig = useMemo( () => DROPDOWN_OPTION_CATEGORIES.find((category) => category.key === activeCategory), [activeCategory], ); const parentCategory = categoryConfig && "parent" in categoryConfig ? categoryConfig.parent : undefined; const { data: options = [], isLoading, error } = useDropdownOptionsByCategory(activeCategory); const { data: parentOptions = [] } = useDropdownOptionsByCategory("Trade", { enabled: Boolean(parentCategory), }); const createMutation = useCreateDropdownOption(); const updateMutation = useUpdateDropdownOption(); const deleteMutation = useDeleteDropdownOption(); const resetFormState = () => { setNewValue(""); setNewParent(""); setEditingId(null); setEditValue(""); }; const handleCategoryChange = (category: DropdownOptionCategory) => { if (category !== activeCategory) { resetFormState(); } setActiveCategory(category); }; const handleAdd = () => { const value = newValue.trim(); if (!value) { return; } createMutation.mutate( { category: activeCategory, value, parentValue: parentCategory ? newParent || null : null, sortOrder: options.length, isActive: true, }, { onSuccess: () => { setNewValue(""); setNewParent(""); }, }, ); }; const handleEditSave = (item: DropdownOption) => { const value = editValue.trim(); if (!value) { return; } updateMutation.mutate( { id: item.id, category: item.category, value, parentValue: item.parentValue, sortOrder: item.sortOrder, isActive: item.isActive, }, { onSuccess: () => { setEditingId(null); }, }, ); }; const handleDelete = (id: string | number) => { deleteMutation.mutate(id); }; const addPlaceholder = `Add new ${categoryConfig?.label.slice(0, -1) ?? "option"}...`; const isMutating = createMutation.isPending || updateMutation.isPending || deleteMutation.isPending; return ( Dropdown Options Manage configurable dropdown values for work orders {DROPDOWN_OPTION_CATEGORIES.map((category) => ( ))} {Boolean(parentCategory) && ( Parent Trade )} setNewValue(event.target.value)} onKeyDown={(event) => { if (event.key === "Enter") { event.preventDefault(); handleAdd(); } }} placeholder={addPlaceholder} className="flex-1" /> {Boolean(error) && ( {error instanceof Error ? error.message : "Failed to load options"} )} {isLoading ? ( ) : options.length === 0 ? ( No options configured yet ) : ( {options.map((item) => ( {editingId === item.id ? ( setEditValue(event.target.value)} onKeyDown={(event) => { if (event.key === "Enter") { handleEditSave(item); } if (event.key === "Escape") { setEditingId(null); } }} autoFocus className="flex-1" /> ) : ( {item.value} {Boolean(item.parentValue) && ( )} )} {editingId === item.id ? ( <> ) : ( <> )} ))} )} ); }