useEditContactCard.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import Taro, { useRouter } from "@tarojs/taro";
  2. import { useState } from "react";
  3. import { useAgentStore } from "@/store/agentStore";
  4. import { TAgentDetail } from "@/types/agent";
  5. const useEditContactCard = (
  6. editKey: string,
  7. initValue?: string,
  8. _agent?: TAgentDetail
  9. ) => {
  10. const router = useRouter();
  11. const { agentId } = router.params;
  12. const agent = useAgentStore((state) => _agent ?? state.agent);
  13. const agentContactCard = useAgentStore((state) => state.agentContactCard);
  14. const [value, setValue] = useState(initValue ?? "");
  15. const { editAgentCard,fetchAgent } = useAgentStore();
  16. const handleSubmit = async () => {
  17. if (!agent?.agentId) {
  18. return;
  19. }
  20. if (value.length <= 0) {
  21. return;
  22. }
  23. const result = await editAgentCard(agent.agentId, {
  24. ...agentContactCard,
  25. [editKey]: value,
  26. });
  27. if(result){
  28. await fetchAgent(agent.agentId)
  29. Taro.navigateBack()
  30. }
  31. };
  32. const onChange = (e: any) => {
  33. setValue(e);
  34. };
  35. return {
  36. value,
  37. setValue,
  38. onChange,
  39. handleSubmit,
  40. };
  41. };
  42. export default useEditContactCard;