|
@@ -1,5 +1,8 @@
|
1
|
|
-import React, { useState } from 'react';
|
2
|
|
-import { List, Popconfirm, Select } from 'antd';
|
|
1
|
+import React, { useEffect, useState, useRef } from 'react';
|
|
2
|
+import { Button, List, Popconfirm, Select } from 'antd';
|
|
3
|
+import classNames from 'classnames';
|
|
4
|
+import { getPackageDetailList, addPackageDetail, deletePackageDetail } from '@/services/api/package';
|
|
5
|
+import { getDishList } from '@/services/api/dish';
|
3
|
6
|
|
4
|
7
|
const { Option } = Select;
|
5
|
8
|
|
|
@@ -10,7 +13,9 @@ const Header = props => {
|
10
|
13
|
|
11
|
14
|
const handleSearch = (newValue) => {
|
12
|
15
|
if (newValue) {
|
13
|
|
- // fetch(newValue, setData);
|
|
16
|
+ getDishList({ name: newValue, pageNum: 1, pageSize: 100 }).then(res => {
|
|
17
|
+ setData(res.records || []);
|
|
18
|
+ });
|
14
|
19
|
} else {
|
15
|
20
|
setData([]);
|
16
|
21
|
}
|
|
@@ -25,9 +30,20 @@ const Header = props => {
|
25
|
30
|
|
26
|
31
|
const options = data.map(d => <Option key={d.id} value={d.id}>{d.name}</Option>);
|
27
|
32
|
|
|
33
|
+ const initList = () => {
|
|
34
|
+ getDishList({ pageNum: 1, pageSize: 10 }).then(res => {
|
|
35
|
+ setData(res.records || []);
|
|
36
|
+ });
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ useEffect(() => {
|
|
40
|
+ initList();
|
|
41
|
+ }, [])
|
|
42
|
+
|
28
|
43
|
return (
|
29
|
44
|
<Select
|
30
|
45
|
style={{ width: '100%' }}
|
|
46
|
+ allowClear
|
31
|
47
|
showSearch
|
32
|
48
|
value={value}
|
33
|
49
|
placeholder='请选择菜肴'
|
|
@@ -37,6 +53,7 @@ const Header = props => {
|
37
|
53
|
onSearch={handleSearch}
|
38
|
54
|
onChange={handleChange}
|
39
|
55
|
notFoundContent={null}
|
|
56
|
+ onClear={initList}
|
40
|
57
|
>
|
41
|
58
|
{options}
|
42
|
59
|
</Select>
|
|
@@ -45,28 +62,66 @@ const Header = props => {
|
45
|
62
|
|
46
|
63
|
|
47
|
64
|
export default (props) => {
|
|
65
|
+ const { current, setCurrent } = props;
|
|
66
|
+
|
|
67
|
+ const packageId = (current || {}).id;
|
|
68
|
+
|
|
69
|
+ const [loading, setLoading] = useState(false);
|
48
|
70
|
const [list, setList] = useState([]);
|
|
71
|
+ const [detail, setDetail] = useState({});
|
|
72
|
+ const listRef = useRef();
|
|
73
|
+ listRef.current = list;
|
49
|
74
|
|
50
|
75
|
const onAdd = item => {
|
51
|
|
- const origin = list.filter(x => x.id !== item.id)[0];
|
52
|
|
- if (!origin) {
|
53
|
|
- setList([...list, item]);
|
54
|
|
- }
|
|
76
|
+ setLoading(true);
|
|
77
|
+ addPackageDetail({ packageId, dishId: item.id }).then(res => {
|
|
78
|
+ setLoading(false);
|
|
79
|
+ const newList = listRef.current.concat(res);
|
|
80
|
+ setList(newList);
|
|
81
|
+ }).catch(() => {
|
|
82
|
+ setLoading(false);
|
|
83
|
+ });
|
55
|
84
|
}
|
56
|
85
|
|
|
86
|
+ const onDelete = item => {
|
|
87
|
+ setLoading(true);
|
|
88
|
+ deletePackageDetail({ packageId, dishId: item.id }).then(res => {
|
|
89
|
+ setLoading(false);
|
|
90
|
+ const newList = listRef.current.filter(x => x.id !== item.id);
|
|
91
|
+ setList(newList);
|
|
92
|
+ }).catch(() => {
|
|
93
|
+ setLoading(false);
|
|
94
|
+ });
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ useEffect(() => {
|
|
98
|
+ if (!packageId) return;
|
|
99
|
+
|
|
100
|
+ setLoading(true);
|
|
101
|
+ getPackageDetailList({ packageId, pageNum: 1, pageSize: 500 }).then(res => {
|
|
102
|
+ setLoading(false);
|
|
103
|
+ setList(res.records || []);
|
|
104
|
+ setDetail((res.records || [])[0] || {});
|
|
105
|
+ }).catch(() => {
|
|
106
|
+ setLoading(false);
|
|
107
|
+ });
|
|
108
|
+
|
|
109
|
+ }, [packageId]);
|
|
110
|
+
|
57
|
111
|
return (
|
58
|
112
|
<List
|
|
113
|
+ style={{ minHeight: '300px' }}
|
59
|
114
|
header={<Header onChange={onAdd} />}
|
60
|
115
|
bordered
|
|
116
|
+ loading={loading}
|
61
|
117
|
dataSource={list}
|
62
|
118
|
renderItem={item => (
|
63
|
119
|
<List.Item
|
64
|
|
- className={classNames({ active: current.id === item.id })}
|
65
|
|
- onClick={() => setCurrent(item)}
|
|
120
|
+ className={classNames({ active: detail.id === item.id })}
|
66
|
121
|
actions={[
|
67
|
122
|
<Popconfirm
|
68
|
123
|
key="delete"
|
69
|
|
- title="确认删除套餐?"
|
|
124
|
+ title="确认删除?"
|
70
|
125
|
onConfirm={() => onDelete(item)}
|
71
|
126
|
>
|
72
|
127
|
<a href="#">删除</a>
|