useEditContactCard.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 | null,
  8. _agent?: TAgentDetail
  9. ) => {
  10. const agent = useAgentStore((state) => _agent ?? state.agent);
  11. const agentContactCard = useAgentStore((state) => state.agentContactCard);
  12. const [value, setValue] = useState(initValue ?? "");
  13. const { editAgentCard,fetchAgent } = useAgentStore();
  14. const handleSubmit = async () => {
  15. submit(value)
  16. };
  17. const submit = async (_value: string, navBack: boolean = true) => {
  18. if (!agent?.agentId) {
  19. return;
  20. }
  21. if (_value === undefined) {
  22. return;
  23. }
  24. // const prev = pickNonEmpty(agentContactCard??{})
  25. const result = await editAgentCard(agent.agentId, {
  26. ...agentContactCard,
  27. [editKey]: _value,
  28. });
  29. if(result){
  30. await fetchAgent(agent.agentId)
  31. navBack && Taro.navigateBack()
  32. }
  33. }
  34. const onChange = (e: any) => {
  35. setValue(e);
  36. };
  37. return {
  38. value,
  39. setValue,
  40. onChange,
  41. submit,
  42. handleSubmit,
  43. };
  44. };
  45. export default useEditContactCard;