import Box from "@mui/material/Box"; import Toolbar from "@mui/material/Toolbar"; import Typography from "@mui/material/Typography"; import * as React from "react"; import {useEffect, useState} from "react"; import Paper from "@mui/material/Paper"; import {Autocomplete, FormControl, FormControlLabel, InputLabel} from "@mui/material"; import {api} from "../../../lib/clients/api"; import {requests} from "../../../requests"; import {useAlert} from "../../../hooks/useAlert"; import {useSearchParams} from "react-router-dom"; import TextField from "@mui/material/TextField"; import Switch from "@mui/material/Switch"; import Stack from "@mui/material/Stack"; import Button from "@mui/material/Button"; import Select from "@mui/material/Select"; import MenuItem from "@mui/material/MenuItem"; import {getComparator} from "../../../components/core/getComparator"; const emptyIngredient = { id: null, name: "", enName: "", have: false, image: null, type: "", alcohol: false, abv: null, description: null } export function EditIngredientPage() { const [searchParams] = useSearchParams(); const [ingredients, setIngredients] = useState([]); const [types, setTypes] = useState([]); const [ingredient, setIngredient] = useState(emptyIngredient) const {createError, createSuccess} = useAlert(); useEffect(() => { api().get(requests.bar.ingredientList) .then((r) => { const arr = r.data.sort(getComparator("asc", "name")); setIngredients(arr) const currentId = searchParams.get("id"); if (!currentId) { return; } const currentIngredient = arr.find((r) => r.id === (currentId * 1)); if (!currentIngredient) { return; } setIngredient(currentIngredient); }) .catch(() => createError("Ошибка получения данных")) api().get(requests.bar.type) .then((r) => setTypes(r.data.sort(getComparator("asc", "name")))) // eslint-disable-next-line }, []); const changeIngredientValue = (name, value) => { setIngredient((prev) => ({ ...prev, [name]: value })) } const saveIngredientHandler = () => { api().patch(requests.bar.ingredient, ingredient) .then(() => createSuccess("Ингредиент сохранен")) .catch(() => createError("Ошибка сохранения")) } return ( {/*Заголовок*/} Ингредиенты {/*Поиск*/} { return !v ? setIngredient(emptyIngredient) : setIngredient(v) }} isOptionEqualToValue={(selected, value) => selected.id === value.id} getOptionKey={(selected) => selected.id} getOptionLabel={(selected) => selected.name} renderInput={(params) => } /> {/*Форма ингредиента*/} changeIngredientValue("have", !ingredient.have)} />} label={"Наличие"} labelPlacement={'start'}/> {""} changeIngredientValue("name", e.target.value)}/> changeIngredientValue("enName", e.target.value)}/> changeIngredientValue("alcohol", !ingredient.alcohol)} />} label="Алкогольный"/> {ingredient.alcohol && ( changeIngredientValue("abv", e.target.value)}/> )} Категория changeIngredientValue("description", e.target.value)} value={!ingredient.description ? "" : ingredient.description}/> ) }