WbwDetail.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { useEffect, useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import { Dropdown, Tabs, Divider, Button, Switch, Rate } from "antd";
  4. import type { MenuProps } from "antd";
  5. import { SaveOutlined, CommentOutlined } from "@ant-design/icons";
  6. import { IWbw, IWbwField, TFieldName } from "./WbwWord";
  7. import WbwDetailBasic from "./WbwDetailBasic";
  8. import WbwDetailBookMark from "./WbwDetailBookMark";
  9. import WbwDetailNote from "./WbwDetailNote";
  10. import WbwDetailAdvance from "./WbwDetailAdvance";
  11. import { LockIcon, UnLockIcon } from "../../../assets/icon";
  12. import { UploadFile } from "antd/es/upload/interface";
  13. import { IAttachmentResponse } from "../../api/Attachments";
  14. import WbwDetailAttachment from "./WbwDetailAttachment";
  15. import CommentBox from "../../comment/CommentBox";
  16. interface IWidget {
  17. data: IWbw;
  18. onClose?: Function;
  19. onSave?: Function;
  20. onCommentCountChange?: Function;
  21. }
  22. const WbwDetailWidget = ({
  23. data,
  24. onClose,
  25. onSave,
  26. onCommentCountChange,
  27. }: IWidget) => {
  28. const intl = useIntl();
  29. const [currWbwData, setCurrWbwData] = useState(data);
  30. useEffect(() => {
  31. setCurrWbwData(data);
  32. }, [data]);
  33. function fieldChanged(field: TFieldName, value: string) {
  34. console.log("field", field, "value", value);
  35. let mData = currWbwData;
  36. switch (field) {
  37. case "note":
  38. mData.note = { value: value, status: 7 };
  39. break;
  40. case "bookMarkColor":
  41. mData.bookMarkColor = { value: parseInt(value), status: 7 };
  42. break;
  43. case "bookMarkText":
  44. mData.bookMarkText = { value: value, status: 7 };
  45. break;
  46. case "word":
  47. mData.word = { value: value, status: 7 };
  48. break;
  49. case "real":
  50. mData.real = { value: value, status: 7 };
  51. break;
  52. case "meaning":
  53. mData.meaning = { value: value, status: 7 };
  54. break;
  55. case "factors":
  56. mData.factors = { value: value, status: 7 };
  57. break;
  58. case "factorMeaning":
  59. mData.factorMeaning = { value: value, status: 7 };
  60. break;
  61. case "parent":
  62. mData.parent = { value: value, status: 7 };
  63. break;
  64. case "parent2":
  65. mData.parent2 = { value: value, status: 7 };
  66. break;
  67. case "grammar2":
  68. mData.grammar2 = { value: value, status: 7 };
  69. break;
  70. case "case":
  71. const arrCase = value.split("#");
  72. mData.case = { value: value, status: 7 };
  73. mData.type = { value: arrCase[0] ? arrCase[0] : "", status: 7 };
  74. mData.grammar = { value: arrCase[1] ? arrCase[1] : "", status: 7 };
  75. break;
  76. case "relation":
  77. mData.relation = { value: value, status: 7 };
  78. break;
  79. case "confidence":
  80. mData.confidence = parseFloat(value);
  81. break;
  82. case "locked":
  83. mData.locked = JSON.parse(value);
  84. break;
  85. default:
  86. break;
  87. }
  88. setCurrWbwData(mData);
  89. }
  90. const onMenuClick: MenuProps["onClick"] = (e) => {
  91. console.log("click", e);
  92. };
  93. const items = [
  94. {
  95. key: "user-dict",
  96. label: intl.formatMessage({ id: "buttons.save.publish" }),
  97. },
  98. ];
  99. return (
  100. <div
  101. style={{
  102. minWidth: 450,
  103. }}
  104. >
  105. <Tabs
  106. size="small"
  107. type="card"
  108. tabBarExtraContent={
  109. data.uid ? (
  110. <CommentBox
  111. resId={data.uid}
  112. resType="wbw"
  113. trigger={<Button icon={<CommentOutlined />} type="text" />}
  114. onCommentCountChange={(count: number) => {
  115. if (typeof onCommentCountChange !== "undefined") {
  116. onCommentCountChange(count);
  117. }
  118. }}
  119. />
  120. ) : undefined
  121. }
  122. items={[
  123. {
  124. label: intl.formatMessage({ id: "buttons.basic" }),
  125. key: "basic",
  126. children: (
  127. <div>
  128. <WbwDetailBasic
  129. data={data}
  130. onChange={(e: IWbwField) => {
  131. console.log("WbwDetailBasic onchange", e);
  132. fieldChanged(e.field, e.value);
  133. }}
  134. onRelationAdd={() => {
  135. if (typeof onClose !== "undefined") {
  136. onClose();
  137. }
  138. }}
  139. />
  140. </div>
  141. ),
  142. },
  143. {
  144. label: intl.formatMessage({ id: "buttons.bookmark" }),
  145. key: "bookmark",
  146. children: (
  147. <div style={{ minHeight: 270 }}>
  148. <WbwDetailBookMark
  149. data={data}
  150. onChange={(e: IWbwField) => {
  151. fieldChanged(e.field, e.value);
  152. }}
  153. />
  154. </div>
  155. ),
  156. },
  157. {
  158. label: intl.formatMessage({ id: "buttons.note" }),
  159. key: "note",
  160. children: (
  161. <div style={{ minHeight: 270 }}>
  162. <WbwDetailNote
  163. data={data}
  164. onChange={(e: IWbwField) => {
  165. fieldChanged(e.field, e.value);
  166. }}
  167. />
  168. </div>
  169. ),
  170. },
  171. {
  172. label: intl.formatMessage({ id: "buttons.spell" }),
  173. key: "spell",
  174. children: (
  175. <div style={{ minHeight: 270 }}>
  176. <WbwDetailAdvance
  177. data={currWbwData}
  178. onChange={(e: IWbwField) => {
  179. fieldChanged(e.field, e.value);
  180. }}
  181. />
  182. </div>
  183. ),
  184. },
  185. {
  186. label: intl.formatMessage({ id: "buttons.attachments" }),
  187. key: "attachments",
  188. children: (
  189. <div style={{ minHeight: 270 }}>
  190. <WbwDetailAttachment
  191. data={currWbwData}
  192. onChange={(e: IWbwField) => {
  193. fieldChanged(e.field, e.value);
  194. }}
  195. onUpload={(fileList: UploadFile<IAttachmentResponse>[]) => {
  196. let mData = currWbwData;
  197. mData.attachments = fileList.map((item) => {
  198. return {
  199. uid: item.uid,
  200. name: item.name,
  201. size: item.size,
  202. type: item.type,
  203. url: item.response?.data.url,
  204. };
  205. });
  206. setCurrWbwData(mData);
  207. }}
  208. />
  209. </div>
  210. ),
  211. },
  212. ]}
  213. />
  214. <Divider style={{ margin: "4px 0" }}></Divider>
  215. <div style={{ display: "flex", justifyContent: "space-between" }}>
  216. <Switch
  217. checkedChildren={<LockIcon />}
  218. unCheckedChildren={<UnLockIcon />}
  219. />
  220. <Rate
  221. allowHalf
  222. defaultValue={data.confidence * 5}
  223. onChange={(value: number) => {
  224. fieldChanged("confidence", (value / 5).toString());
  225. }}
  226. />
  227. </div>
  228. <Divider style={{ margin: "4px 0" }}></Divider>
  229. <div style={{ display: "flex", justifyContent: "space-between" }}>
  230. <div>
  231. <Button
  232. danger
  233. onClick={() => {
  234. if (typeof onClose !== "undefined") {
  235. onClose();
  236. }
  237. }}
  238. >
  239. {intl.formatMessage({ id: "buttons.cancel" })}
  240. </Button>
  241. </div>
  242. <Dropdown.Button
  243. style={{ width: "unset" }}
  244. type="primary"
  245. menu={{ items, onClick: onMenuClick }}
  246. onClick={() => {
  247. if (typeof onSave !== "undefined") {
  248. onSave(currWbwData);
  249. }
  250. }}
  251. >
  252. <SaveOutlined />
  253. {intl.formatMessage({ id: "buttons.save" })}
  254. </Dropdown.Button>
  255. </div>
  256. </div>
  257. );
  258. };
  259. export default WbwDetailWidget;