No Description

ItemToolBar.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import React from "react";
  2. import classnames from "classnames";
  3. import PropTypes from "prop-types";
  4. import { Divider, Popconfirm } from "antd";
  5. import IconFont from "./ScheduleIconFont";
  6. import { CollapseBtn } from "./CollapseBtn";
  7. import { getActionAuth, AuthType } from "../utils";
  8. import styles from "./ItemToolBar.less";
  9. import { LoadingOutlined } from "@ant-design/icons";
  10. export enum TOOL_ACTION_TYPE {
  11. REPLY = "reply",
  12. FAVOR = "favor",
  13. EDIT = "edit",
  14. DELETE = "delete",
  15. }
  16. interface ItemToolBarProps {
  17. replyCount: number;
  18. authType: AuthType;
  19. handleReplyClick(): boolean;
  20. handlePrivateClick(actionType: string): Promise<boolean> | boolean;
  21. handleNormalClick(actionType: string): Promise<boolean> | boolean;
  22. favorCount: number;
  23. favored: boolean;
  24. }
  25. interface ItemToolBarState {
  26. favorAjaxLoading: boolean;
  27. }
  28. class ItemToolBar extends React.Component<ItemToolBarProps, ItemToolBarState> {
  29. constructor(props: ItemToolBarProps) {
  30. super(props);
  31. this.state = {
  32. favorAjaxLoading: false,
  33. };
  34. }
  35. renderPrivateTool = () => {
  36. const {
  37. authType,
  38. replyCount,
  39. handleReplyClick,
  40. handlePrivateClick,
  41. } = this.props;
  42. const showReplyCollapse = replyCount > 0;
  43. const showEdit = getActionAuth(authType, TOOL_ACTION_TYPE.EDIT);
  44. const showDelete = getActionAuth(authType, TOOL_ACTION_TYPE.DELETE);
  45. const notShowPrivate =
  46. [showReplyCollapse, showEdit, showDelete].reduce(
  47. (a, b) => Number(a) + Number(b),
  48. 0
  49. ) === 0;
  50. console.log("notShowPrivate: ", notShowPrivate);
  51. if (notShowPrivate) return null;
  52. return (
  53. <>
  54. {showReplyCollapse && (
  55. <span
  56. className={classnames(styles.toolsItem, styles.replyCollapseBtn)}
  57. >
  58. <CollapseBtn
  59. text={`${replyCount} 回复`}
  60. onChange={handleReplyClick}
  61. />
  62. </span>
  63. )}
  64. {showEdit && (
  65. <span
  66. className={classnames(styles.toolsItem)}
  67. onClick={() => {
  68. handlePrivateClick("edit");
  69. }}
  70. >
  71. <IconFont
  72. type="schedule-icon_answer"
  73. style={{
  74. fontSize: "18px",
  75. }}
  76. />
  77. </span>
  78. )}
  79. {showDelete && (
  80. <span className={classnames(styles.toolsItem)}>
  81. <Popconfirm
  82. title="是否删除"
  83. onConfirm={() => {
  84. handlePrivateClick("delete");
  85. }}
  86. >
  87. <IconFont
  88. type="schedule-icon_deletecopy"
  89. style={{
  90. fontSize: "18px",
  91. }}
  92. />
  93. </Popconfirm>
  94. </span>
  95. )}
  96. <Divider
  97. type="vertical"
  98. style={{ borderLeftColor: "#c6c6c6", fontSize: "24px" }}
  99. />
  100. </>
  101. );
  102. };
  103. renderNormalTool = () => {
  104. const { favorAjaxLoading } = this.state;
  105. const { favorCount, favored, handleNormalClick } = this.props;
  106. return (
  107. <>
  108. <span
  109. className={classnames(styles.toolsItem, styles.favorBtnWrapper)}
  110. onClick={async (e) => {
  111. e.preventDefault();
  112. if (!favorAjaxLoading) {
  113. this.setState({ favorAjaxLoading: true });
  114. await handleNormalClick(TOOL_ACTION_TYPE.FAVOR);
  115. this.setState({ favorAjaxLoading: false });
  116. }
  117. }}
  118. >
  119. <span className={styles.iconWrapper}>
  120. {favorAjaxLoading ? (
  121. <LoadingOutlined
  122. style={{
  123. color: "#fc4747",
  124. display: "inline-flex",
  125. alignItems: "center",
  126. }}
  127. />
  128. ) : (
  129. <IconFont
  130. type={
  131. favored
  132. ? "schedule-icon_like_fill1"
  133. : "schedule-icon_like_line"
  134. }
  135. style={{
  136. fontSize: "18px",
  137. transform: "translateY(1px)",
  138. color: favored ? "#fc4747" : "inherit",
  139. }}
  140. />
  141. )}
  142. </span>
  143. <span className={styles.btnCount}>{favorCount}</span>
  144. </span>
  145. <span
  146. className={styles.toolsItem}
  147. onClick={(e) => {
  148. e.preventDefault();
  149. handleNormalClick(TOOL_ACTION_TYPE.REPLY);
  150. }}
  151. >
  152. <span className={styles.replyBtn}>回复</span>
  153. </span>
  154. </>
  155. );
  156. };
  157. render() {
  158. return (
  159. <div className={styles.wrapper}>
  160. {this.renderPrivateTool()}
  161. {this.renderNormalTool()}
  162. </div>
  163. );
  164. }
  165. }
  166. export default ItemToolBar;