Skip to content

Commit

Permalink
Merge branch 'database-staging' into database-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
yukigesho authored Nov 14, 2024
2 parents f2d3825 + ab2b5b2 commit 00133be
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions src/components/global/FormClaim.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import imageCompression from 'browser-image-compression';
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useDropzone } from 'react-dropzone';
import { toast } from 'react-toastify';

Expand Down Expand Up @@ -39,6 +39,7 @@ export default function FormClaim({
const [description, setDescription] = useState('');
const [uploading, setUploading] = useState(false);
const [status, setStatus] = useState<string>('');
const [file, setFile] = useState<File | null>(null);
const utils = trpc.useUtils();

const account = useAccount();
Expand All @@ -47,19 +48,15 @@ export default function FormClaim({
const switchChain = useSwitchChain();

const onDrop = useCallback((acceptedFiles: File[]) => {
if (acceptedFiles.length > 0) {
const selectedFile = acceptedFiles[0];
const reader = new FileReader();

reader.onload = (e: ProgressEvent<FileReader>) => {
if (e.target?.result) {
setPreview(e.target.result.toString());
handleImageUpload(selectedFile);
}
};

reader.readAsDataURL(selectedFile);
}
const file = acceptedFiles[0];
setFile(file);
const reader = new FileReader();
reader.onload = (e: ProgressEvent<FileReader>) => {
if (e.target?.result) {
setPreview(e.target.result.toString());
}
};
reader.readAsDataURL(file);
}, []);

const { getRootProps, getInputProps, isDragActive } = useDropzone({
Expand Down Expand Up @@ -87,11 +84,17 @@ export default function FormClaim({

const compressImage = async (image: File): Promise<File> => {
const options = {
maxSizeMB: 1,
maxSizeMB: 10,
maxWidthOrHeight: 1920,
useWebWorker: true,
};
return await imageCompression(image, options);
try {
const compressedFile = await imageCompression(image, options);
return compressedFile;
} catch (error) {
toast.error('Error compressing image');
throw error;
}
};

const retryUpload = async (file: File): Promise<string> => {
Expand All @@ -103,13 +106,36 @@ export default function FormClaim({
const cid = await uploadFile(file);
return cid.IpfsHash;
} catch (error) {
if (attempt === MAX_RETRIES) throw error;
if (attempt === MAX_RETRIES) {
throw error;
}
console.log(
`Attempt ${attempt} failed, retrying in ${RETRY_DELAY}ms...`
);
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
}
}
throw new Error('All upload attempts failed');
throw new Error('All attempts failed');
};

useEffect(() => {
const uploadImage = async () => {
if (file) {
setUploading(true);
try {
const compressedFile = await compressImage(file);
const cid = await retryUpload(compressedFile);
setImageURI(`${LINK_IPFS}/${cid}`);
} catch (error) {
toast.error('Failed to upload image: ' + error);
} finally {
setUploading(false);
}
}
};
uploadImage();
}, [file]);

const createClaimMutations = useMutation({
mutationFn: async (bountyId: bigint) => {
const chainId = await account.connector?.getChainId();
Expand Down

0 comments on commit 00133be

Please sign in to comment.