DishList.jsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { Row, Col, Card, Button, notification } from 'antd';
  3. import EditableTag from '@/components/EditableTag';
  4. import { getDishList } from '@/services/api/dish';
  5. import { getPackageList, getPackageDetailList } from '@/services/api/package';
  6. import {
  7. updateGuaranteeTask,
  8. getGuaranteeDetailList,
  9. addGuaranteeDetailBatch,
  10. deleteGuaranteeDetail
  11. } from '@/services/api/guaranteeTask';
  12. import Selector from './components/Selector';
  13. export default (props) => {
  14. const { dataSource, setDataSource } = props;
  15. const [list, setList] = useState([]);
  16. const [loading, setLoading] = useState(false);
  17. const packageRef = useRef();
  18. const handlePackageSubmit = ({item, amount}) => {
  19. packageRef.current = item;
  20. // 获取套餐下所有的菜肴
  21. // 然后把菜肴放入到列表
  22. getPackageDetailList({ pageNum: 1, pageSize: 500, packageId: item.id }).then(res => {
  23. const { records = [] } = res;
  24. if (records.length > 0) {
  25. for (let it of records) {
  26. const found = list.filter(x => x.dishId === it.id)[0]
  27. if (found) {
  28. found.dishAmount = amount;
  29. } else {
  30. list.push({
  31. guaranteeId: dataSource.id,
  32. dishId: it.id,
  33. dishName: it.name,
  34. dishUnit: it.unit,
  35. dishAmount: amount,
  36. })
  37. }
  38. }
  39. setList(list.slice());
  40. }
  41. })
  42. };
  43. const handleDishSubmit = ({item, amount}) => {
  44. const found = list.filter(x => x.dishId === item.id)[0]
  45. if (found) {
  46. found.dishAmount = amount;
  47. } else {
  48. list.push({
  49. guaranteeId: dataSource.id,
  50. dishId: item.id,
  51. dishName: item.name,
  52. dishUnit: item.unit,
  53. dishAmount: amount,
  54. })
  55. }
  56. setList(list.slice());
  57. };
  58. const onDelete = (item) => {
  59. if (item.id) {
  60. setLoading(true);
  61. deleteGuaranteeDetail(item.id).then(res => {
  62. setLoading(false);
  63. const nwList = list.filter(x => x.dishId !== item.dishId);
  64. setList(nwList);
  65. }).catch(() => {
  66. setLoading(false);
  67. })
  68. } else {
  69. const nwList = list.filter(x => x.dishId !== item.dishId);
  70. setList(nwList);
  71. }
  72. }
  73. const onSubmit = () => {
  74. setLoading(true);
  75. addGuaranteeDetailBatch(list, dataSource.id).then(res => {
  76. setLoading(false);
  77. setList(res);
  78. notification.success({ message: '操作成功' });
  79. }).catch(() => {
  80. setLoading(false);
  81. });
  82. if (packageRef.current) {
  83. dataSource.packageName = packageRef.current.name;
  84. updateGuaranteeTask(dataSource, dataSource.id).then(res => {
  85. setDataSource(res);
  86. });
  87. }
  88. }
  89. useEffect(() => {
  90. if (dataSource && dataSource.id) {
  91. setLoading(true);
  92. getGuaranteeDetailList({ pageNum: 1, pageSize: 500, guaranteeId: dataSource.id }).then(res => {
  93. setLoading(false);
  94. setList(res.records || []);
  95. }).catch(() => {
  96. setLoading(false);
  97. })
  98. }
  99. }, [dataSource])
  100. return (
  101. <Row gutter={48}>
  102. <Col span={8}>
  103. <div>
  104. <h3>选择套餐</h3>
  105. <Selector
  106. placeholder="请选择套餐"
  107. disabled={!dataSource}
  108. fetch={getPackageList}
  109. onSubmit={handlePackageSubmit}
  110. />
  111. </div>
  112. <div style={{ marginTop: '48px' }}>
  113. <h3>选择菜肴</h3>
  114. <Selector
  115. placeholder="请选择菜肴"
  116. disabled={!dataSource}
  117. fetch={getDishList}
  118. onSubmit={handleDishSubmit}
  119. />
  120. </div>
  121. </Col>
  122. <Col span={16}>
  123. <Card
  124. title="已选菜肴"
  125. loading={loading}
  126. extra={<Button disabled={!dataSource} type='primary' loading={loading} onClick={onSubmit}>保存</Button>}
  127. >
  128. <EditableTag
  129. list={list}
  130. keyFuc={x => x.dishId}
  131. labelFunc={x => `${x.dishName} × ${x.dishAmount}`}
  132. onDelete={onDelete}
  133. />
  134. </Card>
  135. </Col>
  136. </Row>
  137. )
  138. }