index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * 知识库
  3. */
  4. import { Text, View, Image, ScrollView } from "@tarojs/components";
  5. import WemetaSwitch from "@/components/WemetaSwitch";
  6. import Popup from "@/components/popup/popup";
  7. import { useEffect, useState } from "react";
  8. import Taro, { useRouter, useDidShow } from "@tarojs/taro";
  9. import {
  10. shareToEnt,
  11. } from "@/service/knowledge";
  12. import { useUserStore } from "@/store/userStore";
  13. import { TEntItem } from "@/types/user";
  14. import { isSuccess } from "@/utils";
  15. interface IProps {
  16. knowledgeId: string|number
  17. showPopup: boolean
  18. setShowPopup: (show: boolean)=> void,
  19. }
  20. export default function Index({knowledgeId, showPopup, setShowPopup }:IProps) {
  21. const {entList, fetchMyEntList} = useUserStore()
  22. const [shareEntList, setShareEntList] = useState(entList ?? []);
  23. const handleSwitchChange = async (ent: TEntItem, checked: boolean)=> {
  24. if(!knowledgeId){
  25. return false;
  26. }
  27. const response = await shareToEnt({
  28. knowledgeIds: [knowledgeId],
  29. toEntId: ent.entId,
  30. })
  31. if(isSuccess(response.status)){
  32. Taro.showToast({title: checked ? '分享成功' : '取消分享成功'})
  33. setShareEntList((prev) => {
  34. if(checked){
  35. return [...prev, ent]
  36. }
  37. return prev.filter( item => item.entId !== ent.entId)
  38. })
  39. }
  40. }
  41. useDidShow(() => {
  42. fetchMyEntList()
  43. });
  44. const renderContent = ()=> {
  45. if(!entList.length){
  46. return <View className="rounded-8 bg-gray-3 py-20 text-center">
  47. <View className="leading-24 text-gray-45 font-medium text-14 mb-4">
  48. 暂未加入任何企业
  49. </View>
  50. <View
  51. className="text-primary"
  52. onClick={() => {
  53. Taro.navigateTo({
  54. url: "/pages/contact-us/index",
  55. });
  56. }}
  57. >
  58. 联系我们
  59. </View>
  60. </View>
  61. }
  62. return <View className="flex flex-col gap-12 pb-24" style={{maxHeight: '60vh', overflow: 'auto'}}>
  63. {entList.map(ent=> {
  64. return <View className="flex items-center rounded-8 bg-gray-3 px-16 py-24">
  65. <View className="flex-1 text-black font-medium text-14 leading-22">{ent.entName}</View>
  66. <WemetaSwitch
  67. checked={!!shareEntList.find((item)=> item.entId === ent.entId)}
  68. onChange={(checked) => handleSwitchChange(ent, checked)}
  69. ></WemetaSwitch>
  70. </View>
  71. })}
  72. </View>
  73. }
  74. return (
  75. <Popup setShow={setShowPopup} show={showPopup} title="共享至">
  76. <View className="text-gray-45 leading-20 mb-16 text-12">开启后,该组织下的所有成员都可引用,可随时取消共享。</View>
  77. {renderContent()}
  78. </Popup>
  79. );
  80. }