Переведены все запросы на клиенты

This commit is contained in:
Kayashov.SM
2025-08-25 18:27:19 +04:00
parent 6fe2ca6c57
commit 015416c43b
23 changed files with 518 additions and 564 deletions

View File

@@ -1,18 +1,18 @@
import Dialog from "@mui/material/Dialog";
import DialogTitle from "@mui/material/DialogTitle";
import * as React from "react";
import {useState} from "react";
import Typography from "@mui/material/Typography";
import DialogContent from "@mui/material/DialogContent";
import DialogActions from "@mui/material/DialogActions";
import Button from "@mui/material/Button";
import {useState} from "react";
import TextField from "@mui/material/TextField";
export function BarCreateModal({open, close, create}) {
export function BarCreateModal({open, setOpen, create}) {
const [value, setValue] = useState("");
return (
<Dialog fullWidth={true}
open={open} onClose={close}
open={open} onClose={() => setOpen(false)}
sx={{
'& .MuiDialog-paper': {
margin: '8px',

View File

@@ -4,17 +4,16 @@ import DialogContent from "@mui/material/DialogContent";
import DialogActions from "@mui/material/DialogActions";
import Button from "@mui/material/Button";
import * as React from "react";
import {useEffect, useState} from "react";
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import List from "@mui/material/List";
import {useEffect, useState} from "react";
import {api} from "../../lib/clients/api";
import {requests} from "../../requests";
import {useAlert} from "../../hooks/useAlert";
import ListItem from "@mui/material/ListItem";
import {useSelect} from "../../hooks/useSelect";
import {IngredientAlert} from "./IngredientAlert";
import {useUser} from "../../hooks/useUser";
import {cocktailClient} from "../../lib/clients/CocktailClient";
export function IngredientInfoModal({ingredient, handleDelete}) {
const {user} = useUser();
@@ -28,11 +27,7 @@ export function IngredientInfoModal({ingredient, handleDelete}) {
};
useEffect(() => {
if(!ingredient) {
return
}
api().get(requests.cocktails.byIngredient + ingredient.id)
.then((r) => setCocktails(r.data))
cocktailClient.getCocktailByIngredient(ingredient, setCocktails)
.catch(() => createError())
// eslint-disable-next-line
}, [ingredient]);
@@ -59,7 +54,7 @@ export function IngredientInfoModal({ingredient, handleDelete}) {
</Stack>
{cocktails.length > 0 && (
<>
<Typography sx={{ mt:2}}>Коктейли:</Typography>
<Typography sx={{mt: 2}}>Коктейли:</Typography>
<List>
{cocktails.map((c) => {
return (
@@ -69,7 +64,7 @@ export function IngredientInfoModal({ingredient, handleDelete}) {
}}>
<Stack direction={'row'}>
<img src={c.image} alt={c.name} loading={"eager"} width={"50"}/>
<Typography sx={{mx:1}}>{c.name}</Typography>
<Typography sx={{mx: 1}}>{c.name}</Typography>
{c.rating.rating > 0 && <Typography> {`${c.rating.rating}/5`}</Typography>}
</Stack>
</ListItem>
@@ -83,7 +78,8 @@ export function IngredientInfoModal({ingredient, handleDelete}) {
{user.role !== 'USER' && <Button onClick={() => setOpen(true)}>Удалить</Button>}
<Button onClick={closeIngredient}>Закрыть</Button>
</DialogActions>
<IngredientAlert handleDelete={handleDelete} handleClose={handleClose} open={open} id={ingredient.id} handleCloseParent={closeIngredient}/>
<IngredientAlert handleDelete={handleDelete} handleClose={handleClose} open={open} id={ingredient.id}
handleCloseParent={closeIngredient}/>
</Dialog>
);
}

View File

@@ -10,9 +10,8 @@ import Button from "@mui/material/Button";
import CircularProgress from "@mui/material/CircularProgress";
import Box from "@mui/material/Box";
import {red} from "@mui/material/colors";
import {requests} from "../../requests";
import {useAuth} from "../../hooks/useAuth";
import {api} from "../../lib/clients/api";
import {authClient} from "../../lib/clients/AuthClient";
const emptyRequest = {
byLogin: true,
@@ -38,21 +37,9 @@ export function SignInForm() {
}),
};
const handleButtonClick = async () => {
setLoading(true);
const response = await api().post(requests.auth.login, request);
if (response.data.error) {
setError(response.data.error);
setLoading(false);
return;
}
localStorage.setItem("token", response.data.token);
await checkSession?.();
window.location.reload();
await authClient.login(request, setLoading, setError, checkSession)
}
const renderByCode = () => {
return (
<Stack direction='row' mt={1}>

View File

@@ -1,40 +0,0 @@
import {Card} from "@mui/material";
import Stack from "@mui/material/Stack";
import Box from "@mui/material/Box";
import React from "react";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
const role = (myRole) => {
switch (myRole) {
case "ADMIN":
return "Администратор";
case "ADMIN_NOT_BARMEN":
return "Управляющий";
default:
return null;
}
}
export function BarItem({row, changeHandler, all, enterExist}) {
return (
<Card sx={{mb: 1, height: '100px', display: 'relative', p: 1}}>
<Stack direction='row' justifyContent='start' alignItems='start'>
<Box sx={{width: '75%', pr: 2}}>
<Typography variant='h6'>{row.name}</Typography>
<Typography>{role(row.myRole)}</Typography>
</Box>
<Stack sx={{width: '25%'}} spacing={1} justifyContent='flex-end' display='flex'>
<Typography color={row.open ? 'green' : 'red'}>{row.open ? "Бар открыт" : "Бар закрыт"}</Typography>
<Button variant='contained'
color={row.enter ? 'error' : 'success'}
onClick={() => changeHandler(row, !row.enter)}
disabled={!row.enter && enterExist}
>
{all ? "Добавить" : row.enter ? "Выйти" : "Войти"}
</Button>
</Stack>
</Stack>
</Card>
)
}

View File

@@ -1,70 +0,0 @@
import Box from "@mui/material/Box";
import {useEffect, useMemo, useState} from "react";
import {api} from "../../lib/clients/api";
import {requests} from "../../requests";
import {useAlert} from "../../hooks/useAlert";
import {BarItem} from "./BarItem";
import {Loading} from "../core/Loading";
import * as React from "react";
import {useUser} from "../../hooks/useUser";
export function BarList({all}) {
const {getError, createError} = useAlert();
const {refresh} = useUser();
const [bars, setBars] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
setLoading(true);
api().get(`${requests.bar.all}?my=${!all}`)
.then((r) => {
setBars(r.data)
setLoading(false);
})
.catch(() => getError())
// eslint-disable-next-line
}, []);
const enterExist = useMemo(() => bars.find((b) => b.enter), [bars])
const changeHandler = (row, value) => {
let request;
let newState;
if (!all) {
if (value && enterExist) {
//todo: добавить переключение
createError("Нельзя войти более чем в один бар одновременно")
return;
}
request = api().patch(`${requests.bar.enter}${row.id}&value=${value}`);
newState = bars.map((b) => {
if (b.id !== row.id) {
return b;
}
return {
...b,
enter: value
}
})
} else {
request = api().post(requests.bar.addToMyList, row);
newState = bars.filter((b) => b.id !== row.id);
}
request.then(() => {
setBars(newState)
refresh();
}).catch(() => getError())
}
return (
<Box mt={2}>
{
bars.map((row) => {
return (
<BarItem key={row.id} row={row} changeHandler={changeHandler}
all={all} enterExist={enterExist}/>
)
})
}
{/*Загрузчик*/}
<Loading loading={loading}/>
</Box>
)
}

View File

@@ -15,13 +15,13 @@ import IconButton from "@mui/material/IconButton";
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import DeleteIcon from '@mui/icons-material/Delete';
import {IngredientInfoModal} from "../Ingredients/IngredientInfoModal";
import {api} from "../../lib/clients/api";
import {requests} from "../../requests";
import {useAlert} from "../../hooks/useAlert";
import {paths} from "../../path";
import {Loading} from "../core/Loading";
import {useUser} from "../../hooks/useUser";
import {useSelect} from "../../hooks/useSelect";
import {cocktailClient} from "../../lib/clients/CocktailClient";
import {ingredientClient} from "../../lib/clients/IngredientClient";
export function CocktailInfoModal({row}) {
const {user} = useUser();
@@ -29,56 +29,8 @@ export function CocktailInfoModal({row}) {
const [cocktail, setCocktail] = useState(null)
const [loading, setLoading] = useState(false);
const {closeCocktail, selectIngredient, getIngredient, getOpenCocktail} = useSelect();
const openIngredientModalHandler = (id) => {
api().get(`${requests.bar.ingredient}?id=${id}`)
.then((r) => {
selectIngredient(r.data)
})
.catch(() => createError("Ошибка получения информации об ингредиенте"))
}
const selectIngredientHandler = (ingredient) => {
const url = `${requests.ingredient.crud}?id=${ingredient.id}`;
const request = ingredient.isHave ? api().delete(url) : api().put(url);
const value = !ingredient.isHave;
request.then(() => {
const newReceipts = cocktail.receipt.map((r) => {
if (r.ingredient.id !== ingredient.id) {
return r;
}
return {
...r,
ingredient: {
...ingredient,
isHave: value
}
}
})
setCocktail({
...cocktail,
receipt: newReceipts
})
createSuccess("Сохранено")
}).catch(() => createError("Ошибка сохранения"))
}
useEffect(() => {
setLoading(true)
if (!row) {
setLoading(false)
return;
}
api().get(requests.cocktails.modal + row)
.then((r) => {
setCocktail(r.data)
setLoading(false)
})
.catch(() => {
getError();
setLoading(false)
closeCocktail();
})
// eslint-disable-next-line
}, [row]);
useEffect(() => cocktailClient.getCocktailForModal(row, setLoading, setCocktail, closeCocktail, getError), [row]);
if (!row || !cocktail) {
return null;
@@ -134,7 +86,7 @@ export function CocktailInfoModal({row}) {
<Stack direction='row'>
{(user.role && user.role !== "USER") && (
<IconButton size="small" sx={{pb: "2px"}}
onClick={() => selectIngredientHandler(r.ingredient)}>
onClick={() => ingredientClient.changeIngredientInBar(r.ingredient, cocktail, setCocktail, createSuccess, createError)}>
{r.ingredient.isHave
? (<DeleteIcon fontSize="small"/>)
: (<ShoppingCartIcon fontSize="small"/>)
@@ -142,7 +94,7 @@ export function CocktailInfoModal({row}) {
</IconButton>
)}
<Typography
onClick={() => openIngredientModalHandler(r.ingredient.id)}>{r.ingredient.name}</Typography>
onClick={() => ingredientClient.findOne(r.ingredient.id, selectIngredient, createError)}>{r.ingredient.name}</Typography>
</Stack>
<Typography color={hasError && 'red'}>{measure}</Typography>
</Stack>

View File

@@ -6,13 +6,11 @@ import AddIcon from "@mui/icons-material/Add";
import * as React from "react";
import {useEffect, useState} from "react";
import {useAlert} from "../../hooks/useAlert";
import {api} from "../../lib/clients/api";
import {requests} from "../../requests";
import {getComparator} from "../core/getComparator";
import {Card} from "@mui/material";
import {SelectEdit} from "./SelectEdit";
import TextField from "@mui/material/TextField";
import DeleteForeverIcon from '@mui/icons-material/DeleteForever';
import {ingredientClient} from "../../lib/clients/IngredientClient";
export function EditCocktailReceipt({receipt, handler}) {
const {createError} = useAlert()
@@ -20,13 +18,8 @@ export function EditCocktailReceipt({receipt, handler}) {
const [units, setUnits] = useState([])
useEffect(() => {
api().get(requests.ingredient.all)
.then((r) => setIngredients(r.data.sort(getComparator("asc", "name"))))
.catch(() => createError("Ошибка получения списка ингредиентов"))
api().get(requests.unit)
.then((r) => setUnits(r.data.sort(getComparator("asc", "name"))))
.catch(() => createError("Ошибка получения единиц измерения"))
ingredientClient.findAll(setIngredients, createError)
ingredientClient.findUnit(setUnits, createError)
// eslint-disable-next-line
}, []);
@@ -153,7 +146,8 @@ export function EditCocktailReceipt({receipt, handler}) {
value={r.count}
onChange={(e) => countHandler(i, e.target.value)}
/>
<SelectEdit width={'calc(65% - 28px)'} array={units} value={!r.unit ? null : r.unit.name}
<SelectEdit width={'calc(65% - 28px)'} array={units}
value={!r.unit ? null : r.unit.name}
handler={unitHandler} label={"Ед."}
margin={1} attributeName={i}
/>

View File

@@ -10,9 +10,9 @@ import CheckMarks from "./CheckMarks";
import Button from "@mui/material/Button";
import * as React from "react";
import {useEffect, useState} from "react";
import {requests} from "../../requests";
import {useAlert} from "../../hooks/useAlert";
import {api} from "../../lib/clients/api";
import {categoryClient} from "../../lib/clients/CategoryClient";
import {glassClient} from "../../lib/clients/GlassClient";
export function FilterBlock({filter, handleFilterChange, handleClearFilter, barmen}) {
const {createError} = useAlert();
@@ -23,13 +23,8 @@ export function FilterBlock({filter, handleFilterChange, handleClearFilter, barm
const sort = ['Название по возрастанию', 'Название по убыванию'];
useEffect(() => {
api().get(requests.category.basic)
.then((r) => setCategory(r.data))
.catch(() => createError("Ошибка получения категорий"))
api().get(requests.glass.list)
.then((r) => setGlass(r.data))
.catch(() => createError("Ошибка получения посуды"))
categoryClient.getCategoryList(setCategory, createError)
glassClient.getGlassList(setGlass, createError)
// eslint-disable-next-line
}, []);