Skip to content

Commit

Permalink
refactor: Extract useUpload into its own reusable hook
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Sep 21, 2024
1 parent 3377969 commit 6b5c597
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
24 changes: 4 additions & 20 deletions apps/web/components/dashboard/UploadDropzone.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import React, { useCallback, useState } from "react";
import useUpload from "@/lib/hooks/upload-file";
import { parseNetscapeBookmarkFile } from "@/lib/netscapeBookmarkParser";
import { cn } from "@/lib/utils";
import { useMutation } from "@tanstack/react-query";
Expand All @@ -9,10 +10,6 @@ import DropZone from "react-dropzone";

import { useCreateBookmarkWithPostHook } from "@hoarder/shared-react/hooks/bookmarks";
import { BookmarkTypes } from "@hoarder/shared/types/bookmarks";
import {
zUploadErrorSchema,
zUploadResponseSchema,
} from "@hoarder/shared/types/uploads";

import LoadingSpinner from "../ui/spinner";
import { toast } from "../ui/use-toast";
Expand All @@ -35,26 +32,13 @@ export function useUploadAsset() {
},
});

const { mutateAsync: runUploadAsset } = useMutation({
mutationFn: async (file: File) => {
const formData = new FormData();
formData.append("file", file);
const resp = await fetch("/api/assets", {
method: "POST",
body: formData,
});
if (!resp.ok) {
throw new Error(await resp.text());
}
return zUploadResponseSchema.parse(await resp.json());
},
const { mutateAsync: runUploadAsset } = useUpload({
onSuccess: async (resp) => {
const assetType =
resp.contentType === "application/pdf" ? "pdf" : "image";
return createBookmark({ ...resp, type: BookmarkTypes.ASSET, assetType });
await createBookmark({ ...resp, type: BookmarkTypes.ASSET, assetType });
},
onError: (error, req) => {
const err = zUploadErrorSchema.parse(JSON.parse(error.message));
onError: (err, req) => {
toast({
description: `${req.name}: ${err.error}`,
variant: "destructive",
Expand Down
38 changes: 38 additions & 0 deletions apps/web/lib/hooks/upload-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useMutation } from "@tanstack/react-query";

import {
ZUploadError,
zUploadErrorSchema,
ZUploadResponse,
zUploadResponseSchema,
} from "@hoarder/shared/types/uploads";

export default function useUpload({
onSuccess,
onError,
}: {
onError?: (e: ZUploadError, req: File) => void;
onSuccess?: (resp: ZUploadResponse, req: File) => Promise<void>;
}) {
return useMutation({
mutationFn: async (file: File) => {
const formData = new FormData();
formData.append("file", file);
const resp = await fetch("/api/assets", {
method: "POST",
body: formData,
});
if (!resp.ok) {
throw new Error(await resp.text());
}
return zUploadResponseSchema.parse(await resp.json());
},
onSuccess: onSuccess,
onError: (error, req) => {
const err = zUploadErrorSchema.parse(JSON.parse(error.message));
if (onError) {
onError(err, req);
}
},
});
}
2 changes: 2 additions & 0 deletions packages/shared/types/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const zUploadErrorSchema = z.object({
error: z.string(),
});

export type ZUploadError = z.infer<typeof zUploadErrorSchema>;

export const zUploadResponseSchema = z.object({
assetId: z.string(),
contentType: z.string(),
Expand Down

0 comments on commit 6b5c597

Please sign in to comment.