Implement actions on the home view
This commit is contained in:
69
packages/web/lib/hooks/useLibraryItemActions.tsx
Normal file
69
packages/web/lib/hooks/useLibraryItemActions.tsx
Normal file
@ -0,0 +1,69 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { setLinkArchivedMutation } from '../networking/mutations/setLinkArchivedMutation'
|
||||
import {
|
||||
showErrorToast,
|
||||
showSuccessToast,
|
||||
showSuccessToastWithUndo,
|
||||
} from '../toastHelpers'
|
||||
import { deleteLinkMutation } from '../networking/mutations/deleteLinkMutation'
|
||||
import { updatePageMutation } from '../networking/mutations/updatePageMutation'
|
||||
import { State } from '../networking/fragments/articleFragment'
|
||||
|
||||
export default function useLibraryItemActions() {
|
||||
const archiveItem = useCallback(async (itemId: string) => {
|
||||
const result = await setLinkArchivedMutation({
|
||||
linkId: itemId,
|
||||
archived: true,
|
||||
})
|
||||
|
||||
if (result) {
|
||||
showSuccessToast('Link archived', { position: 'bottom-right' })
|
||||
} else {
|
||||
showErrorToast('Error archiving link', { position: 'bottom-right' })
|
||||
}
|
||||
|
||||
return !!result
|
||||
}, [])
|
||||
|
||||
const deleteItem = useCallback(async (itemId: string, undo: () => void) => {
|
||||
const result = await deleteLinkMutation(itemId)
|
||||
|
||||
if (result) {
|
||||
showSuccessToastWithUndo('Item removed', async () => {
|
||||
const result = await updatePageMutation({
|
||||
pageId: itemId,
|
||||
state: State.SUCCEEDED,
|
||||
})
|
||||
|
||||
undo()
|
||||
|
||||
if (result) {
|
||||
showSuccessToast('Item recovered')
|
||||
} else {
|
||||
showErrorToast('Error recovering, check your deleted items')
|
||||
}
|
||||
})
|
||||
} else {
|
||||
showErrorToast('Error removing item', { position: 'bottom-right' })
|
||||
}
|
||||
|
||||
return !!result
|
||||
}, [])
|
||||
|
||||
const moveItem = useCallback(async (itemId: string) => {
|
||||
const result = await setLinkArchivedMutation({
|
||||
linkId: itemId,
|
||||
archived: true,
|
||||
})
|
||||
|
||||
if (result) {
|
||||
showSuccessToast('Link archived', { position: 'bottom-right' })
|
||||
} else {
|
||||
showErrorToast('Error archiving link', { position: 'bottom-right' })
|
||||
}
|
||||
|
||||
return !!result
|
||||
}, [])
|
||||
|
||||
return { archiveItem, deleteItem, moveItem }
|
||||
}
|
||||
Reference in New Issue
Block a user