Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce or Update useCreateLink Hook #226

Open
5 tasks
mahid797 opened this issue Feb 23, 2025 · 0 comments
Open
5 tasks

Introduce or Update useCreateLink Hook #226

mahid797 opened this issue Feb 23, 2025 · 0 comments
Assignees
Labels
Frontend Frontend Related Issue ⚡Important High-impact issue that needs to be resolved before the next release Refactor Code Improvement
Milestone

Comments

@mahid797
Copy link
Collaborator

Implement a dedicated hook to handle the link creation API call (POST /api/documents/[documentId]/links) with typed request and response. Remove direct axios usage from the component logic, ensuring a DRY approach.

Detailed Tasks

  1. Create a Hook under src/hooks/documentLinks/ (or documents/) named useCreateLink.ts.

    • Use useMutation from React Query (Tanstack Query) or similar.
    • Accept a typed payload (e.g. CreateDocumentLinkPayload) plus the documentId.
  2. Models

    • The request is CreateDocumentLinkPayload, and the response might be DocumentLinkResponse:
      interface DocumentLinkResponse {
      	message: string;
      	link: {
      		linkUrl: string;
      		alias: string;
      		// ...
      	};
      }
    • Return these typed values for better code clarity.
  3. Remove Direct axios in CreateLink.tsx

    • Instead, import useCreateLink and call createLink(payload).
    • Catch and display errors if the mutation fails.
  4. Example

    // useCreateLink.ts
    import { useMutation } from '@tanstack/react-query';
    import axios from 'axios';
    import { CreateDocumentLinkPayload, DocumentLinkResponse } from '@/shared/models/links';
    
    export function useCreateLink(documentId: string) {
    	const mutation = useMutation(async (payload: CreateDocumentLinkPayload) => {
    		const res = await axios.post<DocumentLinkResponse>(
    			`/api/documents/${documentId}/links`,
    			payload,
    		);
    		return res.data;
    	});
    
    	return {
    		createLink: mutation.mutateAsync,
    		loading: mutation.isLoading,
    		error: mutation.error,
    	};
    }

Acceptance Criteria

  • A new or updated useCreateLink hook in src/hooks/.
  • CreateLink.tsx no longer calls axios.post directly.
  • The hook returns typed data so the calling component can read res.link.linkUrl, etc.
  • Error handling is consistent with other hooks (e.g., toasts, logs).
  • The code is DRY—no repeated link creation logic in multiple places.
@mahid797 mahid797 added this to the v0.1 milestone Feb 23, 2025
@mahid797 mahid797 added New Feature New feature to be implemented Refactor Code Improvement Frontend Frontend Related Issue ⚡Important High-impact issue that needs to be resolved before the next release and removed New Feature New feature to be implemented labels Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frontend Frontend Related Issue ⚡Important High-impact issue that needs to be resolved before the next release Refactor Code Improvement
Projects
None yet
Development

No branches or pull requests

2 participants