File size: 1,151 Bytes
cd6f98e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { Agent as PrismaAgent } from "@prisma/client";

import { useAuth } from "./useAuth";
import type { CreateAgentProps, SaveAgentProps } from "../server/api/routers/agentRouter";
import { api } from "../utils/api";


export type AgentUtils = {
  createAgent: (data: CreateAgentProps) => Promise<PrismaAgent | undefined>;
  saveAgent: (data: SaveAgentProps) => void;
};

export function useAgent(): AgentUtils {
  const { status } = useAuth();
  const utils = api.useContext();

  const createMutation = api.agent.create.useMutation({
    onSuccess: (data: PrismaAgent) => {
      utils.agent.getAll.setData(void 0, (oldData) => [data, ...(oldData ?? [])]);
      return data;
    },
  });
  const createAgent = async (data: CreateAgentProps): Promise<PrismaAgent | undefined> => {
    if (status === "authenticated") {
      return await createMutation.mutateAsync(data);
    } else {
      return undefined;
    }
  };

  const saveMutation = api.agent.save.useMutation();
  const saveAgent = (data: SaveAgentProps) => {
    if (status === "authenticated") saveMutation.mutate(data);
  };

  return {
    createAgent,
    saveAgent,
  };
}