добавлен функционал копирования баров
This commit is contained in:
@@ -12,23 +12,34 @@ import Toolbar from "@mui/material/Toolbar";
|
||||
import AddCircleIcon from '@mui/icons-material/AddCircle';
|
||||
import {BarCreateModal} from "../../components/BarCreateModal";
|
||||
import PowerIcon from '@mui/icons-material/Power';
|
||||
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
|
||||
import {barClient} from "../../lib/clients/BarClient";
|
||||
|
||||
export function BarChangePage() {
|
||||
const [bars, setBars] = useState([])
|
||||
const [open, setOpen] = useState(false)
|
||||
const {createError, createSuccess, createWarning} = useAlert();
|
||||
const [oldId, setOldId] = useState(null);
|
||||
const {createError, createSuccess, createWarning, notImplement} = useAlert();
|
||||
|
||||
const createHandler = (name) => barClient.createBar(name, bars, createSuccess, createError, setBars, setOpen)
|
||||
const createHandler = (id, name) => {
|
||||
if (id) {
|
||||
barClient.copyBar(id, name, setBars, bars, createError, createSuccess, setOpen)
|
||||
} else {
|
||||
barClient.createBar(name, bars, createSuccess, createError, setBars, setOpen)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => barClient.getBarList(setBars, createError), []);
|
||||
|
||||
return (<>
|
||||
<BarCreateModal open={open} setOpen={setOpen} create={createHandler}/>
|
||||
<BarCreateModal open={open} setOpen={setOpen} create={createHandler} id={oldId}/>
|
||||
<Paper sx={{p: 1}}>
|
||||
<Toolbar>
|
||||
<Typography variant='h6'>Списки ингредиентов (бары)</Typography>
|
||||
<IconButton edge="end" onClick={() => setOpen(true)}>
|
||||
<IconButton edge="end" onClick={() => {
|
||||
setOldId(null);
|
||||
setOpen(true);
|
||||
}}>
|
||||
<AddCircleIcon/>
|
||||
</IconButton>
|
||||
</Toolbar>
|
||||
@@ -36,18 +47,27 @@ export function BarChangePage() {
|
||||
return <Card key={b.id} sx={{m: 2, p: 2}}>
|
||||
<Stack direction='row' justifyContent={'space-between'}>
|
||||
<Typography>{b.name}</Typography>
|
||||
<Box>
|
||||
<IconButton onClick={() => {
|
||||
setOldId(b.id)
|
||||
setOpen(true);
|
||||
}}>
|
||||
<ContentCopyIcon/>
|
||||
</IconButton>
|
||||
{b.active && <IconButton disabled>
|
||||
<PowerIcon/>
|
||||
</IconButton>}
|
||||
{!b.active && <Box>
|
||||
<IconButton onClick={() => barClient.deleteBar(b, bars, createError, createSuccess, setBars)}>
|
||||
{!b.active && <>
|
||||
<IconButton
|
||||
onClick={() => barClient.deleteBar(b, bars, createError, createSuccess, setBars)}>
|
||||
<DeleteIcon/>
|
||||
</IconButton>
|
||||
<IconButton
|
||||
onClick={() => barClient.changeBar(b.id, bars, createWarning, createSuccess, createError, setBars)}>
|
||||
<ElectricalServicesIcon/>
|
||||
</IconButton>
|
||||
</Box>}
|
||||
</>}
|
||||
</Box>
|
||||
</Stack>
|
||||
</Card>
|
||||
})}
|
||||
|
||||
@@ -8,7 +8,7 @@ import DialogActions from "@mui/material/DialogActions";
|
||||
import Button from "@mui/material/Button";
|
||||
import TextField from "@mui/material/TextField";
|
||||
|
||||
export function BarCreateModal({open, setOpen, create}) {
|
||||
export function BarCreateModal({open, setOpen, create, id}) {
|
||||
const [value, setValue] = useState("");
|
||||
return (
|
||||
<Dialog fullWidth={true}
|
||||
@@ -26,13 +26,17 @@ export function BarCreateModal({open, setOpen, create}) {
|
||||
</DialogTitle>
|
||||
<DialogContent>
|
||||
<TextField sx={{width: '75%'}}
|
||||
label={"Название списка"} variant='outlined'
|
||||
label={<Typography pt={'4px'}>
|
||||
Название списка</Typography>} variant='outlined'
|
||||
value={!value ? "" : value}
|
||||
onChange={(e) => setValue(e.target.value)}
|
||||
/>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => create(value)}>Создать</Button>
|
||||
<Button onClick={() => {
|
||||
create(id, value);
|
||||
setValue("");
|
||||
}}>Создать</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
)
|
||||
|
||||
@@ -58,6 +58,17 @@ class BarClient {
|
||||
setOpen(false)
|
||||
}).catch(() => createError("Ошибка создания списка"))
|
||||
}
|
||||
|
||||
copyBar(oldId, newName, setBars, bars, createError, createSuccess, setOpen) {
|
||||
api().post(requests.bar.crud + "copy/" + oldId + "/" + newName)
|
||||
.then((r) => {
|
||||
const state = bars;
|
||||
state.push(r.data)
|
||||
setBars(state);
|
||||
createSuccess("Бар скопирован")
|
||||
setOpen(false)
|
||||
}).catch(() => createError("Ошибка при копировании бара"))
|
||||
}
|
||||
}
|
||||
|
||||
export const barClient = new BarClient();
|
||||
Reference in New Issue
Block a user