Skip to content

Commit

Permalink
chore: more refactoring of functions in the forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Mar 22, 2024
1 parent b402ce7 commit 0acf1f9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 97 deletions.
56 changes: 7 additions & 49 deletions src/components/forms/FormEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Suspense, lazy, useState, useEffect, useRef } from 'react';
import React, { Suspense, lazy, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import {
FormControl,
Expand All @@ -15,7 +15,6 @@ import {
Skeleton,
FormErrorMessage,
} from '@chakra-ui/react';
import pako from 'pako';
import { useForm } from 'react-hook-form';
import { Select } from 'chakra-react-select';
import 'cropperjs/dist/cropper.css';
Expand All @@ -35,6 +34,8 @@ import {
handleCategory,
handleField,
useFileInputRef,
handleImage,
getCrop,
} from '@components/forms/utils/utilsForm';
import { useMyToast } from '@hooks/useMyToast';
import { useGenerateSlug } from '@hooks/useGenerateSlug';
Expand Down Expand Up @@ -116,55 +117,12 @@ export function FormEdit({
handleField(fieldName, newValue, setBooks);
}

async function handleImageChange(e: React.ChangeEvent<HTMLInputElement>) {
const file = e.target.files?.[0];
if (!file) return;

// 1 MB
if (file.size > 1000000) {
alert(
`El tamaño de la imagen es mayor a 1 MB. Por favor, seleccione una imagen de menor tamaño.`,
);

return;
}

const blobImage = new Blob([file], { type: 'image/webp' });
setCropData(URL.createObjectURL(blobImage));

onOpen();
function handleImageChange(e: React.ChangeEvent<HTMLInputElement>) {
handleImage(e, setCropData, onOpen);
}

async function getCropData() {
if (typeof crop !== 'undefined') {
const croppedCanvas = crop.getCroppedCanvas();
croppedCanvas.toBlob((blob) => {
setPreviewImg(blob);

if (blob) {
const reader = new FileReader();
reader.onload = function () {
const arrayBuffer = reader.result as ArrayBuffer;
const uint8Array = new Uint8Array(arrayBuffer);
const compressedArrayBuffer = pako.deflate(uint8Array);
const byteArray = [...new Uint8Array(compressedArrayBuffer)];
const publicId = books.image.public_id;
const pId = publicId.replace('xbu/', '');

setBooks((prevBooks) => ({
...prevBooks,
image: {
url: byteArray,
public_id: pId,
},
}));
};
reader.readAsArrayBuffer(blob);

onClose();
}
}, 'image/webp');
}
function getCropData() {
getCrop(crop, setPreviewImg, books, setBooks, onClose);
}

function onSubmit() {
Expand Down
52 changes: 6 additions & 46 deletions src/components/forms/NewBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
Skeleton,
FormErrorMessage,
} from '@chakra-ui/react';
import pako from 'pako';
import { useForm } from 'react-hook-form';
import { Select } from 'chakra-react-select';
import 'cropperjs/dist/cropper.css';
Expand All @@ -36,6 +35,8 @@ import {
handleCategory,
handleField,
useFileInputRef,
handleImage,
getCrop,
} from '@components/forms/utils/utilsForm';
import { useMyToast } from '@hooks/useMyToast';
import { useAuth } from '@contexts/AuthContext';
Expand Down Expand Up @@ -104,53 +105,12 @@ export function FormNewBook() {
handleField(fieldName, newValue, setBooks);
}

async function handleImageChange(e: React.ChangeEvent<HTMLInputElement>) {
const file = e.target.files?.[0];
if (!file) return;

// 1 MB
if (file.size > 1000000) {
alert(
`El tamaño de la imagen es mayor a 1 MB. Por favor, seleccione una imagen de menor tamaño.`,
);

return;
}

const blobImage = new Blob([file], { type: 'image/webp' });
setCropData(URL.createObjectURL(blobImage));

onOpen();
function handleImageChange(e: React.ChangeEvent<HTMLInputElement>) {
handleImage(e, setCropData, onOpen);
}

async function getCropData() {
if (typeof crop !== 'undefined') {
const croppedCanvas = crop.getCroppedCanvas();
croppedCanvas.toBlob((blob) => {
setPreviewImg(blob);

if (blob) {
const reader = new FileReader();
reader.onload = function () {
const arrayBuffer = reader.result as ArrayBuffer;
const uint8Array = new Uint8Array(arrayBuffer);
const compressedArrayBuffer = pako.deflate(uint8Array);
const byteArray = [...new Uint8Array(compressedArrayBuffer)];

setBooks((prevBooks) => ({
...prevBooks,
image: {
url: byteArray,
public_id: '',
},
}));
};
reader.readAsArrayBuffer(blob);

onClose();
}
}, 'image/webp');
}
function getCropData() {
getCrop(crop, setPreviewImg, books, setBooks, onClose);
}

function onSubmit() {
Expand Down
62 changes: 60 additions & 2 deletions src/components/forms/utils/utilsForm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef } from 'react';
// const fileInputRef = useRef<HTMLInputElement>(null);
import pako from 'pako';

function handleInputChange(
e,
Expand Down Expand Up @@ -69,4 +69,62 @@ function useFileInputRef() {
return { fileInputRef, handleButtonClick };
}

export { handleInputChange, handleCategory, handleField, useFileInputRef };
async function handleImage(e, setCropData, onOpen) {
const file = e.target.files?.[0];
if (!file) return;

// 1 MB
if (file.size > 1000000) {
alert(
`El tamaño de la imagen es mayor a 1 MB. Por favor, seleccione una imagen de menor tamaño.`,
);

return;
}

const blobImage = new Blob([file], { type: 'image/webp' });
setCropData(URL.createObjectURL(blobImage));

onOpen();
}

async function getCrop(crop, setPreviewImg, books, setBooks, onClose) {
if (typeof crop !== 'undefined') {
const croppedCanvas = crop.getCroppedCanvas();
croppedCanvas.toBlob((blob) => {
setPreviewImg(blob);

if (blob) {
const reader = new FileReader();
reader.onload = function () {
const arrayBuffer = reader.result as ArrayBuffer;
const uint8Array = new Uint8Array(arrayBuffer);
const compressedArrayBuffer = pako.deflate(uint8Array);
const byteArray = [...new Uint8Array(compressedArrayBuffer)];
const publicId = books.image.public_id;
const pId = publicId.replace('xbu/', '');

setBooks((prevBooks) => ({
...prevBooks,
image: {
url: byteArray,
public_id: pId,
},
}));
};
reader.readAsArrayBuffer(blob);

onClose();
}
}, 'image/webp');
}
}

export {
handleInputChange,
handleCategory,
handleField,
useFileInputRef,
handleImage,
getCrop,
};

0 comments on commit 0acf1f9

Please sign in to comment.