-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathusePin.ts
62 lines (54 loc) · 1.4 KB
/
usePin.ts
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
import { revalidate } from '@/actions/revalidate'
import { toast } from 'sonner'
import { createSupabaseBrowserClient } from '@/utils/supabase-client'
import { removePin, updateIsTopStatus } from '@/services/pins'
export function usePin() {
const deletePin = async ({ id }: { id: string }) => {
try {
const response = await removePin({
id
})
if (response === 'ok') {
revalidate({
key: '/pins',
type: 'path'
})
}
} catch (error) {
if (error instanceof Error) {
toast.error(error.message)
}
}
}
const updatePinStatus = async ({ id, action }: { id: string; action: 'add' | 'remove' }) => {
try {
const supabase = await createSupabaseBrowserClient()
const {
data: { user }
} = await supabase.auth.getUser()
if (!user) {
toast.warning('You need to be logged in to pin a resource.')
return
}
const { id: userId } = user
const response = await updateIsTopStatus({ pinId: id, action, userId })
if (response === 'ok') {
revalidate({
key: '/pins',
type: 'path'
})
// toast.success('Pin Updated', {
// duration: 2000
// })
}
} catch (error) {
if (error instanceof Error) {
toast.error(error.message)
}
}
}
return {
deletePin,
updatePinStatus
}
}