useMoveDocumentToProject
Overview
useMoveDocumentToProject
is a custom React hook designed to move a document to a different project. It uses @tanstack/react-query
for managing the mutation and caching. The hook utilizes the authentication state and project session to determine the current project and user context.
Usage
To use useMoveDocumentToProject
, ensure your component has access to authentication data via useAuth
and project session data via useProjectSession
.
import React from 'react';
import { useMutation } from '@tanstack/react-query';
import useMoveDocumentToProject from '@lunchbox/apps/hooks/useMoveDocumentToProject';
import { NavbarDoc } from '@lunchbox/apps/types/docs';
const DocumentMover: React.FC<{ doc: NavbarDoc }> = ({ doc }) => {
const moveDocument = useMoveDocumentToProject();
const handleMove = () => {
moveDocument.mutate({ doc, projectId: 'newProjectId' });
};
return (
<button onClick={handleMove}>Move Document</button>
);
};
export default DocumentMover;
Parameters
This hook does not take any parameters directly. It uses the following:
payload
: An object containing:doc
: The document to be moved.projectId
: The ID of the project to move the document to.
Returns
The hook returns a mutation object with the following properties, managed by useMutation
from @tanstack/react-query
:
mutate
: A function to trigger the mutation.isLoading
: A boolean indicating if the mutation is currently being executed.isError
: A boolean indicating if there was an error during the mutation.error
: The error object if an error occurred during the mutation.isSuccess
: A boolean indicating if the mutation was successful.
Notes
- The hook relies on
useAuth
to retrieve the authenticated user data anduseProjectSession
to retrieve the current project data. - The
DOCUMENT_MAPPER
should map document types to their respective classes, which implement themoveToProject
method. - Upon successful mutation, the hook invalidates the query for the document list to ensure the UI reflects the updated state.