aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared-react/hooks/highlights.ts
blob: 3f6a6e0122898461aab210b11b3f2527b9bfb4e7 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useMutation, useQueryClient } from "@tanstack/react-query";

import { useTRPC } from "../trpc";

type TRPCApi = ReturnType<typeof useTRPC>;

export function useCreateHighlight(
  opts?: Parameters<TRPCApi["highlights"]["create"]["mutationOptions"]>[0],
) {
  const api = useTRPC();
  const queryClient = useQueryClient();
  return useMutation(
    api.highlights.create.mutationOptions({
      ...opts,
      onSuccess: (res, req, meta, context) => {
        queryClient.invalidateQueries(
          api.highlights.getForBookmark.queryFilter({
            bookmarkId: req.bookmarkId,
          }),
        );
        queryClient.invalidateQueries(api.highlights.getAll.pathFilter());
        return opts?.onSuccess?.(res, req, meta, context);
      },
    }),
  );
}

export function useUpdateHighlight(
  opts?: Parameters<TRPCApi["highlights"]["update"]["mutationOptions"]>[0],
) {
  const api = useTRPC();
  const queryClient = useQueryClient();
  return useMutation(
    api.highlights.update.mutationOptions({
      ...opts,
      onSuccess: (res, req, meta, context) => {
        queryClient.invalidateQueries(
          api.highlights.getForBookmark.queryFilter({
            bookmarkId: res.bookmarkId,
          }),
        );
        queryClient.invalidateQueries(api.highlights.getAll.pathFilter());
        return opts?.onSuccess?.(res, req, meta, context);
      },
    }),
  );
}

export function useDeleteHighlight(
  opts?: Parameters<TRPCApi["highlights"]["delete"]["mutationOptions"]>[0],
) {
  const api = useTRPC();
  const queryClient = useQueryClient();
  return useMutation(
    api.highlights.delete.mutationOptions({
      ...opts,
      onSuccess: (res, req, meta, context) => {
        queryClient.invalidateQueries(
          api.highlights.getForBookmark.queryFilter({
            bookmarkId: res.bookmarkId,
          }),
        );
        queryClient.invalidateQueries(api.highlights.getAll.pathFilter());
        return opts?.onSuccess?.(res, req, meta, context);
      },
    }),
  );
}