summaryrefslogtreecommitdiff
path: root/apps/web/lib/queries/use-folder-mutations.ts
blob: cc845801286cfc380f28023cce4b3875451d60a6 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"use client"

import { useMutation, useQueryClient } from "@tanstack/react-query"
import { createSupabaseBrowserClient } from "@/lib/supabase/client"
import { queryKeys } from "./query-keys"
import { notify } from "@/lib/notify"

export function useCreateFolder() {
  const supabaseClient = createSupabaseBrowserClient()
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: async ({ name }: { name: string }) => {
      const {
        data: { user },
      } = await supabaseClient.auth.getUser()

      if (!user) throw new Error("not authenticated")

      const { data: maxRow } = await supabaseClient
        .from("folders")
        .select("position")
        .eq("user_id", user.id)
        .order("position", { ascending: false })
        .limit(1)
        .maybeSingle()

      const nextPosition = (maxRow?.position ?? 0) + 1000

      const { error } = await supabaseClient.from("folders").insert({
        user_id: user.id,
        name,
        position: nextPosition,
      })

      if (error) throw error
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: queryKeys.subscriptions.all })
      queryClient.invalidateQueries({ queryKey: queryKeys.userProfile.all })
      notify("folder created")
    },
    onError: (error: Error) => {
      notify(error.message.includes("limit")
        ? "folder limit reached for your plan"
        : "failed to create folder: " + error.message)
    },
  })
}

export function useDeleteAllFolders() {
  const supabaseClient = createSupabaseBrowserClient()
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: async () => {
      const {
        data: { user },
      } = await supabaseClient.auth.getUser()

      if (!user) throw new Error("not authenticated")

      await supabaseClient
        .from("subscriptions")
        .update({ folder_id: null })
        .eq("user_id", user.id)
        .not("folder_id", "is", null)

      const { error } = await supabaseClient
        .from("folders")
        .delete()
        .eq("user_id", user.id)

      if (error) throw error
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: queryKeys.subscriptions.all })
      queryClient.invalidateQueries({ queryKey: queryKeys.userProfile.all })
      notify("all folders deleted")
    },
    onError: (error: Error) => {
      notify("failed to delete all folders: " + error.message)
    },
  })
}

export function useRenameFolder() {
  const supabaseClient = createSupabaseBrowserClient()
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: async ({
      folderIdentifier,
      name,
      iconUrl,
    }: {
      folderIdentifier: string
      name: string
      iconUrl?: string | null
    }) => {
      const updatePayload: Record<string, unknown> = { name }
      if (iconUrl !== undefined) updatePayload.icon_url = iconUrl

      const { error } = await supabaseClient
        .from("folders")
        .update(updatePayload)
        .eq("id", folderIdentifier)

      if (error) throw error
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: queryKeys.subscriptions.all })
      notify("folder renamed")
    },
    onError: (error: Error) => {
      notify("failed to rename folder: " + error.message)
    },
  })
}

export function useDeleteFolder() {
  const supabaseClient = createSupabaseBrowserClient()
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: async ({
      folderIdentifier,
    }: {
      folderIdentifier: string
    }) => {
      await supabaseClient
        .from("subscriptions")
        .update({ folder_id: null })
        .eq("folder_id", folderIdentifier)

      const { error } = await supabaseClient
        .from("folders")
        .delete()
        .eq("id", folderIdentifier)

      if (error) throw error
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: queryKeys.subscriptions.all })
      queryClient.invalidateQueries({ queryKey: queryKeys.userProfile.all })
      notify("folder deleted")
    },
    onError: (error: Error) => {
      notify("failed to delete folder: " + error.message)
    },
  })
}