1 | import React, {useEffect, useState} from 'react'
|
---|
2 | import {Button, Form, Input, Card, notification, Checkbox, message, Upload} from "antd";
|
---|
3 | import axios from "axios";
|
---|
4 | import env from "../env";
|
---|
5 | import {PlusOutlined} from "@ant-design/icons";
|
---|
6 |
|
---|
7 | const Menu = props => {
|
---|
8 |
|
---|
9 | const [loadingSave, setLoadingSave] = useState(false);
|
---|
10 | const [restaurant, setRestaurant] = useState({menu:[]});
|
---|
11 | const [loading, setLoading] = useState(true);
|
---|
12 |
|
---|
13 | useEffect(()=> {
|
---|
14 | setLoading(true);
|
---|
15 | getRestaurant()
|
---|
16 | },[])
|
---|
17 |
|
---|
18 | const getRestaurant = () => {
|
---|
19 | axios.get(env.api + 'Restaurants').then(res=>{
|
---|
20 | setRestaurant(res.data);
|
---|
21 | setLoadingSave(false);
|
---|
22 | setLoadingSave(false);
|
---|
23 | });
|
---|
24 | }
|
---|
25 |
|
---|
26 | const beforeUpload = (file) => {
|
---|
27 | const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
|
---|
28 | if (!isJpgOrPng) {
|
---|
29 | message.error('Сликата мора да е формат jpg или png');
|
---|
30 | }
|
---|
31 | if(isJpgOrPng) setLoadingSave(true);
|
---|
32 | return isJpgOrPng;
|
---|
33 | }
|
---|
34 | const uploadChanged = (ev) =>{
|
---|
35 | if(ev.file.status == 'done'){
|
---|
36 | getRestaurant()
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | const addMenu = (data) => {
|
---|
41 | setLoadingSave(true);
|
---|
42 | console.log(data)
|
---|
43 | axios.post(env.api + 'Menu/',{...data,price: parseInt(data.price)}, { headers: {Authorization: localStorage.getItem('Auth')}
|
---|
44 | }).then(res => {
|
---|
45 | notification['success']({
|
---|
46 | message: 'Успешно зачувано',
|
---|
47 | });
|
---|
48 | getRestaurant()
|
---|
49 | }).catch(er => {
|
---|
50 | notification['error']({
|
---|
51 | message: 'Се случи проблем при зачувување мени. Ве молиме пробајте повторно подоцна',
|
---|
52 | });
|
---|
53 | setLoadingSave(false);
|
---|
54 | console.log(er);
|
---|
55 | })
|
---|
56 | console.log(data)
|
---|
57 | }
|
---|
58 |
|
---|
59 | const deleteMenu = (id) => {
|
---|
60 | axios.delete(env.api + 'Menu/'+id, {headers: {Authorization: localStorage.getItem('Auth')}
|
---|
61 | }).then(res => {
|
---|
62 | notification['success']({
|
---|
63 | message: 'Успешно избришано',
|
---|
64 | });
|
---|
65 | getRestaurant()
|
---|
66 | }).catch(er => {
|
---|
67 | notification['error']({
|
---|
68 | message: 'Се случи проблем при бришење мени. Ве молиме пробајте повторно подоцна',
|
---|
69 | });
|
---|
70 | console.log(er);
|
---|
71 | })
|
---|
72 | }
|
---|
73 |
|
---|
74 | return(
|
---|
75 | <div style={{padding:'20px'}}>
|
---|
76 | <div style={{backgroundColor:'white'}}>
|
---|
77 | <div style={{
|
---|
78 | width: '100%',
|
---|
79 | height:'75px',
|
---|
80 | backgroundColor: 'white',
|
---|
81 | padding: '20px',
|
---|
82 | border: '1px solid lightgray'
|
---|
83 | }} >
|
---|
84 | <h2 style={{float: 'left'}}>Мени</h2>
|
---|
85 | </div>
|
---|
86 | <Form style={{padding:20}} onFinish={addMenu}>
|
---|
87 | <h2 style={{margin: '20px',marginTop: 0}}>Додади нов запис:</h2>
|
---|
88 | <Form.Item
|
---|
89 | label="Име"
|
---|
90 | name="title"
|
---|
91 |
|
---|
92 | rules={[
|
---|
93 | {
|
---|
94 | required: true,
|
---|
95 | message: 'Ве молиме внесете име!',
|
---|
96 | },
|
---|
97 | ]}
|
---|
98 | >
|
---|
99 | <Input/>
|
---|
100 | </Form.Item>
|
---|
101 | <Form.Item
|
---|
102 | label="Опис"
|
---|
103 | name="description"
|
---|
104 |
|
---|
105 | rules={[
|
---|
106 | {
|
---|
107 | required: true,
|
---|
108 | message: 'Ве молиме внесете опис!',
|
---|
109 | },
|
---|
110 | ]}
|
---|
111 | >
|
---|
112 | <Input/>
|
---|
113 | </Form.Item>
|
---|
114 | <Form.Item
|
---|
115 | label="Алергени"
|
---|
116 | name="alergens"
|
---|
117 | >
|
---|
118 | <Input/>
|
---|
119 | </Form.Item>
|
---|
120 | <Form.Item
|
---|
121 | label="Цена"
|
---|
122 | name="price"
|
---|
123 |
|
---|
124 | rules={[
|
---|
125 | {
|
---|
126 | required: true,
|
---|
127 | message: 'Ве молиме внесете цена!',
|
---|
128 | },
|
---|
129 | ]}
|
---|
130 | >
|
---|
131 | <Input placeholder="Цена" type={'number'}/>
|
---|
132 | </Form.Item>
|
---|
133 | <Form.Item
|
---|
134 | name="isVipOnly"
|
---|
135 | valuePropName="checked"
|
---|
136 | >
|
---|
137 | <Checkbox>Само за VIP</Checkbox>
|
---|
138 | </Form.Item>
|
---|
139 | <Form.Item style={{textAlign: 'center'}}>
|
---|
140 | <Button type="primary" htmlType="submit" loading={loadingSave}>
|
---|
141 | Зачувај
|
---|
142 | </Button>
|
---|
143 | </Form.Item>
|
---|
144 | </Form>
|
---|
145 | </div>
|
---|
146 | <div style={{textAlign:'start'}}>
|
---|
147 | {restaurant.menu.map(el =>
|
---|
148 | <Card key={el.id} title={el.title} extra={<div>{el.isVipOnly ? <span style={{color:'orange',fontWeight:'bold'}}>VIP </span>:''}<span style={{color:'red',cursor:'pointer'}} onClick={()=>deleteMenu(el.id)}>Избриши</span></div>} style={{ width: 300, display:'inline-block', margin:'10px' }} size="small">
|
---|
149 | <div style={{height:'100px', width:'270px'}}>
|
---|
150 | <Upload
|
---|
151 | name="file"
|
---|
152 | onChange={uploadChanged}
|
---|
153 | showUploadList={false}
|
---|
154 | beforeUpload={beforeUpload}
|
---|
155 | style={{height: '100px', overflow: 'hidden'}}
|
---|
156 | action={env.api+'Menu/'+el.id+'/upload'}
|
---|
157 | >
|
---|
158 | {el.image != "data:image/png;base64," ? <img style={{cursor: 'pointer'}} src={el.image} alt="avatar"
|
---|
159 | height='100px' width={'270px'}/> : <div style={{cursor:'pointer',margin: 15,border:'1px solid gray',padding: 5}}><PlusOutlined /><div style={{ marginTop: 8}}>Додади слика</div></div>}
|
---|
160 | </Upload>
|
---|
161 | </div>
|
---|
162 | <p>{el.description}</p>
|
---|
163 | <p>Алергени: {el.alergens.length ==0 ? 'Нема' : el.alergens}</p>
|
---|
164 | <b>{el.price} ден.</b>
|
---|
165 | </Card>
|
---|
166 | )}
|
---|
167 | </div>
|
---|
168 | </div>
|
---|
169 | )
|
---|
170 | }
|
---|
171 |
|
---|
172 | export default Menu; |
---|